Inheritance
Dept. of Computer Science & Engineering
1
Introducing Inheritance
Two concept needed to understand –
Base class – class from which the features are being inherited
Derived class – class which is inheriting the features.
Base class defines all the variable and functions that will
be common to all the derived classes. It can also have other
members which belongs to only the base class.
Derived class inherits those common variables and
functions. It can also have other members which belongs to
only that derived class.
Syntax of using general inheritance –
class derived_class_name : access_specifier base_class_name {
// body }
2 Department of CSE, CUET
Access Specifiers
There are three specifiers in C++
public – public of base, becomes public of derived
private – public of base, becomes private of derived
protected – equivalent to private, but different in the sense that,
protected members are accessible only to the derived classes of
base, not others.
Private members of base remain private to base.
When the specifier is private, the private members are
accessible through the member of that derived class.
Protected members adopt the role specified by the access
specifier
If public, they become protected
If private, they become private
If protected, public & protected member become protected
3 Department of CSE, CUET
Inherited private
# include <iostream >
using namespace std; int main ()
class base {
derived ob;
{
base base_ob ;
int x; base_ob.setx (1);
public : base_ob.showx();
void setx (int n) { x = n; } [Link] (10) ; // access member of base
void showx () { cout << x << '\ class
n‘};}; [Link] (20) ; // access member of
derived class
// Inherit as public . [Link] (); // access member of base
class derived : private base class
{ [Link] (); // access member of
int y; derived class
return 0;
public :
}
void sety (int n) { y = n; }
void showy () { cout << y << '\
n'; }};
4 Department of CSE, CUET
Inherited public
# include <iostream >
using namespace std; int main ()
class base {
{ derived ob;
[Link] (10) ; // access member of base
int x;
class
public : [Link] (20) ; // access member of
void setx (int n) { x = n; } derived class
void showx () { cout << x << '\n'; } [Link] (); // access member of base
}; class
// Inherit as public . [Link] (); // access member of
derived class
class derived : public base
return 0;
{ }
int y;
public :
void sety (int n) { y = n; }
void showy () { cout << y << '\n'; }
};
5 Department of CSE, CUET
Inherited protrected
# include <iostream >
using namespace std; // this function has access to a and b from
class base base
void showabc () { cout << a << ' ' << b
{
<< ' ' << c << '\n'; }
protected : // private to base };
int a, b; // but still accessible by
derived int main ()
public : {
void setab (int n, int m) { a = n; b derived ob;
= m; } [Link] (1, 2); // setab () is not accessible
here .
}; [Link] (3) ;
class derived : protected base // inherit [Link] ();
as protected return 0;
{ }
int c;
public :
void setc (int n) { c = n; }
6 Department of CSE, CUET
Inherited protected
# include <iostream > // this function has access to a and b from base
using namespace std; void showbc () { cout << a << ' ' << b << '
class base ' << c << '\n'; }
};
{ int main ()
protected : // private to base {
int a, b; // but still accessible by derived ob;
derived ob.b = 99;
public : [Link] (1, 2);
[Link] (3) ;
void setab (int n, int m) { a = n; b
[Link] ();
= m; }
return 0;
}; }
class derived : public base
{
int c;
public :
void setc (int n) { c = n; }
7 Department of CSE, CUET
Constructors, Destructors & Inheritance
As at least two classes involved in Inheritance, there is a
specific order, by which the constructors and destructors
are called.
In case of constructor, constructor of base class is executed
first, then the constructor of derived class comes into
execution.
For destructor, the flow is reversed. That means, destructor
of derived is executed first, then base class’s destructor
executes.
It is also possible to pass argument to derived or base or
both at the same time. Syntax is –
derived_constructor(parameter_list) : base(parameter_list) {
// body }
8 Department of CSE, CUET
Constructors, Destructors & Inheritance
# include <iostream > derived (int n, int m) : base (m) { cout <<
using namespace std; "Constructing derived class \n"; j = n; }
class base ~ derived () { cout << "Destructing
derived class \n"; }
{ void showj () { cout << j << '\n'; }
int i; };
public : int main ()
base (int m) { cout << {
"Constructing base class \n"; i = m; } derived o (10, 20) ;
[Link] ();
~ base () { cout << "Destructing
[Link] ();
base class \n"; }
return 0;
void showi () { cout << i << '\n'; } }
};
class derived : public base
{
int j;
public :
9 Department of CSE, CUET
Multiple Inheritance
There are two ways that a derived class can be inherited
from more than one base class.
First method is to use a derived class as base class in the
middle, creating a multilevel hierarchy. Ex. A -> B -> C
Second method is that derived class can directly inherit
more than one class. Ex. A, B -> C
In case of first method, constructor and destructor follows
the rule stated previously.
In case of second method, the syntax is –
class derived_classname : access base1, access base2, …,
access baseN {
// body }
10 Department of CSE, CUET
Inherited Multiple
# include <iostream > D1(int x, int y) : B1(y) { cout <<
using namespace std; "Constructing D1\n"; b = x; }
class B1 ~D1 () { cout << "Destructing D1\n"; }
int getb () { return b; }
{
};
int a;
// Inherit a derived class and an indirect base .
public : class D2 : public D1
B1(int x) { cout << "Constructing {
B1\n"; a = x; } int c;
~B1 () { cout << "Destructing B1\ public :
n"; } D2(int x, int y, int z) : D1(y, z) { cout <<
int geta () { return a; } "Constructing D2\n"; c = x; }
}; ~D2 () { cout << "Destructing D2\n"; }
void show () { cout << geta () << ' ' <<
// Inherit direct base class .
getb () << ' ' << c << '\n'; }
class D1 : public B1 };
{ int main ()
int b; {
public : D2 ob (1, 2, 3);
ob. show ();
cout << ob. getaDepartment
() << ' ' << ob.
ofgetb
CSE, () CUET
<< '\
11
Virtual Base Class
When directly inheriting from multiple base class, a
problem arises.
Suppose, from one base, two derived are created, derived1,
derived2. Then another derived, derived3 is created,
inheriting the derived1 and derived2.
It causes an ambiguity when base’s member is used by
derived3. So that member is inherited through derived1 or
through derived2?
To mitigate this problem, C++ allows only one copy of
base in derived3, through a method, called virtual base
class.
12 Department of CSE, CUET
Virtual Base Class
# include <iostream > int k;
using namespace std; };
class base class derived3 : public derived1 , public
{ derived2
public :
{
public :
int i;
int product () { return i * j * k; }
}; };
// Inherit base as virtual . int main ()
class derived1 : virtual public base {
{ derived3 ob;
public : ob.i = 10; // unambiguous because only one
int j; copy present
ob.j = 3;
};
ob.k = 5;
// Inherit base as virtual here , too .
cout << "Product is " << ob. product () << '\
class derived2 : virtual public base n';
{ return 0;
public : }
13 Department of CSE, CUET
14 Department of CSE, CUET