Single level Inheritance

When a class inherits from a single base class, it is known as single inheritance. It is the most simple form of inheritance.

single level inheritance
single level inheritance

While inheriting data from base class , derived class may use any one of the visibility modes to specify the data access level. It may public derivation, private derivation or protected derivation.

Example:

Class ABC : private XYZ    // private derivation of Class XYZ 
{
        int no;
public:
        void get ( ) ;
        void show ( ) ;
};

In above code, Class ABC is inheriting data from class XYZ using private derivation. Only one Base class and one Derived class is involved in single level inheritance.

Example:

#include<iostream.h>
#include<conio.h>
class worker
{
		int age;
		char name[10];
	public:
		void get()
		{
		cout <<”yout name please”
		cin >> name;
		cout <<”your age please” ;
		cin >> age;
		}
		void show()
		{
		cout <<”In My name is :”<<name<<”In My age is :”<<age;
		}
};
class manager :: public worker 	//derived class (publicly)
{
	int now;
	public:
		void get() 
		{
		worker :: get(); //the calling of base class fn.
		cout << “number of workers under you”;
		cin >> now;
		cin>>name>>age;
		} 
		void show()
		{
		worker :: show(); //calling of base class o/p fn.
		cout <<“in No. of workers under me are: “ << now; 
		}	
};

void main()
{
	clrscr () ;
	worker W1;
	manager M1;
	M1.get( );
	M1.show( );
}

Output:

Your name please
Ravinder
Your age please
27
number of workers under you
30

My name is : Ravinder
My age is : 27
No. of workers under me are : 30

Related posts