Introduction to C++
INTRODUCTION TO C++
Beginner to C++…????
Usually when people hear the programming language "C++", they think it is next language to C programming. But C++ is different from C and has its own importance in programming field. If you know the syntax in C++ you can write any basic programs and understand the concepts very easily.
- C++ is not really an extension of the C language, where some new symbols have been added. The basic purpose of C++ language is to add features to the C language that support the concepts of OOP(Object oriented Programming. Ex: even JAVA).
C++ fully supports Object oriented programming including the four pillars of object-oriented development: encapsulation, data hiding, inheritance and polymorphism.
· The core language giving all the building blocks including variables, data types and literals, etc.
· The C++ Standard Library giving a rich set of functions manipulating files, strings, etc.
· The Standard Template Library (STL) giving a rich set of methods manipulating data structures, etc.
Pre-requisites to start the programming in C++ (Windows).
1) Install the software which works for the C++ (Like Code-blocks, Turbo C++), which helps in working with the C++.
2) Click on Create New project, next it asks for template selection, so select the option “Console Application” and click on “GO” button. Then you will get the message of Console Application. Then click on Next, Now the Application asks for options (C or C++), Click C++.
3) Then Application asks for some project title and link of the folder where the program should be saved. Answer the requirements and click Next, and in next page set the compiler to GNC GCC compiler and click Next.
4) Home page of the C++ for programming is ready for use now. Left side of the page we see Filename as we saved and has Source (Click “+”), You can see main.cpp(Double click), the code will be ready (“Hello World”).
5) Modify a program in text editors as required.
6) File name will be saved as extension .cpp (eg: program.cpp).
7) Click on Build and Run option, Command prompt occurs and program will be
Executed directly.
Simple Code 1,for C++ to understand the Syntax:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Explanation:
#include <iostream> :
It is the predefined library function used for input and output also called as header files. iostream is the header file which contains all the functions of program like cout, cin etc. and #include tells the pre-processor to include these header file in the program.
using namespace std:
std is the standard namespace. cout, cin and a lot of other things are defined in it. ( This means that one way to call them is by using std::cout and std::cin.) The keyword using technically means, use this whenever you can.
Cout and Cin:
C++ Input/Output. ... There are two variables (among others) defined in <iostream>. cout is used for output, cin for input. Important Point. cout and cin are not key words in the C++ language. They are variables, instances of classes, that have been declared in <iostream>. cout is a variable of type ostream.
<< and >>:
This refers to the Input and output.
Simple Code 2:
// Header Files
#include<iostream>
#include<conio.h>
//Std namespace declaration
using namespace std;
//Main Function
int main()
{
//Standard Ouput Statement
cout<<"My First C++ Program";
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Use of getch():
getch() is a way to get a user inputted character. putch() and putchar() are used to write a character to screen. getch() and putch() are non-standard functions defined in conio.h, mostly used in turbo C/dev C++ environement.
Conio.h:
conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C, nor is it defined by POSIX. This header declares several useful library functions for performing "console input and output" from a program.
No comments: