Skip to main content

C++ Basic Syntax

Before start learning of c++ programming there are important to familiar with its syntax and structures. First of all C++ are supporting both functional and object oriented programming. Here given all basic details to make c++ program. See this first program to without class concept.

/*Basic example of 
  C++ Programming.*/
#include<iostream>
//use predefined namespace std
using namespace std;
int main(){
  //display message
  cout<<"C++ Programming";
  return 0;
}
Output
C++ Programming

This program are consist of head file, comments, main function return statements and namespace. Combination of this are making a c++ source code.

comment are used to describe information about the statements (logical and working). There are useful to programmer. Compiler are not execute of comment portion. There are two type of comment are use in c++. Single line comment and multiline comments.

 1) single line
//demo single line comment
or
//this is continuation of single line\
in our program

 2) multi line example
/*Demo of
  multiline comment
*/

We can use both type of comment in our program. learn more information about comment in next section.

header files like a combinations of method class statements. when we are included any header file in program then capable to access those function and statement in our program. There are is an two type predefined and user defined. In this program include one predefined header file iostream there are provide input output mechanism. Look at view this example.

/*Basic  input/output example of 
  C++ Programming.*/
#include<iostream>
//use predefined namespace std
using namespace std;
int main(){
  int number; //variable

  //display message
  cout<<"Enter one integer value :";
  //input value
  cin>>number;
  //display result
  cout<<"Your are put "<<number;
  return 0;
}
Output
Enter one integer value :100
Your are put 100

cout is object of ostream class. Help of this object and << (left shift) operator we are display the value of variable and constant statements. << operator is also known as insertion or put to operator.

cin (pronounced as 'c in') is object of istream class. help of (>>) operator provided dynamic value of any variables. this operator (>>) is also known as extraction or get from operator).

namespace are used to define a scope or uniqueness of scope. Or we can say namespace are resolve the ambiguity of the access of scopes. std is namespace of above program.

main() is function. By default execution of program will start with main function. And executes the all statements which are define by inside main function.

Create class in C++

Class is an special concept of object oriented programming. that are protected to data and function of outside the class. Let take simple example of c++ class.

/*Basic example of c++ class.*/
#include<iostream>
using namespace std;
class Example{
  //data member of class
  int item;
  
  public:
  //class constructor
  Example(int value){
    //assign initial value to 
    //item variable
    item=value;
  }
  void print(){
    cout<<"item is :"<<item;
  }
};
int main(){
  //create object of Example class
  Example obj(786);
  obj.print(); //call member function
  return 0;
}
Output
item is :786

Here given basic example of c++ class. Observe that variables and function are declared inside a class and those variable and function are only accessing by class object.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment