Introduction To C++

C++  is the Object Oriented language, It is the OOPS version of "C" language. 

History

In 1960's 
Simula language was developed, It is the simulation of two programming languages Simula I and Simula 67. 

Simula 67 has introduced object and features concept which would later form as "Object Oriented Concept". Simula was the first OOPS based language. Simula could not do much better with real life applications as it was complex and could not provide security.


In 1970's OOPS concept becomes popular and  in 1980's "Bjarne Stroustrup" has developed OOPS based programming language "C++", he was taken idea from Simula.


Difference Between C and C++

"C" is the procedural language but "C++" is the both procedural and object oriented language.

"C" does not support security for its data but "C++" support by using data hiding feature.


"C" does not support re-usability of code but "C++" is does support re-usability through inheritance feature.


"C" 
does not support exception handling directly but "C++" provides separate feature for exception handling.

"C" 
does not support flush unused memory but "C++" provides can flush out the unused object by using destructor.

"C" language can create functions but "C++" support different types of functions - friend function, inline functions and virtual functions.

"C" 
is very much useful in system designing such as writing operating systems   whereas  "C++" is useful in writing real-life based programmings.

Please go through with links  - OOPS Concept and 
Programming Methodology

Start Working With C++

1. Download Turbo C++ or Visual C++ , turbo is older version.

2. I explained steps in Introduction To C chapter you can go through with installation steps.


3. Save your file with ".cpp" extension.


Let's start with simple program in 
C++.

Program 1 : 

Write a program to enter two numbers and find out the sum.

 #include <iostream>

 using namespace std; 

 int main() {

 int a, b, c;

 cout<<"Enter 1st number"; // use std::cout<< if we don't define std in file.
 cin >> a;

 cout<<"Enter 2nd number"; 
 cin>> b;

 c = a+b;

 cout << "Sum of the numbers is - " <<c

}
Explanation 1 : 

1 - iostream :  is the base class of istream and ostream classes, It defines all input and output stream functions and variables. The iostream is derived from ios class. Please see the hierarchy of classes in the diagram given below.

In the above example pre-processor including a class file having methods to deal with input and output operations of c++.
iostream class hierarchy in c++
Iostream hierarchy in c++. ByTechAchievers.com

2 - using namespace std :   namespace is the keyword to declare the scope of any variable in the file. The "std" is the instance variable of iostream that gives access to use class's variables and methods anywhere in the program. 

Note : If we don't define std in file then all iostream methods and variables will be accessed by std reference.( ex: std::cout , std::cin )

3 - cout and cin : "cout" is the instance variable of ostream and "cin" is the instance variable of istream class. These instances having access of methods and variables of it's corresponding classes. 

4 - << and >> :  "<<"  is known as insertion operator, it insert data from variable or stream to console and  ">>" is known as extraction operator, it sends data from console to specified variable or stream.

Program 2 : 

Write a program to enter employee name, salary, calculate employee 20% bonus and find the total salary.

 #include <iostream>

 using namespace std;

 int main(void) {

 char name [10];

 float salary,bonus, total_salary;

 cout<<"Enter Name";

 cin >> name;

 cout<<"Enter Salary";

 cin>> salary;

 bonus = (salary*20)/100;

 total_salary = salary+bonus;

 cout << "Bonus is  - " <<bonus<<endl;

 cout << "Total salary is  - " <<total_salary;

  return 0;

}
Explanation 1 : 

1 - string behaviour : will discuss it in next chapter.

2
 - endl : is the "string manipulator" in C++, it gives new line. We can also use "\n" in C++ but we can not use endl in C. The "\n" is a character it takes size in memory but endl does not take any memory space.One more benefit of endl is it flush out the stream or console.

Note
Flush-out means sending data from buffer to permanent memory. 


Thanks for reading, please do share and comment your suggestions.



No comments

Powered by Blogger.