BIIT

C++ Programing

Introduction:
C++ is a General-purpose, compiled, Case-sensitive, free-form programming language that supports procedural and object-oriented programming language. C++ is a middle-level language. C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.

Difference Between Procedure Oriented Programming & Object Oriented Programming

POPOOP
It is a programming technique in which stress is given on functions rather than data.It is a programming technique in which stress is given on the data rather than functions.
In this programming the data members are global and they float from function to function. In this programming the data members are not global and they remain attached with their specific function.
Data hiding/Encapsulation is not possible in POP. Data hiding/Encapsulation is one of the Principles of OOP.
In this programming technique problem is divided into functions. In this programming technique problem is divided into segments (i.e. modules).
It is a Top down approach of Programming. It is a Bottom up approach of Programming.
No inheritance is possible in POP. Inheritance is one of the basic principles of OOP.
Polymorphism is not possible in POP. Polymorphism is also one of the basic principles of OOP.
Function overloading is not possible in POP. Function overloading is possible in OOP.
Function overriding is not possible in POP. Function overriding is possible in OOP.
Operator overloading is not possible in POP. Operator overloading is possible in OOP.
Mapping between Data and Function is difficult and complicated. Mapping between Data and Function can be used using "Objects".
Data abstraction is not possible in POP. Data abstraction is possible in OOP.
We can’t use access specifiers (i.e. public/private/protected) in POP. We can use access specifiers (i.e. public/private/protected) in OOP.
Examples: COBOL, BASIC, FORTRAN, C etc. Examples: C++, JAVA, PYTHON, VISUAL BASIC etc.

Difference Between C Language & C++ Language

C LanguageC++ Language
It is a Procedural & Structural Language. It is an Object oriented Language.
It is a Top down approach of Programming. It is a Bottom up approach of Progrmming.
It uses printf() function for output and scanf() function for input. It uses cout<< for output and cin>> for input.
In C, Polymorphism is not possible. The concept of polymorphism is used in C++.
Operator overloading is not possible in C. Operator overloading is one important Feature of C++.
Mapping between Data and Function is difficult and complicated. Mapping between Data and Function can be used using "Objects"
C requires all the variables to be defined at the starting of a scope. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.
No inheritance is possible in C. Inheritance is possible in C++
In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating. In C++, new and delete operators are used for Memory Allocating and Deallocating.
It supports built-in and primitive data types. It supports both built-in and user define data types.
In C, Exception Handling is not present. In C++, Exception Handling is done with Try and Catch block.
No virtual Functions are present in C The concept of virtual Functions are used in C++.
Object Oriented Programming:
A Programming that is oriented about an object is known as Object Oriented Programming (OOP) and a language that supports such Programming is known as Object Oriented Programming Language (OOPL).
In other words Object Oriented means we organize our software as a combination of different types of objects that incorporates both data and behaviour.
Object-oriented programming (OOPs) is a methodology that simplify software development and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
5. Data hiding
6. Message Passing
7. Dynamic binding
Encapsulation:
Wrapping of the data and code into a single unit is known as encapsulation. Here single unit is referred to as class.
Inheritance:
To acquire the properties of one Class or Object into another Class or Object is known as Inheritance.
Polymorphism:
“Single Interface Multiple Methods” is the concept of Polymorphism.
Abstraction:
“To show the abstract view and hide the complexity” is the concept of Data Abstraction.
Data hiding:
“To access the data inside the class and not by object (ie outside of class)” is the concept of Data hiding.
Message Passing:
function call statement means sending some message to function and then receiving what is done by the function. This is called communication. This communication is known as Message Passing.
Dynamic binding:
Linking of function call statement with the function prototype is known as Dynamic Binding.
Input & Output in C++:
In C++ we use cin and cout objects for input and output. C++ standard libraries provide an these objects.
We use a header file for same purpose:
iostream
This header file defines the cin & cout objects, which correspond to the standard input stream and the standard output stream respectively.
The Standard Output Stream (cout)
The predefined object cout is an instance of ostream class. The cout object is said to be "connected to" the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << (ie two less than symbols).
The Standard Input Stream (cin)
The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> (ie two greater than symbols).
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int main()
	{
		cout<<"Welcome";
		return 0;
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int main()
	{
		int x;
		float y;
		char z;
		x=5;
		y=4.4;
		z='a';

		cout<<endl<<"x is "<<x<<endl<<"y is "<<y<<endl<<"z is "<<z;
		return 0;
	}
	
	
Example 3:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int main()
	{
		int x;
		float y;
		char z;
		cout<<"Enter the value of x ";
		cin>>x;
		cout<<"Enter the value of y ";
		cin>>y;
		cout<<"Enter the value of z ";
		cin>>z;
		cout<<endl<<"x = "<<x;
		cout<<endl<<"y = "<<y;
		cout<<endl<<"z = "<<z;
		return 0;
	}
	
	
Example 4:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int main()
	{
		int x;
		float y;
		char z;
		cout<<"Enter the values of x, y & z ";
		cin>>x>>y>>z;
		cout<<"\nx = "<<x<<"\ny = "<<y<<"\nz = "<<z;
	}
	
	
Review of C Language
Click on me
Class & Object:


Class:
Class is a collection of data and code which is never existing thing. it can be said to as architecture or blue print of an object. it can be designed only.
Syntax:
	
	class class_name
	{
		 Access_specifier:
					 data
					 code
		 Access_specifier:
					 data
					 code
		  ----------
		  ----------
		  ----------
	};
	
	
Object:
Object is also a collection of data and code which is always existing thing. it can be said to as instance of a class. it can be created also.
Syntax:
class_name Object_name;
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	
	//Class Design
	class demo
	{
		private:
			int x;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x ";
				cin>>x;
			}
			void showdata()
			{
				cout<<"\nValue of x "<<x;
			}
	};	
	int main()
	{
		demo d;	//Creating Object
		d.getdata();
		d.showdata();	
	}
	
	
Scope Resolution Operator:
There are two uses of this Operator:
1. To access the Global Variable.
2. To specify class for the member function of any class.
1. To access the Global Variable:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int x=55;//Global Variable
	int main()
	{
		int x=22;//Local Variable
		cout<<"\nLocal Value of x is "<<x;	
		cout<<"\nGlobal Value of x is "<<::x;	
	}
	
	
2. To specify class for the member function of any class:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float m;
		char n[10];
		public:
		void getdata();
		void showdata();
	};
	void student :: getdata()
	{
		cout<<"\nEnter your rn, name and marks ";
		cin>>rn>>n>>m;
	}
	void student::showdata()
	{
		cout<<"\nyour rn is "<<rn;
		cout<<"\nyour name is "<<n;
		cout<<"\nyour marks is "<<m;
	}
	int main()
	{
		student s;
		s.getdata();
		s.showdata();	
	}
	
	
Object can be passed in a non member function:
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		public:
		int rn;
		float m;
		char n[10];
		void getdata();
	};
	void student::getdata()
	{
		cout<<"\nEnter your rn, name and marks ";
		cin>>rn>>n>>m;
	}
	void showdata(student st)
	{
		cout<<"\nyour rn is "<<st.rn;
		cout<<"\nyour name is "<<st.n;
		cout<<"\nyour marks is "<<st.m;
	}
	int main()
	{
		student s;
		s.getdata();
		showdata(s);	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			int x,y;
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
	};
	void add(demo obj1,demo obj2)
	{
		demo obj3;
		obj3.x=obj1.x+obj2.x;
		obj3.y=obj1.y+obj2.y;
		obj3.showdata();	
	}
	main()
	{
		demo ob1,ob2;
		ob1.getdata();
		ob2.getdata();
		add(ob1,ob2);		
	}
	
	
Object can be returned from a non member function:
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		public:
		int rn;
		float m;
		char n[10];
		void showdata();
	};
	student getdata()
	{
		student st;
		cout<<"\nEnter your rn, name and marks ";
		cin>>st.rn>>st.n>>st.m;
		return st;
	}
	void student::showdata()
	{
		cout<<"\nyour rn is "<<rn;
		cout<<"\nyour name is "<<n;
		cout<<"\nyour marks is "<<m;
	}
	int main()
	{
		student s;
		s=getdata();
		s.showdata();	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			int x,y;
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
	};
	demo add()
	{
		demo obj1,obj2,obj3;
		obj1.getdata();
		obj2.getdata();
		obj3.x=obj1.x+obj2.x;
		obj3.y=obj1.y+obj2.y;
		return obj3;	
	}
	main()
	{
		demo obj;
		obj=add();		
		obj.showdata();
	}
	
	
