C++ Basic Syntax

Here we will discuss one simple and basic C++ program to print "Hello this is C++" and its structure in parts with details and uses.

First C++ program

#include <iostream>
using namespace std;
int main()
cout << "Hello this is C++";

Header files are included at the beginning just like in C program. Here iostream is a header file which provides us with input & output streams. Header files contained predeclared function libraries, which can be used by users for their ease.

Using namespace std, tells the compiler to use standard namespace. Namespace collects identifiers used for class, object and variables. NameSpace can be used by two ways in a program, either by the use of using statement at the beginning, like we did in above mentioned program or by using name of namespace as prefix before the identifier with scope resolution (::) operator.


std::cout << "A";

b>main(), is the function which holds the executing part of program its return type is int.

cout <<, is used to print anything on screen, same as printf in C language.

cin and cout are same as scanf and printf, only difference is that you do not need to mention format specifiers like, %d for int etc, in cout & cin.


For single line comments, use // before mentioning comment, like

cout<<"single line";   // This is single line comment
For multiple line comment, enclose the comment between /* and */
/*this is 
  a multiple line 
  comment */

Using Classes

Classes name must start with capital letter, and they contain data variables and member functions. This is a mere introduction to classes.

class jk201
 int i;           //data variable
 void display()         //Member Function
   cout<<"Inside Member Function";
}; // Class ends here

int main()
 jk201 obj;  // Creatig jk201 class's object
 obj.display();  //Calling member function using class object

This is how class is defined, its object is created and the member functions are used.

Variables can be declared anywhere in the entire program, but must be declared, before they are used. Hence, we don't need to declare variable at the start of the program.

Share this article on