Friday, July 17, 2020

Introduction to C++ Programming


Let us start with our first program in C++. Consider the following program code

Program Reference: Cpp_Programs/Day_01/Prog_01.cpp

#include<stdlib.h> 
#include <iostream> 
 

int main(void)
{
  int i1;
  char ch1;
  float f1;

  system("clear");  //TO CLEAR THE SCREEN [use clrscr(); in windows]

  std::cout<<"\n\t\tThis is our first C++ program";
  
  std::cout<<"\nEnter an Integer: ";
  std::cin>>i1;

  std::cout<<"Enter a real No: ";
  std::cin>>f1;

  std::cout<<"Enter a printable Character: ";
  std::cin>>ch1;

  std::cout<<"\n\nYou Have Entered";
  std::cout<<"\n\tInteger = "<<i1;
  std::cout<<"\n\tReal No = "<<f1;
  std::cout<<"\n\tCharacter = "<<ch1;

  std:: cout<<"\n\nEnd of the first program....\n\n\n";
}


Write the cpp program code using any text editor like gedit/ notepad.
Use g++ filename.cpp to compile the program.

The default output file(executable) will be a.out under the current directory.

Output
This is our first C++ program
Enter an Integer: 12
Enter a real No: 12.123
Enter a printable Character: A


You Have Entered
    Integer = 12
    Real No = 12.123
    Character = A

End of the first program....
 








Program Reference: Cpp_Programs/Day_01/Prog_02.cpp

using namespace std;

#include<stdlib.h> 
#include <iostream> 
int main(void)
{
  int i1;
  char ch1;
  float f1;

  system("clear");  //TO CLEAR THE SCREEN [use clrscr(); in windows]

  cout<<"\n\t\tThis is our first C++ program";
  
  cout<<"\nEnter an Integer: ";
  cin>>i1;

  cout<<"Enter a real No: ";
  cin>>f1;

  cout<<"Enter a printable Character: ";
  cin>>ch1;

  cout<<"\n\nYou Have Entered";
  cout<<"\n\tInteger = "<<i1;
  cout<<"\n\tReal No = "<<f1;
  cout<<"\n\tCharacter = "<<ch1;

  cout<<"\n\nEnd of the first program....\n\n\n";
}
 

Output
This is our first C++ program
Enter an Integer: 12
Enter a real No: 12.123
Enter a printable Character: A


You Have Entered
    Integer = 12
    Real No = 12.123
    Character = A

End of the first program....
 







Output of both the program codes are same. Then what is the difference between above two program codes? What is namespace? What is the implication of namespace std in the second program code?