Object can be passed & returned with a non member function:
Example 1:
	
	#include<iostream>
	#include<conio.h>
	#include<string.h>
	using namespace std;
	class student
	{
		public:
		int rn;
		float m;
		char n[10];
		void getdata();
		void showdata();
	};
	void student::getdata()
	{
		cout<<"\nEnter your rn, name and marks ";
		cin>>rn>>n>>m;
	}
	void student::showdata()
	{
		cout<<"\nyour rn is "<<rn;
		cout<<"\nyour name is "<<n;
		cout<<"\nyour marks is "<<m;
	}
	student modifydata(student st)
	{
		student rst;
		rst.rn=st.rn+10;
		strcpy(rst.n,st.n);
		strcat(rst.n," Gupta");
		rst.m=st.m+20;
		return rst;
	}
	int main()
	{
		student s,rs;
		s.getdata();
		rs=modifydata(s);
		rs.showdata();	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			int x,y;
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
	};
	demo add(demo obj1,demo obj2)
	{
		demo obj3;
		obj3.x=obj1.x+obj2.x;
		obj3.y=obj1.y+obj2.y;
		return obj3;	
	}
	main()
	{
		demo ob1,ob2,ob3;
		ob1.getdata();
		ob2.getdata();
		ob3=add(ob1,ob2);		
		ob3.showdata();
	}
	
	
Object can be passed & returned with a member function:
Example 1:
	
	#include<iostream>
	#include<conio.h>
	#include<string.h>
	using namespace std;
	class student
	{
		int rn;
		float m;
		char n[10];
		public:
		void getdata();
		void showdata();
		student modifydata(student);
	};
	void student::getdata()
	{
		cout<<"\nEnter your rn, name and marks ";
		cin>>rn>>n>>m;
	}
	void student::showdata()
	{
		cout<<"\nyour rn is "<<rn;
		cout<<"\nyour name is "<<n;
		cout<<"\nyour marks is "<<m;
	}
	student student::modifydata(student st)
	{
		student rst;
		rst.rn=st.rn+10;
		strcpy(rst.n,st.n);
		strcat(rst.n," Gupta");
		rst.m=st.m+20;
		return rst;
	}
	int main()
	{
		student s,rs,temp;
		s.getdata();
		rs=temp.modifydata(s);
		rs.showdata();	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
			int x,y;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
			demo add(demo obj2)
			{
				demo obj3;
				obj3.x=x+obj2.x;
				obj3.y=y+obj2.y;
				return obj3;	
			}
	};
	main()
	{
		demo ob1,ob2,ob3;
		ob1.getdata();
		ob2.getdata();
		ob3=ob1.add(ob2);		
		ob3.showdata();
	}
	
	
Function Overloading:
If two or more functions share common name, we can say this situation becomes overloading of functions. Following are the points that can be seen when function overloading is being done:
* Name of all overloaded functions must be same.
* Prototypes of all overloaded functions must be different.
* Overloading of fuctions supports the concept of Polymorphism (Compile Time Polymorphism).
* Overloading of functions can be possible in single Class also.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	void show()
	{
		cout<<"\nWelcome to BIIT";
	}
	void show(int x)
	{
		cout<<"\nvalue of x = "<<x;
	}
	main()
	{
		show();
		show(5);	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	int area(int side)
	{
		int ar;
		ar=side*side;
		return ar;
	}
	float area(float rad)
	{
		float ar;
		ar=3.14*rad*rad;
		return ar;
	}
	int area(int len,int br)
	{
		int ar;
		ar=len*br;
		return ar;
	}
	main()
	{
		int sd,l,b;
		float r;
		cout<<"\nFor Square";
		cout<<"\nEnter the Side of any Square ";
		cin>>sd;
		cout<<"\nArea of Square = "<<area(sd);
		cout<<"\nFor Rectangle";
		cout<<"\nEnter the Length & Bredth of any Rectangle ";
		cin>>l>>b;
		cout<<"\nArea of Rectangle = "<<area(l,b);
		cout<<"\nFor Circle";
		cout<<"\nEnter the Radius of any Circle ";
		cin>>r;
		cout<<"\nArea of Circle = "<<area(r);
	}
	
	
Example 3:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demooverload
	{
			public:
				int area(int side)
				{
					int ar;
					ar=side*side;
					return ar;
				}
				float area(float rad)
				{
					float ar;
					ar=3.14*rad*rad;
					return ar;
				}
				int area(int len,int br)
				{
					int ar;
					ar=len*br;
					return ar;
				}
	};
	main()
	{
		demooverload obj;
		int sd,l,b;
		float r;
		cout<<"\nFor Square";
		cout<<"\nEnter the Side of any Square ";
		cin>>sd;
		cout<<"\nArea of Square = "<<obj.area(sd);
		cout<<"\nFor Rectangle";
		cout<<"\nEnter the Length & Bredth of any Rectangle ";
		cin>>l>>b;
		cout<<"\nArea of Rectangle = "<<obj.area(l,b);
		cout<<"\nFor Circle";
		cout<<"\nEnter the Radius of any Circle ";
		cin>>r;
		cout<<"\nArea of Circle = "<<obj.area(r);
	}
	
	
Constructor:
A special type of function that follows the rules defined below:
Rules:
1. Name of Constructor must be same as the name of Class.
2. Constructors are used to initialize something.
3. Constructors may or may not have arguments.
4. Since Constructors do not return anything, so there is no need to have any return-type with this function prototype.
5. Constructors must be declared as public.
6. Constructor is not called by any object but it is called itself when an object of same class (to which it is declared as constructor) is created.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			demo()
			{
				cout<<"\nConstructor is being run";
			}
	};
	main()
	{
		demo d;		
		getch();
	}
	
	
Types of Constructors:
There are three types of constructors used in C++:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
1. Default Constructor
A Constructor that has no Argument is known as Default Constructor.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x;
		public:
			demo()
			{
				x=0;
			}
			void show()
			{
				cout<<"\nx = "<<x;
			}
	};
	main()
	{
		demo d,a,b;		
		d.show();
		a.show();
		b.show();
		getch();
	}
	
	
2. Parameterized Constructor
A Constructor that has at least one parameter is known as parameterized constructor. It is also known as Dynamic Constructor.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x;
		public:
			demo(int p)
			{
				x=p;
			}
			void show()
			{
				cout<<"\nx = "<<x;
			}
	};
	main()
	{
		demo obj1(55);		
		obj1.show();
		demo obj2=155;		
		obj2.show();
		int n=22;
		demo obj3(n);		
		obj3.show();
		cout<<"\nEnter the value of n ";
		cin>>n;
		demo obj4(n);		
		obj4.show();
		getch();
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x,y;
		public:
			demo(int p,int q)
			{
				x=p;
				y=q;
			}
			void show()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;
			}
	};
	main()
	{
		demo obj1(55,33);		
		obj1.show();
		demo obj2={55,33};		
		obj2.show();
		getch();
	}
	
	
3. Copy Constructor
A Constructor that is used to make a copy of one object is known as Copy Constructor.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x;
		public:
			demo()
			{
				x=0;
			}
			demo(int p)
			{
				x=p;
			}
			demo(demo &d)//copy constructor
			{
				x=d.x;
			}		
			void show()
			{
				cout<<"\nx = "<<x;
			}
	};
	main()
	{
		demo obj1,obj2(99),obj3(obj1),obj4=obj2;		
		cout<<"\nObject 1";
		obj1.show();
		cout<<"\nObject 2";
		obj2.show();
		cout<<"\nObject 3";
		obj3.show();
		cout<<"\nObject 4";
		obj4.show();
		getch();
	}
	
	
Overloading of Constructors:
Since a Constructor is a special type of function and a function can be overloaded, this is why Constructor can also be overloaded.
• If a class has two or more constructors, this concept is known as Overloading of Constructors.
• All the constructors must have same name (as the name of class).
• All the constructors must have different prototypes. That means No. & Type of arguments must be different for all constructors.
• Overloading of Constructors also supports the concept of Polymorphism (Compile time).
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x;
		public:
			demo()	//default constructor
			{
				x=0;
			}
			demo(int p)//parameterized constructor
			{
				x=p;
			}
			void show()
			{
				cout<<"\nx = "<<x;
			}
	};
	main()
	{
		demo obj1,obj2(99);		
		obj1.show();
		obj2.show();
		getch();
	}
	
	
