Professional Documents
Culture Documents
Programming: Inheritance
Introduction
• Inheritance
– The capability of a class to derive properties and
characteristics from another class is called Inheritance
– New classes created from existing classes
– Absorb attributes and behaviors.
• Base class
– A class that is used to create (or derive) another class is
called the base class. Also called a super class or parent
class.
• Derived class
– Class that inherits data members and member functions
from a previously defined base class. Also, called as child
class or sub class.
Introduction
• Inheritance
– Software reusability
– Create new class from existing
class
• Absorb existing class’s data and
behaviors
• Enhance with new capabilities
– Subclass extends superclass
• Subclass
– More specialized group of objects
– Behaviors inherited from superclass
» Can customize
– Additional behaviors
Inheritance vs Composition
• Inheritance is an "is-a" relationship. Composition is a "has-a".
• Flexibility
– When you use Inheritance, you have to define which class you are extending in
code, it cannot be changed at runtime, but with Composition you just define a
Type which you want to use, which can hold its different implementation.
• Limited code reuse with Inheritance
– Inheritance you can only extend one class, which means you code can only
reuse just one class (except multiple inheritance), not more than one. If you
want to leverage functionalities from multiple class, you must use
Composition.
• Encapsulation
– Though both Inheritance and Composition allows code reuse, Inheritance
breaks encapsulation because in case of Inheritance, sub class is dependent
upon super class behavior. If parent classes changes its behavior than child
class is also get affected.
Introduction
• Inheritance
– Single Inheritance
• Class inherits from one base class
– Multiple Inheritance
• Class inherits from multiple base classes
– Multilevel Inheritance
• A derived class with one base class and that
base class is a derived class of another
Introduction
• Inheritance
– Hierarchical Inheritance
• Multiple derived classes with same base
class
– Hybrid Inheritance
• Combination of multiple and
hierarchical inheritance
– Multipath Inheritance
• A derived class with two base classes and
these two base classes have one common
base class
Base and Derived Classes
• protected inheritance
– Intermediate level of protection between public and
private inheritance
– Derived-class members can refer to public and
protected members of the base class simply by using the
member names
– Note that protected data “breaks” encapsulation
• protected access
– Intermediate level of protection between public and private
– protected members accessible by
• superclass members
• subclass members
• Class members in the same package
– Subclass access to superclass member
• Keyword super and a dot (.)
Public, Private, and Protected Inheritance
Public, Private, and Protected Inheritance
class base
{
public:
int x;
protected:
int y;
private:
int z;
};
Public, Private, and Protected Inheritance
class publicDerived: public base
{
// x is public // y is protected // z is not accessible from
publicDerived
};
class protectedDerived: protected base
{ // x is protected // y is protected // z is not accessible from
protectedDerived
};
class privateDerived: private base
{ // x is private // y is private // z is not accessible from
privateDerived
};
Public Inheritance
class Base int main()
{ {
public: Base base;
int m_public; base.m_public = 1; // okay: m_public is
private: public in Base
int m_private; base.m_private = 2; // not okay: m_priva
protected: te is private in Base
int m_protected; base.m_protected = 3; // not okay: m_pr
}; otected is protected in Base
class Pub: public Base // public inheritance
{ Pub pub;
public: pub.m_public = 1; // okay: m_public is p
Pub() ublic in Pub
{ pub.m_private = 2; // not okay: m_privat
m_public = 1; // okay: m_public was e is inaccessible in Pub
inherited as public pub.m_protected = 3; // not okay: m_pro
m_private = 2; // not okay: m_private is in tected is protected in Pub
accessible from derived class return 0;
m_protected = 3; // okay: m_protected was }
inherited as protected
}
};
Private Inheritance (Default Inheritance)
class Base int main()
{ {
public: Base base;
int m_public; base.m_public = 1; // okay: m_public is
private: public in Base
int m_private; base.m_private = 2; // not okay: m_priva
protected: te is private in Base
int m_protected; base.m_protected = 3; // not okay: m_pr
}; otected is protected in Base
class Pri: private Base // note: private inherita
nce Pri pri;
{ pri.m_public = 1; // not okay: m_public i
public: s now private in Pri
Pri() pri.m_private = 2; // not okay: m_private
{ is inaccessible in Pri
m_public = 1; // okay: m_public is now pri pri.m_protected = 3; // not okay: m_prot
vate in Pri ected is now private in Pri
m_private = 2; // not okay: derived classes return 0;
can't access private members in the base class }
m_protected = 3; // okay: m_protected is
now private in Pri
}};
Single Inheritance Example
// inheritance.cpp class derive : public base
#include <iostream> //single derived class
using namespace std; {
class base //single base class private:
{ int y;
public: public:
int x; void readdata()
void getdata() {
{ cout << "Enter the value of
y = "; cin >> y;
cout << "Enter the value of x =
"; cin >> x; }
} void product()
}; {
cout << "Product = " << x * y;
} };
Single Inheritance Example
int main() Output
{
derive a; //object of Enter the value of x = 3
derived class Enter the value of y = 4
a.getdata(); Product = 12
a.readdata();
a.product();
return 0;
} //end of program
Multiple Inheritance Example
#include <iostream> class C: public A, public B {
using namespace std; public:
class A { int c = 20;
public: C() {
int a = 5; cout << "Constructor for class
A() { C" << endl;
cout << "Constructor for class cout<<"Class C inherits from
A" << endl; class A and class B" << endl;
} }};
}; int main() {
class B { C obj;
public: cout<<"a = "<< obj.a <<endl;
int b = 10; cout<<"b = "<< obj.b <<endl;
B() { cout<<"c = "<< obj.c <<endl;
cout << "Constructor for class return 0;
B" << endl; }
}};
Multilevel Inheritance Example
// inheritance.cpp class derive2 : public derive1
#include <iostream> { private:
using namespace std; int z;
class base //single base class public:
{public: void indata()
int x; {
void getdata() { cout << "\nEnter value of
cout << "Enter value of z= "; cin >> z;
x= "; cin >> x;}}; }
class derive1 : public base void product()
{public: {
int y; cout << "\nProduct= "
void readdata(){ << x * y * z;
cout << "\nEnter value }
of y= "; cin >> y; };
}
};
Multilevel Inheritance Example
int main() Output
{
derive2 a; //object of Enter value of x= 2
derived class
a.getdata(); Enter value of y= 3
a.readdata();
a.indata(); Enter value of z= 3
a.product();
return 0; Product= 18
} //end of program
Hierarchical Inheritance Example
// hierarchial inheritance.cpp class C : public A //C is also
#include <iostream> derived from class base
using namespace std; { public:
class A //single base class void sum(){
{ public: cout << "\nSum= " << x + y;
int x, y; }};
void getdata() { int main()
cout << "\nEnter value {
of x and y:\n"; cin >> x >> y;}}; B obj1; //object of derived
class B : public A //B is derived class B
from class base C obj2; //object of derived
{ public: class C
void product(){ obj1.getdata();
cout << "\nProduct= " obj1.product();
<< x * y; obj2.getdata();
} obj2.sum();
}; return 0;
} //end of program
Hierarchical Inheritance Example
Output
HourlyWorker::print() is executing
• Derived-class constructor
– Calls the constructor for its base class first to initialize its
base-class members
– If the derived-class constructor is omitted, its default
constructor calls the base-class’ default constructor
• Destructors are called in the reverse order of
constructor calls.
– Derived-class destructor is called before its base-class
destructor
1 // Fig. 19.7: point2.h
2 // Definition of class Point
3 #ifndef POINT2_H
4 #define POINT2_H
5
6 class Point {
7 public:
8 Point( int = 0, int = 0 ); // default constructor
9 ~Point(); // destructor
10 protected: // accessible by derived classes
11 int x, y; // x and y coordinates of Point
12 }; // end class Point
13
14 #endif
15 // Fig. 19.7: point2.cpp
16 // Member function definitions for class Point
17 #include <iostream>
18
19 using std::cout;
20 using std::endl;
21
22 #include "point2.h"
23
24 // Constructor for class Point
25 Point::Point( int a, int b )
26 {
27 x = a;
28 y = b;
29
30 cout << "Point constructor: "
31 << '[' << x << ", " << y << ']' << endl;
32 } // end Point constructor
33
34 // Destructor for class Point
35 Point::~Point()
36 {
37 cout << "Point destructor: "
38 << '[' << x << ", " << y << ']' << endl;
39 } // end Point destructor
40 // Fig. 19.7: circle2.h
41 // Definition of class Circle
42 #ifndef CIRCLE2_H
43 #define CIRCLE2_H
44
45 #include "point2.h"
46
47 class Circle : public Point {
48 public:
49 // default constructor
50 Circle( double r = 0.0, int x = 0, int y = 0 );
51
52 ~Circle();
53 private:
54 double radius;
55 }; // end class Circle
56
57 #endif
58 // Fig. 19.7: circle2.cpp
59 // Member function definitions for class Circle
60 #include <iostream>
61
62 using std::cout;
63 using std::endl;
64
65 #include "circle2.h"
66
67 // Constructor for Circle calls constructor for Point
68 Circle::Circle( double r, int a, int b )
69 : Point( a, b ) // call base-class constructor
70 {
71 radius = r; // should validate
72 cout << "Circle constructor: radius is "
73 << radius << " [" << x << ", " << y << ']' << endl;
74 } // end Circle constructor
75
76 // Destructor for class Circle
77 Circle::~Circle()
78 {
79 cout << "Circle destructor: radius is "
80 << radius << " [" << x << ", " << y << ']' << endl;
81 } // end Circle destructor
82 // Fig. 19.7: fig19_07.cpp
83 // Demonstrate when base-class and derived-class
84 // constructors and destructors are called.
85 #include <iostream>
86
87 using std::cout;
88 using std::endl;
89
90 #include "point2.h"
91 #include "circle2.h"
92
93 int main()
94 {
95 // Show constructor and destructor calls for Point
96 {
97 Point p( 11, 22 );
98 } // end block
99
100 cout << endl;
101 Circle circle1( 4.5, 72, 29 );
102 cout << endl;
103 Circle circle2( 10, 5, 5 );
104 cout << endl;
105 return 0;
106 } // end function main
Point constructor: [11, 22]
Point destructor: [11, 22]
• baseClassObject = derivedClassObject;
– This will work
• Remember, the derived class object has more members than the base
class object
– Extra data is not given to the base class
• derivedClassObject = baseClassObject;
– May not work properly
• Unless an assignment operator is overloaded in the derived class, data
members exclusive to the derived class will be unassigned
– Base class has less data members than the derived class
• Some data members missing in the derived class object
Implicit Derived-Class Object to Base-Class
Object Conversion