codescracker


c++

C++ Inheritance



« Previous Tutorial Next Tutorial »


Inheritance is the capability of one class to inherit the properties from another class. The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Already tested and saved previous code can be reused.

Reusing existing code saves times, money, and efforts also, and increases program's reliability. Without redefining the old class, you can add new properties to the derived class, and you even can redefine an inherited class member functions.

Need for Inheritance

Inheritance is a splendid concept of object-oriented languages. Here are several reasons why inheritance was introduced:

Different Forms of Inheritance

Inheritance may take place in many forms which are :

Visibility Modes

Here, the following table specifies the role of visibility-modes:

Visibility Mode is Inheritable public member becomes (in derived class) Inheritable protected member becomes (in derived class) Private Member of base class are not directly accessible to derived class
public public protected
protected protected protected
private private private

Inheritance and Access Control

When a class inherits some members from another class, there must be a way to control the access of inherited members. As you know that we can derive a class publicly or privately. Let's take a look at these.

Access Control in Publicly Derived Class

Here is an example program illustrating the access control in public derivation of a class

/* C++ Inheritance - Example Program */

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class EMPLOYEE
{
	private:
		char name[30];
		unsigned long enumb;
	public:
		void getdata()
		{
			cout<<"Enter name: ";
			gets(name);
			cout<<"Enter Employee Number: ";
			cin>>enumb;
		}
		void putdata()
		{
			cout<<"Name: "<<name<<"\t";
			cout<<"Emp. No: "<<enumb<<"\t";
			cout<<"Basic Salary: "<<basic;
		}
	protected:
		float basic;
		void getbasic()
		{
			cout<<"Enter Basic: ";
			cin>>basic;
		}
};
class MANAGER:public EMPLOYEE
{
	private:
		char title[30];
	public:
		void getdata()
		{
			EMPLOYEE::getdata();
			getbasic();
			cout<<"Enter Title: ";
			gets(title);
		}
		void putdata()
		{
			EMPLOYEE::putdata();
			cout<<"\tTitle: "<<title<<"\n";
		}
};
void main()
{
	clrscr();
	MANAGER m1, m2;
	cout<<"Manager 1\n";
	m1.getdata();
	cout<<"\nManager 2\n";
	m2.getdata();
	cout<<"\n\t\tManager 1 Details\n";
	m1.putdata();
	cout<<"\n\t\tManager 2 Details\n";
	m2.putdata();
	getch();
}

Here is the sample run of the above C++ program:

c++ inheritance

Access Control in Privately Derived Class

Here is an example program illustrating access control of inherited members in the privately derived class

/* C++ Inheritance - Example Program */

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class EMPLOYEE
{
	private:
		char name[30];
		unsigned long enumb;
	public:
		void getdata()
		{
			cout<<"Enter name: ";
			gets(name);
			cout<<"Enter Employee Number: ";
			cin>>enumb;
		}
		void putdata()
		{
			cout<<"Name: "<<name<<"\t";
			cout<<"Emp. No: "<<enumb<<"\t";
			cout<<"Basic Salary: "<<basic;
		}
	protected:
		float basic;
		void getbasic()
		{
			cout<<"Enter Basic: ";
			cin>>basic;
		}
};
class MANAGER:private EMPLOYEE
{
	private:
		char title[30];
	public:
		void getdata()
		{
			EMPLOYEE::getdata();
			getbasic();
			cout<<"Enter Title: ";
			gets(title);
		}
		void putdata()
		{
			EMPLOYEE::putdata();
			cout<<"\tTitle: "<<title<<"\n";
		}
};
void main()
{
	clrscr();
	MANAGER mng1, mng2;
	cout<<"Manager 1\n";
	mng1.getdata();
	cout<<"\nManager 2\n";
	mng2.getdata();
	cout<<"\n\t\tManager 1 Details\n";
	mng1.putdata();
	cout<<"\n\t\tManager 2 Details\n";
	mng2.putdata();
	getch();
}

Here is the sample output of this C++ program:

inheritance in c++

The derived class inherits all the members of the base class. However, the derived class has the direct access privilege only to the non-private members of the base class. Let's take an example program.

/* C++ Inheritance - Example Program */

#include<iostream.h>
#include<conio.h>
class BASE
{
	int a;
	public:
		int b;
		void getdata()
		{
			cin>>a;
		}
		void putdata()
		{
			cout<<a;
		}
};
class DERIVED:public BASE
{
	int p;
	public:
		int q;
};
void main()
{
	clrscr();
	cout<<"Size of BASE Class: "<<sizeof(BASE)<<"\n";
	cout<<"Size of DERIVED Class: "<<sizeof(DERIVED)<<"\n";
	BASE bobj;
	DERIVED dobj;
	cout<<"\nSize of BASE Class Object: "<<sizeof(bobj)<<"\n";
	cout<<"Size of DERIVED Class Object: "<<sizeof(dobj);
	getch();
}

Below is the sample output of the above C++ program:

c++ inheritance example

Constructors in Multiple Inheritance

Here is an example program illustrate the working of constructors and destructors in multiple inheritance

/* C++ Inheritance - Example Program */

#include<iostream.h>
#include<conio.h>
class BASE1
{
	protected:
		int a;
	public:
		BASE1(int x)
		{
			a=x;
			cout<<"Constructing BASE1\n";
		}
		~BASE1()
		{
			cout<<"Destructing BASE1\n";
		}
};
class BASE2
{
	protected:
		int b;
	public:
		BASE2(int y)
		{
			b=y;
			cout<<"Constructing BASE2\n";
		}
		~BASE2()
		{
			cout<<"Destructing BASE2\n";
		}
};
class DERIVED:public BASE2, public BASE1
{
	int c;
	public:
		DERIVED(int i, int j, int k):BASE2(i),BASE1(j)
		{
			c=k;
			cout<<"Constructing DERIVED\n";
		}
		~DERIVED()
		{
			cout<<"Destructing DERIVED\n";
		}
		void show()
		{
			cout<<"1."<<a<<"\t2."<<b<<"\t3."<<c<<"\n";
		}
};
void main()
{
	clrscr();
	DERIVED obj(10,11,12);
	obj.show();
	getch();
}

Here is the sample output of the above C++ program:

c++ inheritance program

Virtual Base Classes

Below is an example program to show the working of virtual base classes

/* C++ Inheritance - Example Program */

#include<iostream.h>
#include<conio.h>
class BASE
{
	public:
		int a;
};
class DERIVED1:virtual public BASE
{
	public:
		int b;
};
class DERIVED2:virtual public BASE
{
	public:
		int c;
};
class DERIVED3:public DERIVED1, public DERIVED2
{
	public:
		int total;
};
void main()
{
	clrscr();
	DERIVED3 obj;
	obj.a=15;
	obj.b=20;
	obj.c=25;
	obj.total=obj.a+obj.b+obj.c;
	cout<<obj.a<<"\t"<<obj.b<<"\t"<<obj.c<<"\t"<<obj.total<<"\n";
	getch();
}

Here is the sample run of this C++ program:

inheritance

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test