Destructor:
A special type of function that follows the rules defined below:
Rules:
1. Name of Destructor must be same as the name of Class like a Constructor but a tilde symbol (~) must be succeeded by function prototype defined in class.
2. Destructors are used to de-initialize what is initialized by Constructors.
3. Destructors can never have arguments.
4. Since Destructors do not return anything, so there is no need to have any return-type with this function prototype.
5. Destructors must be declared as public.
6. Destructor is not called by any object but it is called itself when an object of same class (to which it is declared as destructor) is destroyed.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			~demo()
			{
				cout<<"\nDestructor is being run";
			}
	};
	main()
	{
		demo d;		
		getch();
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		int x;
		public:
			demo()
			{
				x=55;
			}
			void show()
			{
				cout<<"\nx = "<<x;
			}
			~demo()
			{
				x=0;
			}
	};
	main()
	{
		demo d;
		d.show();		
		getch();
	}
	
	
Operator Overloading:
If an operator is used for two or more operations, we can say this situation becomes overloading of operators. Following are the points that can be seen when operator overloading is being done:
* Single Operator can be used for two or more Operations.
* An operator keyword is used for same purpose.
* Operator keyword is succeeded by the operator symbol when the work of operator is defined by using a function.
* Overloading of Operator supports the concept of Polymorphism (Compile Time Polymorphism).
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
			int x,y;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
			demo operator+(demo obj2)
			{
				demo obj3;
				obj3.x=x-obj2.x;
				obj3.y=y*obj2.y;
				return obj3;	
			}
	};
	main()
	{
		demo ob1,ob2,ob3;
		ob1.getdata();
		ob2.getdata();
		ob3=ob1+ob2;		
		ob3.showdata();
	}
	
	
Types of Operator Overloading:
Threre are two categories of same:
1. Overloading of Binary Operators
2. Overloading of Unary Operators
1. Overloading of Binary Operators:
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
			int x,y;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
			demo operator+(demo obj2)
			{
				demo obj3;
				obj3.x=x+obj2.x;
				obj3.y=y+obj2.y;
				return obj3;	
			}
	};
	main()
	{
		demo ob1,ob2,ob3;
		ob1.getdata();
		ob2.getdata();
		ob3=ob1+ob2;		
		ob3.showdata();
	}	
	
	
2. Overloading of Unary Operators:
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
			int x,y;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
			demo operator++()
			{
				demo obj3;
				obj3.x=x+1;
				obj3.y=y+1;
				return obj3;	
			}
	};
	main()
	{
		demo ob1,ob3;
		ob1.getdata();
		ob3=++ob1;		
		ob3.showdata();
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
			int x,y;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x & y ";
				cin>>x>>y;			
			}
			void showdata()
			{
				cout<<"\nx = "<<x;
				cout<<"\ny = "<<y;			
			}
			void operator++()
			{
				x++;
				y++;
			}
	};
	main()
	{
		demo ob1;
		ob1.getdata();
		++ob1;		
		ob1.showdata();
	}
	
	
Access Specifiers:
A Keyword which is used to specify what type of access is given to the code and data defined under this keyword. of Access specifiers:
There are three access specifiers used in C++ language.
1. private:
if private keyword is used, then data and code defined under this keyword are accessible to the same class in which they are defined. That means we can say member functions of the class can access private members of the class.
2. public:
if public keyword is used, then data and code defined under this keyword are accessible to the same class as well as other classes also. That means we can say member as well as non-member functions can access the public members of any class.
3. protected:
if protected keyword is used, then data and code defined under this keyword are accessible to the same class as well as to it’s derived classes. Other class can never access to the protected members of any class. That means we can say member functions of the base and derived classes can access private members of the class.
Base Class:
A Class that provides its properties to another class, is known as Base Class. it is also known as Parent or Super class.
Derived Class:
A Class that acquires the properties of Base class, is known as Derived Class. it is also known as Child or Sub class.
Member Data:
A variable, which is declared inside the body of any class, is known as member data.
Member Function:
A function, which is defined inside the body of any class, is known as member function.
For example:
	
	class demo
	{
		int x;   //Member Data
		void show() //Member function
		{
			…….
			…….
		}
	}; 
	
	
Non-member function:
A function, which is defined outside of the body of any class, is known as non-member function.
For Example :
	
	class demo
	{
			…….
			…….
	}; 
	void show() //Non-member function
	{
			…….
			…….
	} 
	
	
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		public:
			int x;	
	};
	int main()
	{
		demo d;
		cout<<"\nEnter the value of x ";
		cin>>d.x;
		cout<<"\nValue of x is "<<d.x;	
		getch();
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demo
	{
		private:
			int x;
		public:
			void getdata()
			{
				cout<<"\nEnter the value of x ";
				cin>>x;
			}
			void showdata()
			{
				cout<<"\nValue of x "<<x;
			}
	}d;	
	int main()
	{
		d.getdata();
		d.showdata();	
	}

	
Example 3:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		private:
			int rn;
			float m;
			char n[10];
		public:
			void getdata()
			{
				cout<<"\nEnter your rn, name and marks ";
				cin>>rn>>n>>m;
			}
			void showdata()
			{
				cout<<"\nyour rn is "<<rn;
				cout<<"\nyour name is "<<n;
				cout<<"\nyour marks is "<<m;
			}
	};
	int main()
	{
		student s,st;
		s.getdata();
		st.getdata();
		s.showdata();	
		st.showdata();	
	}
	
	
Inheritance:
if an object qcquires the properties of another object, then this property is known as Inheritance.
At least two classes are involved in this procedure:
Base Class:
A Class that provides its properties to another class, is known as Base Class. it is also known as Parent or Super class.
Derived Class:
A Class that acquires the properties of Base class, is known as Derived Class. it is also known as Child or Sub class.
Access Specifiers:
A Keyword which is used to specify what type of access is given to the code and data defined under this keyword. of Access specifiers:
There are three access specifiers used in C++ language.
1. private:
if private keyword is used, then data and code defined under this keyword are accessible to the same class in which they are defined. That means we can say member functions of the class can access private members of the class.
2. public:
if public keyword is used, then data and code defined under this keyword are accessible to the same class as well as other classes also. That means we can say member as well as non-member functions can access the public members of any class.
3. protected:
if protected keyword is used, then data and code defined under this keyword are accessible to the same class as well as to it’s derived classes. Other class can never access to the protected members of any class. That means we can say member functions of the base and derived classes can access private members of the class.
Few terms important for Inheritance:
Member Data:
A variable, which is declared inside the body of any class, is known as member data.
Member Function:
A function, which is defined inside the body of any class, is known as member function.
For example:
	
	class demo
	{
		int x;   //Member Data
		void show() //Member function
		{
			…….
			…….
		}
	}; 
	
	
Non-member function:
A function, which is defined outside of the body of any class, is known as non-member function.
For Example :
	
	class demo
	{
			…….
			…….
	}; 
	void show() //Non-member function
	{
			…….
			…….
	} 
	
	
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base 
	{
		private:
			int privdata;
		protected:
			int protdata;
		public:
			int pubdata;
			void initdatainbase()
			{
				cout<<"\nInitdata In Base Class";
				privdata=1;
				protdata=2;
				pubdata=3;
			}
			void showdatainbase()
			{
				cout<<"\nShowdata In Base Class";
				cout<<"\nPrivdata = "<<privdata;
				cout<<"\nProtdata = "<<protdata;
				cout<<"\nPubdata = "<<pubdata;			
			}
	};
	class derived:public base
	{
		public:
			void initdatainderived()
			{
				cout<<"\nInitdata In Derived Class";
				//privdata=11;
				protdata=22;
				pubdata=33;
			}
			void showdatainderived()
			{
				cout<<"\nShowdata In Derived Class";
				//cout<<"\nPrivdata = "<<privdata;
				cout<<"\nProtdata = "<<protdata;
				cout<<"\nPubdata = "<<pubdata;		
			}
	};
	class other
	{
		base b;
		public:
			void initdatainother()
			{
				cout<<"\nInitdata In other Class";
				//privdata=111;
				//protdata=222;
				b.pubdata=333;
			}
			void showdatainother()
			{
				cout<<"\nShowdata In other Class";
				//cout<<"\nPrivdata = "<<privdata;
				//cout<<"\nProtdata = "<<protdata;
				cout<<"\nPubdata = "<<b.pubdata;	
			}
	};

	main()
	{
		//base b;
		derived d;
		other o;
		//b.initdatainbase();
		//b.showdatainbase();
		d.initdatainderived();
		d.showdatainderived();		
		d.initdatainbase();
		d.showdatainbase();
		o.initdatainother();
		o.showdatainother();
	}	
	
	
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class other
	{
		base b;
		public:
			void initdatainother()
			{
				cout<<"\nInitdata In other Class";
				//privdata=11;
				//protdata=22;
				b.pubdata=33;
			}
			void showdatainother()
			{
				cout<<"\nShowdata In other Class";
				//cout<<"\nPrivdata = "<<privdata;
				//cout<<"\nProtdata = "<<protdata;
				cout<<"\nPubdata = "<<b.pubdata;	
			}
	};

	class emp
	{
		int eid;
		public:
			void getemp()
			{
				cout<<"\nEnter the Employee ID of employee ";
				cin>>eid;
			}
			void showemp()
			{
				cout<<"\nEmployee ID = "<<eid;
			}
	};
	class manager:public emp
	{
		char cat[10];
		public:
			void getmanager()
			{
				cout<<"\nEnter Category of Manager ";
				cin>>cat;
			}
			void showmanager()
			{
				cout<<"\nCategory = "<<cat;
			}	
	};
	int main()
	{
		manager m;
		m.getemp();
		m.getmanager();
		m.showemp();
		m.showmanager();
	}
	
	
