/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Inheritance in c++?


When be creating a new class from a existing class is called inheritance. existing class is callesd Base class.base class is also knows as Parent class.or A new class is was creating is called Derived class.this class is also knows as child class.

Type of Inheritance.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

syntax :

class < derived_class_name> : < access-specifier> < base_class_name>
{
    //body
}
Q1.) Example of Inheritance .
    
    
#include< iostream>
using namespace std;

class Person
{
	int id;
	char name[100];

	public:
		void set_p()
		{
			cout<<"Enter  Id:";
			cin>>id;
			fflush(stdin);
			cout<<"Enter  Name:";
			cin.get(name,100);
		}

		void display_p()
		{
			cout<< endl<< id<<"\t"<< name<<"\t";
		}
};

class Student: private Person
{
	char course[50];
	int fee;
	
	public:
	void set_s()
		{
			set_p();
			cout<<"Enter Course Name:";
			fflush(stdin);
			cin.getline(course,50);
			cout<<"Enter  Course Fee:";
			cin>>fee;
		}
		
		void display_s()
		{
			display_p();
			cout<< course<<"\t"<< fee<< endl;
		}
};

main()
{
	Student s;
	s.set_s();
	s.display_s();
	return 0;
}


Output
Enter Id: 32
Enter Name: Samant kumar
Enter Course Name: C++
Enter Course Fee:3000