C++ Basic Syntax

Before start learning of c++ programming there are important to familiar with its syntax and structures. here given all basic details to make c++ program. consider a basic example of c++ program.

/*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 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++

/*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.

© 2019, regularcodes.com, All rights reserved