Function Overriding:
If two or more functions share same name, it becomes function overriding if it follows rules defined below:
1. Names of all overridden functions must be same.
2. Prototypes of all overridden functions must be same.
3. Overriding of functions must be possible only in the concept of inheritance.
4. If function is defined inside the base class then it is redefined again with same prototype in it’s derived classes.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		int x;
		public: 
			void getdata()
			{
				cout<<"\nEnter the value of x ";
				cin>>x;
			}
			void showdata()
			{
				cout<<"\nValue of x = "<<x;
			}
	};
	class derived :public base
	{
		int y;
		public: 
			void getdata()
			{
				base::getdata();
				cout<<"\nEnter the value of y ";
				cin>>y;
			}
			void showdata()
			{
				base::showdata();
				cout<<"\nValue of y = "<<y;
			}
	};
	main()
	{
		derived d;
		d.getdata();
		d.showdata();
		getch();
	}
	
	

Difference Between Overloading & Overriding

Function OverloadingFunction Overriding
It is possible in single class also. It is not possible in single class but it is possible in the base and derived class.
It is possible that more than one definition of a function can be defined in single class. It is not possible to have more than one definition in single class but if one body is defined in base class then other body can be defined inside its derived class only.
Prototypes of all functions must be different. Prototypes of all functions must be same.


Types of Inheritances
In C++ Inheritance is categorized into 5 Categories:
1. Single level Inheritance
2. Multilevel Inheritance
3. Hierarcical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Level Inheritance:
If there is a one single base class and one single derived class in the Inheritance, it is known as Single Level Inheritance.


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class employee
	{
		int e_id;
		char e_name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the Employee ID & Employee Name ";
				cin>>e_id>>e_name;
			}
			void showdata()
			{
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Employee Name\t: "<<e_name;
			}
	};
	class manager : public employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	main()
	{
		manager m;
		m.getdata();
		m.showdata();
		getch();
	}
	
	
2. Multilevel Inheritance:
If there is a one single class(Base class) provides its features to another one single class(Derived Class) and this(Derived class) provides its features to another class in the Inheritance, it is known as Multilevel Inheritance.


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class person
	{
		int p_ano;
		char p_name[20];
		protected:
			void getdata()
			{
				cout<<endl<<"Enter the Aadhar No & Name ";
				cin>>p_ano>>p_name;
			}
			void showdata()
			{
				cout<<endl<<"Aadhar No\t: "<<p_ano;
				cout<<endl<<"Name\t\t: "<<p_name;
			}
	};
	class student : public person
	{
		int r_no;
		char qual[20];
		protected:
			void getdata()
			{
				person::getdata();
				cout<<endl<<"Enter the Roll No & Qualification ";
				cin>>r_no>>qual;
			}
			void showdata()
			{
				person::showdata();
				cout<<endl<<"Roll No\t\t: "<<r_no;
				cout<<endl<<"Qualification\t: "<<qual;
			}
	};
	class employee : public student
	{
		int e_id;
		double e_sal;
		public:
			void getdata()
			{
				student::getdata();
				cout<<endl<<"Enter the Employee ID & Salary ";
				cin>>e_id>>e_sal;
			}
			void showdata()
			{
				student::showdata();
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Salary\t\t: "<<e_sal;
			}
	};
	class manager : public employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	main()
	{
		manager m;
		m.getdata();
		m.showdata();
		getch();
	}
	
	
3. Hierarchical Inheritance:
If One single class(Base Class) provides its features to two or more classes(Derived classes) in the Inheritance, it is known as Hierarchical Inheritance.


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class employee
	{
		int e_id;
		char e_name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the Employee ID & Employee Name ";
				cin>>e_id>>e_name;
			}
			void showdata()
			{
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Employee Name\t: "<<e_name;
			}
	};
	class manager : public employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	class scientist : public employee
	{
		char no_rp[20];
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the No. of Research Papers Published ";
				cin>>no_rp;
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"No. of \t\t: "<<no_rp<<endl<<"Research Papers ";
			}
	};
	class laborer : public employee
	{	
	};
	main()
	{
		manager m;
		scientist s;
		laborer l;
		cout<<endl<<endl<<"Manager";
		m.getdata();
		cout<<endl<<endl<<"Scientist";
		s.getdata();
		cout<<endl<<endl<<"Laborer";
		l.getdata();
		cout<<endl<<endl<<"Manager";	
		m.showdata();
		cout<<endl<<endl<<"Scientist";
		s.showdata();
		cout<<endl<<endl<<"Laborer";
		l.showdata();
		getch();
	}
	
	
4. Multiple Inheritance:
If Two or more Classes(Base Classes) provide their features to one single class (Derived Class) in the Inheritance, it is known as Multiple Inheritance.


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class A
	{	
		...........
		...........
		...........
	};
	class B
	{
		...........
		...........
		...........
	};
	class C:public A, public B
	{
		...........
		...........
		...........
	};
	void main()
	{
		...........
		...........
		...........
	}
	
	

For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int r_no;
		char qual[20],name[20];
		protected:
			void getdata()
			{
				cout<<endl<<"Enter the Roll No, Name & Qualification ";
				cin>>r_no>>name>>qual;
			}
			void showdata()
			{
				cout<<endl<<"Roll No\t\t: "<<r_no;
				cout<<endl<<"Name\t\t: "<<name;
				cout<<endl<<"Qualification\t: "<<qual;
			}
	};
	class employee
	{
		int e_id;
		double e_sal;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the Employee ID & Salary ";
				cin>>e_id>>e_sal;
			}
			void showdata()
			{
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Salary\t\t: "<<e_sal;
			}
	};
	class manager : public student, public employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				student::getdata();
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				student::showdata();
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	main()
	{
		manager m;
		m.getdata();
		m.showdata();
		getch();
	}
	
	
5. Hybrid Inheritance:
If Two or more inheritances are combined together, then it is known as Hybrid Inheritance.


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int r_no;
		char qual[20];
		protected:
			void getdata()
			{
				cout<<endl<<"Enter the Roll No & Qualification ";
				cin>>r_no>>qual;
			}
			void showdata()
			{
				cout<<endl<<"Roll No\t\t: "<<r_no;
				cout<<endl<<"Qualification\t: "<<qual;
			}
	};
	class employee
	{
		int e_id;
		char e_name[20];
		double e_sal;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the Employee ID, Name & Salary ";
				cin>>e_id>>e_name>>e_sal;
			}
			void showdata()
			{
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Name\t\t: "<<e_name;
				cout<<endl<<"Salary\t\t: "<<e_sal;
			}
	};
	class manager : public student, public employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				student::getdata();
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				student::showdata();
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	class scientist : public student, public employee
	{
		char no_rp[20];
		public:
			void getdata()
			{
				student::getdata();
				employee::getdata();
				cout<<endl<<"Enter the No. of Research Papers Published ";
				cin>>no_rp;
			}
			void showdata()
			{
				student::showdata();
				employee::showdata();
				cout<<endl<<"No. of \t\t: "<<no_rp<<endl<<"Research Papers ";
			}
	};
	class laborer : public employee
	{	
	};
	main()
	{
		manager m;
		scientist s;
		laborer l;
		cout<<endl<<endl<<"Manager";
		m.getdata();
		cout<<endl<<endl<<"Scientist";
		s.getdata();
		cout<<endl<<endl<<"Laborer";
		l.getdata();
		cout<<endl<<endl<<"Manager";	
		m.showdata();
		cout<<endl<<endl<<"Scientist";
		s.showdata();
		cout<<endl<<endl<<"Laborer";
		l.showdata();
		getch();
	}
	
	
Constructors in Inheritance:
Threre are two cases for calling the constructors of base class through the object of derived class.

Case 1: for Default Constructors

For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			base()
			{
				cout<<endl<<"Constructor of Base class";			
			}
	};
	class derived1 : public base
	{
		public:
			derived1()
			{
				cout<<endl<<"Constructor of Derived1 class";			
			}
	};
	class derived2 : public base
	{
		public:
			derived2()
			{
				cout<<endl<<"Constructor of Derived2 class";			
			}
	};
	class derived3 : public derived1
	{
		public:
			derived3()
			{
				cout<<endl<<"Constructor of Derived3 class";			
			}
	};
	main()
	{
		cout<<endl<<"When object of derived3 class is created";
		derived3 d3;
		cout<<endl<<"When object of derived2 class is created";
		derived2 d2;
		getch();
	}
	
	

Case 2: for Parameterized Constructors

For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		int x;
		public:
			base(int a)
			{
				x=a;
			}
			void show()
			{
				cout<<endl<<"x = "<<x;			
			}
	};
	class derived1 : public base
	{
		int y;
		public:
			derived1(int b):base(b)
			{
				y=b;
			}
			void show()
			{
				base::show();
				cout<<endl<<"y = "<<y;			
			}
	};
	class derived2 : public base
	{
		int z;
		public:
			derived2(int c):base(c)
			{
				z=c;
			}
			void show()
			{
				base::show();
				cout<<endl<<"z = "<<z;			
			}
	};
	class derived3 : public derived1
	{
		int w;
		public:
			derived3(int d):derived1(d)
			{
					w=d;
			}
			void show()
			{
				derived1::show();
				cout<<endl<<"w = "<<w;			
			}
	};
	main()
	{
		cout<<endl<<"When object of derived3 class is created";
		derived3 d3(90);
		d3.show();
		cout<<endl<<"When object of derived2 class is created";
		derived2 d2(50);
		d2.show();
		getch();
	}

	
	

Destructors in Inheritance:

For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			~base()
			{
				cout<<endl<<"Destructor of Base class";			
			}
	};
	class derived1 : public base
	{
		public:
			~derived1()
			{
				cout<<endl<<"Destructor of Derived1 class";			
			}
	};
	class derived2 : public base
	{
		public:
			~derived2()
			{
				cout<<endl<<"Destructor of Derived2 class";			
			}
	};
	class derived3 : public derived1
	{
		public:
			~derived3()
			{
				cout<<endl<<"Destructor of Derived3 class";			
			}
	};
	main()
	{
		cout<<endl<<"When object of derived3 class is created";
		derived3 d3;
		cout<<endl<<"When object of derived2 class is created";
		derived2 d2;
		getch();
	}
	
	

Sequence of invoking Constructors & Destructors in Inheritance:
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			base()
			{
				cout<<endl<<"Constructor of Base class";			
			}
			~base()
			{
				cout<<endl<<"Destructor of Base class";			
			}
	};
	class derived1 : public base
	{
		public:
			derived1()
			{
				cout<<endl<<"Constructor of Derived1 class";			
			}
			~derived1()
			{
				cout<<endl<<"Destructor of Derived1 class";			
			}
	};
	class derived2 : public base
	{
		public:
			derived2()
			{
				cout<<endl<<"Constructor of Derived2 class";			
			}
			~derived2()
			{
				cout<<endl<<"Destructor of Derived2 class";			
			}
	};
	class derived3 : public derived1
	{
		public:
			derived3()
			{
				cout<<endl<<"Constructor of Derived3 class";			
			}		
			~derived3()
			{
				cout<<endl<<"Destructor of Derived3 class";			
			}
	};
	main()
	{
		cout<<endl<<"When object of derived3 class is created";
		derived3 d3;
		cout<<endl<<"When object of derived2 class is created";
		derived2 d2;
		getch();
	}
	
	

Ambiguity in Multiple Inheritance:

For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class Base1
	{
		public:
			void show()
			{
				cout<<endl<<"show() function of Base 1 Class";
			}
	};
	class Base2
	{
		public:
			void show()
			{
				cout<<endl<<"show() function of Base 2 Class";
			}
	};
	class Derived:public Base1, public Base2
	{
	};
	main()
	{
		Derived d;
		d.Base1::show();
		d.Base2::show();
		getch();
	}
	
	

Access Protection in Inheritance:
There are three ways of Access in Inheritance shown below
1. public Access Protaction
2. protected Access Protaction
3. private Access Protaction





Concept:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base 
	{
		private:
			int privdata;
		protected:
			int protdata;
		public:
			int pubdata;
			void function1()
			{
				privdata=1;	//OK
				protdata=2;	//OK
				pubdata=3;	//OK
			}		
	};
	class derived1:public base
	{
		public:
			void function2()
			{
				privdata=11;//ERROR
				protdata=22;//OK
				pubdata=33;//OK
			}		
	};
	class derived2:protected base
	{
		public:
			void function3()
			{
				privdata=111;//ERROR
				protdata=222;//OK
				pubdata=333;//OK
			}		
	};
	class derived3:private base
	{
		public:
			void function4()
			{
				privdata=1111;//ERROR
				protdata=2222;//OK
				pubdata=3333;//OK
			}		
	};
	main()
	{
		base b;
		derived1 d1;
		derived2 d2;
		derived3 d3;
		b.privdata=9;//ERROR
		b.protdata=9;//ERROR
		b.pubdata=9;//OK
		d1.privdata=8;//ERROR
		d1.protdata=8;//ERROR
		d1.pubdata=8;//OK
		d2.privdata=7;//ERROR
		d2.protdata=7;//ERROR
		d2.pubdata=7;//ERROR
		d3.privdata=6;//ERROR
		d3.protdata=6;//ERROR
		d3.pubdata=6;//ERROR
	}	
	
	


For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int r_no;
		char qual[20];
		protected:
			void getdata()
			{
				cout<<endl<<"Enter the Roll No & Qualification ";
				cin>>r_no>>qual;
			}
			void showdata()
			{
				cout<<endl<<"Roll No\t\t: "<<r_no;
				cout<<endl<<"Qualification\t: "<<qual;
			}
	};
	class employee
	{
		int e_id;
		char e_name[20];
		double e_sal;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the Employee ID, Name & Salary ";
				cin>>e_id>>e_name>>e_sal;
			}
			void showdata()
			{
				cout<<endl<<"Employee ID\t: "<<e_id;
				cout<<endl<<"Name\t\t: "<<e_name;
				cout<<endl<<"Salary\t\t: "<<e_sal;
			}
	};
	class manager : private student, private employee
	{
		char dept[20],cat[20];
		public:
			void getdata()
			{
				student::getdata();
				employee::getdata();
				cout<<endl<<"Enter the Category & Department name ";
				cin>>cat>>dept;
			}
			void showdata()
			{
				student::showdata();
				employee::showdata();
				cout<<endl<<"Category\t: "<<cat;
				cout<<endl<<"Department\t: "<<dept;
			}
	};
	class scientist : private student, private employee
	{
		char no_rp[20];
		public:
			void getdata()
			{
				student::getdata();
				employee::getdata();
				cout<<endl<<"Enter the No. of Research Papers Published ";
				cin>>no_rp;
			}
			void showdata()
			{
				student::showdata();
				employee::showdata();
				cout<<endl<<"No. of \t\t: "<<no_rp<<endl<<"Research Papers ";
			}
	};
	class laborer : protected employee
	{	
	};
	class dailywage:private laborer
	{
		double wage;
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the wage of laborer ";
				cin>>wage;		
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"Wage\t\t: "<<wage;
			}
	};
	class monthly:private laborer
	{
		double salary;
		public:
			void getdata()
			{
				employee::getdata();
				cout<<endl<<"Enter the Salary of laborer ";
				cin>>salary;		
			}
			void showdata()
			{
				employee::showdata();
				cout<<endl<<"Salary\t\t: "<<salary;
			}
	};
	main()
	{
		manager ma;
		scientist s;
		dailywage d;
		monthly m;
		cout<<endl<<endl<<"Manager";
		ma.getdata();
		cout<<endl<<endl<<"Scientist";
		s.getdata();
		cout<<endl<<endl<<"Daily Wage Laborer";
		d.getdata();
		cout<<endl<<endl<<"Monthly Laborer";
		m.getdata();
		cout<<endl<<endl<<"Manager";	
		ma.showdata();
		cout<<endl<<endl<<"Scientist";
		s.showdata();
		cout<<endl<<endl<<"Daily Wage Laborer";
		d.showdata();
		cout<<endl<<endl<<"Monthly Laborer";
		m.showdata();
		getch();
	}
	
	
Virtual Base Class
If there is a situation One base class (ie “A”) is inherited in two or more classes (ie “B” & “C”) and these two or more classes (ie “B” & “C”) are again inherited in one class (“D”). Members of class "A" which are not present in class “B”, “C” & “D” are to be accessed with the help of object of class “D”, then there will be an ambiguity how the members of class “A” are to be accessed either through class “B” or class “C” with the help of Object of class "D".
For this ambiguity we use the concept of Virtual base class.
A virtual base class is a base class that ensures only one copy of it will be present in any derived class.


For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;

	class A
	{
		public:
			void show()
			{
				cout<<endl<<"show() function of class A";
			}
	};
	class B:public virtual A
	{	
	};
	class C:public virtual A
	{	
	};
	class D:public B, public C
	{
	};
	main()
	{
		D obj;
		obj.show();
	}
	
	
Arrays in C++:
go for Array in C language(Click here)
Arrays of Objects:
A Collection of Objects (Homogeneous) is known as Array of objects.
For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float m;
		char n[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the roll number, name and marks ";
				cin>>rn>>n>>m;
			}
			void showdata()
			{
				cout<<endl<<"Roll number\t: "<<rn;
				cout<<endl<<"Name\t\t: "<<n;
				cout<<endl<<"Marks\t\t: "<<m;
			}
	};
	main()
	{
		int i;
		student s[2];//Array of objects
		for(i=0;i<2;i++)
		{
			cout<<endl<<"Student "<<(i+1);
			s[i].getdata();
		}
		for(i=0;i<2;i++)
		{
			cout<<endl<<"Student "<<(i+1);
			s[i].showdata();
		}
	}

	
	
Nested Classes:
• If a class is defined inside another class, then this concept is known as nesting of classes.
• If an object of any class is created inside of another class, then this concept is also known as nesting of classes.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class outer 
	{
	   public:
			class inner 
			{
				private:
					int x;
				public:
					void getdata() 
					{
						cout<<endl<<"Enter the value of x ";
						cin>>x;
					}
					void showdata() 
					{
						cout<<endl<<"value of x is "<<x;
					}
			};
	};
	main() 
	{
	   outer::inner obj;
	   obj.getdata();
	   obj.showdata();
	   getch();
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class outer 
	{
			class inner 
			{
				private:
					int x;
				public:
					void getdata() 
					{
						cout<<endl<<"Enter the value of x ";
						cin>>x;
					}
					void showdata() 
					{
						cout<<endl<<"value of x is "<<x;
					}
			};
	   public:
			inner i;
	};
	main() 
	{
	   outer obj;
	   obj.i.getdata();
	   obj.i.showdata();
	   getch();
	}
	
	
Example 3:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class outer 
	{
			private:
					int x;
					class inner 
					{
						private:
							int y;
						public:
							void getdata() 
							{
								cout<<endl<<"Enter the value of y ";
								cin>>y;
							}
							void showdata() 
							{
								cout<<endl<<"value of y is "<<y;
							}
					};
	   public:
			inner i;
			void getdata() 
			{
				cout<<endl<<"Enter the value of x ";
				cin>>x;
			}
			void showdata() 
			{
				cout<<endl<<"value of x is "<<x;
			}
	};
	main() 
	{
	   outer obj;
	   obj.getdata();
	   obj.showdata();
	   obj.i.getdata();
	   obj.i.showdata();
	   getch();
	}
	
	
Example 4:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class inner 
	{
		private:
			int y;
		public:
			void getdata() 
			{
				cout<<endl<<"Enter the value of y ";
				cin>>y;
			}
			void showdata() 
			{
				cout<<endl<<"value of y is "<<y;
			}
	};
	class outer 
	{
		private:
			int x;
		public:
			inner i;
			void getdata() 
			{
				cout<<endl<<"Enter the value of x ";
				cin>>x;
			}
			void showdata() 
			{
				cout<<endl<<"value of x is "<<x;
			}
	};
	main() 
	{
	   outer obj;
	   obj.getdata();
	   obj.showdata();
	   obj.i.getdata();
	   obj.i.showdata();
	   getch();
	}
	
	
Example 5:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class inner 
	{
		public:
			int y;
	};
	class outer 
	{
		private:
			int x;
		public:
			inner i;
			void getdata() 
			{
				cout<<endl<<"Enter the value of x & y ";
				cin>>x>>i.y;
			}
			void showdata() 
			{
				cout<<endl<<"value of x is "<<x;
				cout<<endl<<"value of y is "<<i.y;
			}
	};
	main() 
	{
	   outer obj;
	   obj.getdata();
	   obj.showdata();
	   getch();
	}
	
	
Pointers in C++:
go for Pointers in C language(Click here)
Pointer to Object/Class:
A Pointer that contains the address of object of same class to which it is declared as Pointer.
For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		private:
			int rn;
			float m;
			char n[10];
		public:
			void getdata()
			{
				cout<<"\nEnter your rn, name and marks ";
				cin>>rn>>n>>m;
			}
			void showdata()
			{
				cout<<"\nyour rn is "<<rn;
				cout<<"\nyour name is "<<n;
				cout<<"\nyour marks is "<<m;
			}
	};
	int main()
	{
		student s,*ptr;
		s.getdata();
		ptr=&s;
		ptr->showdata();
	}
	
	
Dynamic Memory Allocation in C++:
Pointer Operators (new & delete keywords):
new keyword:

This operator is used to create a space for any variable dynamically.
Syntax 1:
Datatype *pointer;
pointer = new Datatype[size];
For Example:
int *ptr;
ptr = new int[5];
Syntax 2:
Datatype *pointer = new Datatype[size];
For Example:
int *ptr = new int[5];
delete keyword:

This operator is used to free a dynamically allocated space by new operator.
Syntax:
delete pointer;
For Example:
delete ptr;

Example 1:
	
	#include<iostream>
	#include<conio.h>
	#include<string.h>
	using namespace std;
	main()
	{
		char str[20],*ptr;
		int l;
		cout<<endl<<"Enter any string ";
		cin>>str;
		l=strlen(str);
		ptr=new char[l+1];
		cout<<endl<<"Before copy";
		cout<<endl<<"str = "<<str;
		cout<<endl<<"ptr = "<<ptr;	
		strcpy(ptr,str);
		cout<<endl<<"After copy";
		cout<<endl<<"str = "<<str;
		cout<<endl<<"ptr = "<<ptr;	
		delete ptr;	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	#include<string.h>
	using namespace std;
	class demoptr
	{
		public:
			char *ptr;
			demoptr(int l)
			{
				ptr=new char[l+1];
			}
			~demoptr()
			{
				delete ptr;
			}		
	};
	main()
	{
		char str[20];
		int l;
		cout<<endl<<"Enter any string ";
		cin>>str;
		l=strlen(str);
		demoptr obj(l);
		cout<<endl<<"Before copy";
		cout<<endl<<"str = "<<str;
		cout<<endl<<"ptr = "<<obj.ptr;	
		strcpy(obj.ptr,str);
		cout<<endl<<"After copy";
		cout<<endl<<"str = "<<str;
		cout<<endl<<"ptr = "<<obj.ptr;		
	}
	
	
The this Pointer (this keyword):
It is a magic pointer which is used to point the object by which a function is being called. If you put it, it is well if not it is put itself.
How to use it:
this->variable_name;

For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demothis
	{
		private:
			int x;
		public:
			demothis()
			{
				this->x=55;
			}
			void showdata()
			{
				cout<<"\nValue of x "<<this->thix;
			}
	};	
	int main()
	{
		demothis d,d1;
		d.showdata();	
		d1.showdata();	
	}
	
	
inline Function:
• A function that sends it’s body to the line where it is called is known as inline function.
• An inline keyword is preceded by function prototype defined inside the class.
• It makes the program execution faster.
• If a definition of any function is defined inside the body of class, by default it becomes inline.
• Functions with small definition must be declared as inline.
• Functions with longer definition must not be declared as inline.
• Definitions of longer functions must not be defined inside the body of class.
• Definitions of small functions may be defined inside the body of class.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demoinline
	{
		private:
			int x;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the value of x ";
				cin>>x;
			}
			void showdata()
			{
				cout<<"\nValue of x "<<x;
			}
	};	
	int main()
	{
		demoinline obj;
		obj.getdata();	
		obj.showdata();	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demoinline
	{
		private:
			int x;
		public:
			inline void getdata();
			inline void showdata();
	};	
	void demoinline::getdata()
	{
		cout<<endl<<"Enter the value of x ";
		cin>>x;
	}
	void demoinline::showdata()
	{
		cout<<"\nValue of x "<<x;
	}
	int main()
	{
		demoinline obj;
		obj.getdata();	
		obj.showdata();	
	}
	
	
friend Function:
Till now we have read that a non-member function can never access the private members of any class but now it is possible to access the private members of any class by the non-members with the help of the concept of friend function.
• A friend keyword is preceded by function prototype defined inside the class.
• A friend function is a non-member function.
• A friend function must be declared inside the class to which it is made as a friend.
• At least one object of the class, to which it is declared as a friend, must be passed to the function as an argument.
• Since it is a non-member function, there is no need to put class-name and scope resolution operator with function prototype defined inside the class.
• Since it is a non-member function, there is no need to have an object to call a friend function.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class demofriend
	{
		private:
			int x;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the value of x ";
				cin>>x;
			}
			friend void showdata(demofriend d);
	};	
	void showdata(demofriend d)
	{
		cout<<"\nValue of x "<<d.x;
	}
	int main()
	{
		demofriend obj;
		obj.getdata();	
		showdata(obj);	
	}
	
	
friend function can be used as a bridge between two or more classes

For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class B;
	class A
	{
		int x;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the value of x ";
				cin>>x;
			}
			friend void showadd(A,B);
	};
	class B
	{
		int y;
		public:
			void getdata()
			{
				cout<<endl<<"Enter the value of y ";
				cin>>y;
			}
			friend void showadd(A,B);
	};
	void showadd(A obj1,B obj2)
	{
		int z;
		z=obj1.x+obj2.y;
		cout<<endl<<"Sum is "<<z;
	}
	main()
	{
		A ob1;
		B ob2;
		ob1.getdata();
		ob2.getdata();
		showadd(ob1,ob2);
		getch();
	}

	
virtual Function:
A function that is defined inside the base class and is redefined in the derived classes, is known as virtual function.
• A virtual keyword is preceded by function prototype defined inside the base class.
• Prototypes of all virtual functions (defined and redefined) must be same.
• Virtual functions supports the concepts of Polymorphism (run-time polymorphism)
• Virtual functions can be accessed with the help of base class pointers.
For Example :
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			virtual void vfun()
			{
				cout<<endl<<"Virtual function of base class";
			}
	};
	class derived:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived class";
			}
	};
	main()
	{
		base b, *ptr;
		derived d;
		ptr=&b;
		ptr->vfun();//base class
		ptr=&d;
		ptr->vfun();//derived class
		getch();	
	}
	
	
Virtual functions are hierarchical

For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			virtual void vfun()
			{
				cout<<endl<<"Virtual function of base class";
			}
	};
	class derived1:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived 1 class";
			}
	};
	class derived2:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived 2 class";
			}
	};
	main()
	{
		base b, *ptr;
		derived1 d1;
		derived2 d2;
		ptr=&b;
		ptr->vfun();//base class
		ptr=&d1;
		ptr->vfun();//derived1 class
		ptr=&d2;
		ptr->vfun();//derived2 class
		getch();	
	}

	
Virtual attribute is inherited:

Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			virtual void vfun()
			{
				cout<<endl<<"Virtual function of base class";
			}
	};
	class derived1:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived 1 class";
			}
	};
	class derived2:public derived1
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived 2 class";
			}
	};
	main()
	{
		base b, *ptr;
		derived1 d1;
		derived2 d2;
		ptr=&b;
		ptr->vfun();//base class
		ptr=&d1;
		ptr->vfun();//derived1 class
		ptr=&d2;
		ptr->vfun();//derived2 class
		getch();	
	}

	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			virtual void vfun()
			{
				cout<<endl<<"Virtual function of base class";
			}
	};
	class derived1:public base
	{
	};
	class derived2:public derived1
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived 2 class";
			}
	};
	main()
	{
		base b, *ptr;
		derived1 d1;
		derived2 d2;
		ptr=&b;
		ptr->vfun();//base class
		ptr=&d1;
		ptr->vfun();//base class
		ptr=&d2;
		ptr->vfun();//derived2 class
		getch();	
	}

	
Pure Virtual function:
A virtual fnction that has no body in the base class, is known as Pure Virtual function. This virtual function is defined inside its derived classes.
Syntax:
virtual return_type function_name(arguments)=0;
For Example:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base
	{
		public:
			virtual void vfun()=0;		//Pure Virtual function
	};
	class derived:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived class";
			}
	};
	main()
	{
		base *ptr;
		derived d;
		ptr=&d;
		ptr->vfun();//derived class
		getch();	
	}

	
Abstract Class:
A class that contains atleast one pure virtual function, is known as Abstract Class.
Since Abstract class contains one or more Pure Virtual functions (ie functions without body), so object of an Abstract classes can not be created.
Example 1:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base			//Abstract class
	{
		public:
			virtual void vfun()=0;		//Pure Virtual function
	};
	class derived:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived class";
			}
	};
	main()
	{
		base *ptr;
		derived d;
		ptr=&d;
		ptr->vfun();//derived class
		getch();	
	}
	
	
Example 2:
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class base			//Abstract class
	{
		public:
			virtual void vfun()=0;		//Pure Virtual function
			void show()
			{
				cout<<endl<<"I am in Show() function of Base class";
			}
	};
	class derived:public base
	{
		public:
			void vfun()
			{
				cout<<endl<<"Virtual function of derived class";
			}
	};
	main()
	{
		base *ptr;
		derived d;
		ptr=&d;
		ptr->vfun();//derived class
		d.show();
		getch();	
	}
	
	
File Management System (File Handling):
File is a collection of records.
Record is a collection of data.
Data is an information of an object.
Operations on File
1. Create
2. Open
3. Write
4. Read
5. Close
C++ FILES AND STREAMS
So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. But now we will learn how to read and write into and from a file. This requires another standard C++ library called fstream, which defines three new data types:
Data Type Description:
ofstream This data type represents the output file stream and is used to create files and to write information to files.
ifstream This data type represents the input file stream and is used to read information from files.
fstream This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
To perform file processing in C++, header files iostream.h and fstream.h must be included in your C++ source file.


Opening a File:
A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
void open(char *filename, ios::openmode);
Here, the first argument specifies the name and location of the file to be opened and the second argument defines the mode in which the file should be opened.
Mode Description
ios::app Append mode. All output to that file to be appended to the end.
ios::in Open a file for reading.
ios::out Open a file for writing.
You can combine two or more of these values by ORing them together.
For example :
you can open a file for reading and writing purpose as follows:
fstream f;
f.open("file.txt", ios::out | ios::in );
Closing a File:
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
void close();
Writing to a File:
While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.
Reading from a File:
You read information from a file into your prog ram using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
File Position Pointers:
seekg() & seekp() functions:
Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg() for ifstream and seekp() for ofstream.
The first argument to seekg() and seekp() normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beg inning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.
tellg() & tellp() functions:
Both istream and ostream provide member functions for telling the current position of file-position pointer in file. These member functions are tellg() for ifstream and tellp() for ofstream.
There is no argument for tellg() and tellp() functions but both the functions return a long integer.
Example 1:
A Program to write a line into a file.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	main()
	{
		ofstream obj("fileout.txt");
		obj<<"Welcome Students to BIIT";
		cout<<endl<<"Data is written";
		getch();
		obj.close();
	}
	
	
Example 2:
A Program to read a word from a file.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	main()
	{
		ifstream obj("fileout.txt");
		char line[80];
		obj>>line;
		cout<<endl<<line;
		obj.close();
	}
	
	
Example 3:
A Program to read a line from a file.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	main()
	{
		ifstream obj("fileout.txt");
		char line[80];
		obj.getline(line,80);
		cout<<endl<<line;
		obj.close();
	}
	
	
Example 4:
A Program to write multiple lines into a file.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	main()
	{
		ofstream obj;
		obj.open("fileout.txt");
		char str[80],choice;
		do
		{
			cout<<"Enter the line "<<endl;
			cin.getline(str,80);
			obj<<str<<endl;		
			cout<<"Do you want more ";
			cin>>choice;
			cin.ignore();		
		}while(choice=='y'||choice=='Y');
		cout<<endl<<"Data is written";
		getch();
		obj.close();
	}
	
	
Example 5:
A Program to read multiple lines from a file.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	main()
	{
		ifstream obj;
		obj.open("fileout.txt");
		char line[80];
		do
		{
			obj.getline(line,80);
			cout<<endl<<line;
		}while(!obj.eof());
		getch();
		obj.close();
	}
	
	
Example 6:
A Program to write a record into file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the rn, name & marks of a student ";
				cin>>rn>>name>>marks;
			}
	};
	main()
	{
		ofstream obj;
		student s;
		obj.open("fileout.txt");
		s.getdata();
		obj.write((char*)&s,sizeof(s));
		cout<<"Record is written";
		getch();
		obj.close();
	}
	
	
Example 7:
A Program to read a record from file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void showdata()
			{
				cout<<endl<<"Roll number = "<<rn;
				cout<<endl<<"Name\t= "<<name;
				cout<<endl<<"Marks\t= "<<marks;
			}
	};
	main()
	{
		ifstream obj;
		student s;
		obj.open("fileout.txt");
		obj.read((char*)&s,sizeof(s));
		s.showdata();
		getch();
		obj.close();
	}
	
	
Example 8:
A Program to write multiple records into file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the rn, name & marks of a student ";
				cin>>rn>>name>>marks;
			}
	};
	main()
	{
		ofstream obj;
		student s;
		char choice;
		obj.open("fileout.txt");
		do
		{
			s.getdata();
			obj.write((char*)&s,sizeof(s));
			cout<<"Do you want more ";
			cin>>choice;
		}while(choice=='y'||choice=='Y');
		getch();
		obj.close();
	}
	
	
Example 9:
A Program to read multiple records from file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void showdata()
			{
				cout<<endl<<"Roll number = "<<rn;
				cout<<endl<<"Name\t= "<<name;
				cout<<endl<<"Marks\t= "<<marks;
			}
	};
	main()
	{
		ifstream obj;
		student s;
		obj.open("fileout.txt");
		obj.read((char*)&s,sizeof(s));		
		do
		{
			s.showdata();
			obj.read((char*)&s,sizeof(s));
		}while(!obj.eof());
		getch();
		obj.close();
	}
	
	
Example 10:
A Program to read and write a record into file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the rn, name & marks of a student ";
				cin>>rn>>name>>marks;
			}
			void showdata()
			{
				cout<<endl<<"Roll number = "<<rn;
				cout<<endl<<"Name\t= "<<name;
				cout<<endl<<"Marks\t= "<<marks;
			}
	};
	main()
	{
		ofstream obj;
		student s1,s2;
		obj.open("fileout.txt");
		s1.getdata();
		obj.write((char*)&s1,sizeof(s1));
		obj.close();
		ifstream ob;
		ob.open("fileout.txt");
		ob.read((char*)&s2,sizeof(s2));		
		s2.showdata();
		getch();
		ob.close();
	}
	
	
Example 11:
Usage of File position pointers
A Program to read and write a record into file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	class student
	{
		int rn;
		float marks;
		char name[20];
		public:
			void getdata()
			{
				cout<<endl<<"Enter the rn, name & marks of a student ";
				cin>>rn>>name>>marks;
			}
			void showdata()
			{
				cout<<endl<<"Roll number = "<<rn;
				cout<<endl<<"Name\t= "<<name;
				cout<<endl<<"Marks\t= "<<marks;
			}
	};
	main()
	{
		fstream obj;
		student s1,s2;
		obj.open("fileout.txt",ios::out|ios::in);
		s1.getdata();
		obj.write((char*)&s1,sizeof(s1));
		obj.seekg(0);
		obj.read((char*)&s2,sizeof(s2));		
		s2.showdata();
		getch();
		obj.close();
	}
	
	
Example 12:
A Program to read desired record from file
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
class student
{
	int rn;
	float marks;
	char name[20];
	public:
		void showdata()
		{
			cout<<endl<<"Roll number = "<<rn;
			cout<<endl<<"Name\t= "<<name;
			cout<<endl<<"Marks\t= "<<marks;
		}
};
main()
{
	ifstream obj;
	student s;
	int num,loc;
	obj.open("fileout.txt");
	//From Begining
	cout<<endl<<"Current Position : "<<obj.tellg();
	cout<<endl<<"Enter the record number (from Begining) ";
	cin>>num;
	loc=(num-1)*sizeof(s);
	obj.seekg(loc,ios::beg);
	cout<<endl<<"Current Position : "<<obj.tellg();
	obj.read((char*)&s,sizeof(s));		
	s.showdata();
	cout<<endl<<"Current Position : "<<obj.tellg();
	
	//From End
	cout<<endl<<"Enter the record number (from End) ";
	cin>>num;
	loc=-1*(num)*sizeof(s);
	obj.seekg(loc,ios::end);
	cout<<endl<<"Current Position : "<<obj.tellg();
	obj.read((char*)&s,sizeof(s));		
	s.showdata();
	cout<<endl<<"Current Position : "<<obj.tellg();
	
	//From Current Position in forward direction
	cout<<endl<<"Enter the record number (from Current Position in Forward direction) ";
	cin>>num;
	loc=(num-1)*sizeof(s);
	obj.seekg(loc,ios::cur);
	cout<<endl<<"Current Position : "<<obj.tellg();
	obj.read((char*)&s,sizeof(s));		
	s.showdata();
	cout<<endl<<"Current Position : "<<obj.tellg();
	
	//From Current Position in backward direction
	cout<<endl<<"Enter the record number (from Current Position in Backward direction) ";
	cin>>num;
	loc=-1*(num+1)*sizeof(s);
	obj.seekg(loc,ios::cur);
	cout<<endl<<"Current Position : "<<obj.tellg();
	obj.read((char*)&s,sizeof(s));		
	s.showdata();	
	cout<<endl<<"Current Position : "<<obj.tellg();
	getch();
	obj.close();
}
	
	
Namespace:
Namespaces are used in recent addition to C++. These are used to localize the names of identifiers to avoid collisions. The C++ programming environment has seen an explosion of variable, function, and class names. Prior to the invention of namespaces, all of these names competed for slots in the global namespace and many conflicts arose. For example: if a function strlen() is defined, it could override the standard library function strlen() because both names would be stored in the global namespace.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	namespace a
	{
		class demo
		{
			public:
				void show()
				{
					cout<<"\nIn the namespace A";
				}
		};
	}
	namespace b
	{
		class demo
		{
			public:
				void show()
				{
					cout<<"\nIn the namespace B";
				}
		};
	}
	using namespace a;
	using namespace b;
	main()
	{
		a::demo d;
		b::demo d1;
		d.show();
		d1.show();
		getch();
	}
	
	
Templates:
The template is one of the C++’s most sophisticated and high-powered features. Using templates, it is possible to create generic functions and classes. In generic function or class, the type of data upon which the function or class operates is specified as a parameter. Thus, you can use one function or class with several different types of data without having to explicitly recode specific versions for each data type.
Generic Functions:
A generic function defines a general set of operations that will be applied to various types of data. The type of data that the function will operate upon is passed to it as a parameter. Through a generic function, a general procedure can be applied to a wide range of data. For example, the generic bubble sort function can be applied to the integer, float as well as character data. Generic functions can be created with the help of templates.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	template <class dtype>
	void show(dtype a) //Generic Function
	{
		cout<<endl<<a;
	}
	main()
	{
		int i=44;
		float j=45.5;
		char k='a';
		cout<<"\nInteger";
		show(i);
		cout<<"\nFloat";
		show(j);
		cout<<"\nCharacter";
		show(k);
		getch();
	}
	
	
Generic Classes:
In addition to generic functions, generic classes can also be defined. When you do this, you create a class that defines all the algorithms used by that class. however, the actual type of the data being manipulated will be specified as a parameter when objects of that class are created.
Generic classes are useful when a class uses logic that can be generalized. For example, the same algorithms that maintain a queue of integers will also work for the queue of characters. Generic classes can also be created with the help of templates.
	
	#include<iostream>
	#include<conio.h>
	using namespace std;
	template &tl;class ctyp>
	class demo          //Generic Class
	{
		public:
			void show(ctyp p)
			{
				cout<<endl<<p;
			}
	};
	main()
	{
		demo<char> d;
		cout<<"\nCharacter";
		d.show('a');
		demo<int> d1;
		cout<<"\nInteger";
		d1.show(11);
		demo<float> d2;
		cout<<"\nFloat";
		d2.show(22.33);
		getch();
	}