Chapter 7.
INHERITANCE
Chapter 7. INHERITANCE
Minh Quang NGUYEN
Hanoi National University of Education
September 2015
Paris Saclay
Minh Quang NGUYEN
Chapter 7. INHERITANCE
Paris Saclay
Minh Quang NGUYEN
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
C++ allow you to create a new class from existing classes.
Class B is inherited from class A: B possesses all data
members and function members of A, except the private
members.
I
I
Paris Saclay
Class B is child class, or so-called derived class
Class A is parent class, or so-called base class.
Minh Quang NGUYEN
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
C++ allow you to create a new class from existing classes.
Class B is inherited from class A: B possesses all data
members and function members of A, except the private
members.
I
I
Paris Saclay
Class B is child class, or so-called derived class
Class A is parent class, or so-called base class.
Minh Quang NGUYEN
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
C++ allow you to create a new class from existing classes.
Class B is inherited from class A: B possesses all data
members and function members of A, except the private
members.
I
I
Paris Saclay
Class B is child class, or so-called derived class
Class A is parent class, or so-called base class.
Minh Quang NGUYEN
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
C++ allow you to create a new class from existing classes.
Class B is inherited from class A: B possesses all data
members and function members of A, except the private
members.
I
I
Paris Saclay
Class B is child class, or so-called derived class
Class A is parent class, or so-called base class.
Minh Quang NGUYEN
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
C++ allow you to create a new class from existing classes.
Class B is inherited from class A: B possesses all data
members and function members of A, except the private
members.
I
I
Paris Saclay
Class B is child class, or so-called derived class
Class A is parent class, or so-called base class.
Minh Quang NGUYEN
Chapter 7. INHERITANCE
Inheritance implementation
Structure
Structure to create new class inherited from a base class.
class base_class : access_modifier derived_class
{
data members;
function members;
};
Access Modifier
public
protected
private
Paris Saclay
Members inherited from based class
have the same access modifier of based class
access modifier transforms to protected
access modifier transforms to protected
Minh Quang NGUYEN
Chapter 7. INHERITANCE
Inheritance implementation
Example
Base class: class Shape
Derive class 1: Rectangle
class Shape
{
protected:
float width, height;
public:
void set_data(float a,
float b)
{
width = a;
height = b;
}
};
class Rectangle: public Shape
{
public:
float area ()
{
return (width * height);
}
};
Paris Saclay
Minh Quang NGUYEN
Chapter 7. INHERITANCE
Inheritance implementation
Example (cont.)
Derive class 1: Triangle
main() function
class Triangle: public Shape
{
public:
float area ()
{
return width * height / 2;
}
};
int main ()
{
Rectangle rect;
Triangle tri;
rect.set_data (5,3);
tri.set_data (2,5);
cout << rect.area() << endl;
cout << tri.area() << endl;
return 0;
}
Paris Saclay
Minh Quang NGUYEN
10
Chapter 7. INHERITANCE
Inheritance implementation
Constructor of derived class
Constructor of derived class can call/invoke constructor of base
class by the structure:
derived_class(parameters) :: base_class(parameters)
Example
void Shape(float a, float b)
{
width = a;
height = b;
}
Paris Saclay
void Rectangle(float a, float b)
{
width = a;
height = b;
}
Minh Quang NGUYEN
11
Chapter 7. INHERITANCE
Upcasting and Downcasting
Upcasting
Upcasting is converting a derived-class reference or pointer to a
base-class. In other words, upcasting allows us to treat a derived
type as though it were its base type.
Example given class Shape and Rectangle in previous example. A
function play() that uses base class:
void play(Shape &s)
{
s.set_data(1.1, 2.2);
}
If in some other part of the program we use the play() function
like below:
Triangle c;
play(c);
Paris Saclay
Minh Quang NGUYEN
12
Chapter 7. INHERITANCE
Upcasting and Downcasting
Upcasting: how does it work?
I
A Triangle is being passed into a function that is expecting a
Shape.
Since a Triangle is a Shape, it can be treated as one by play().
That is, any message that play() can send to a Shape a
Triangle can accept.
Child *pChild = &parent; // actually this wont compile
// error: cannot convert from Parent * to Child *
pChild -> gotoSchool();
A Parent is not a Child (a Parent need not have a gotoSchool()
method), the downcasting in the above line can lead to an unsafe
operation.
Paris Saclay
Minh Quang NGUYEN
13
Chapter 7. INHERITANCE
Upcasting and Downcasting
Upcasting: how does it work?
I
A Triangle is being passed into a function that is expecting a
Shape.
Since a Triangle is a Shape, it can be treated as one by play().
That is, any message that play() can send to a Shape a
Triangle can accept.
Child *pChild = &parent; // actually this wont compile
// error: cannot convert from Parent * to Child *
pChild -> gotoSchool();
A Parent is not a Child (a Parent need not have a gotoSchool()
method), the downcasting in the above line can lead to an unsafe
operation.
Paris Saclay
Minh Quang NGUYEN
14
Chapter 7. INHERITANCE
Upcasting and Downcasting
Upcasting: how does it work?
I
A Triangle is being passed into a function that is expecting a
Shape.
Since a Triangle is a Shape, it can be treated as one by play().
That is, any message that play() can send to a Shape a
Triangle can accept.
Child *pChild = &parent; // actually this wont compile
// error: cannot convert from Parent * to Child *
pChild -> gotoSchool();
A Parent is not a Child (a Parent need not have a gotoSchool()
method), the downcasting in the above line can lead to an unsafe
operation.
Paris Saclay
Minh Quang NGUYEN
15
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Multiple inheritance - diamond problem
Paris Saclay
An ambiguity that arises when two
classes B and C inherit from A, and
class D inherits from both B and C.
If there is a method in A that B
and/or C has overridden, and D
does not override it, then which
version of the method does D
inherit: that of B, or that of C?
Minh Quang NGUYEN
16
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Multiple inheritance - diamond problem
Paris Saclay
An ambiguity that arises when two
classes B and C inherit from A, and
class D inherits from both B and C.
If there is a method in A that B
and/or C has overridden, and D
does not override it, then which
version of the method does D
inherit: that of B, or that of C?
Minh Quang NGUYEN
17
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Multiple inheritance - diamond problem
Paris Saclay
An ambiguity that arises when two
classes B and C inherit from A, and
class D inherits from both B and C.
If there is a method in A that B
and/or C has overridden, and D
does not override it, then which
version of the method does D
inherit: that of B, or that of C?
Minh Quang NGUYEN
18
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Diamond problem - solution
1. Clear declaration
2. Virtual base class
D d_object;
d_object.B::x;
d_object.C::x;
Paris Saclay
Minh Quang NGUYEN
19
Chapter 7. INHERITANCE
Virtual base class
Virtual base class
To share a base class, simply insert the virtual keyword in the
inheritance list of the derived class. This creates what is called a
virtual base class, which means there is only one base object that
is shared.
class derived_class: virtual public base_class
{
// member function
};
Paris Saclay
Minh Quang NGUYEN
20
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Virtual base class - example
class C: virtual public A
class A
{
{
public:
public:
C(int nC, int nA): A(nA)
A(int nPower)
{
{
cout
<< "C: " << nC << endl;
cout << "A: " << nPower;
}
}
};
};
class D: public B, public C
class B: virtual public A
{
{
public:
public:
D(int nA, int nB, int nC)
B(int nB, int nA): A(nA)
: A(nB, nA), B(nB, nA), A
{
{
cout << "B: " << nA;
}
}
};
};Paris Saclay
Minh Quang NGUYEN
21
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Display
In main() function:
int main()
{
Copier cCopier(1, 2, 3);
}
Without virtual:
With virtual:
PoweredDevice: 3
Scanner: 1
PoweredDevice: 3
Printer: 2
Paris Saclay
PoweredDevice: 3
Scanner: 1
Printer: 2
Minh Quang NGUYEN
22
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Display
In main() function:
int main()
{
Copier cCopier(1, 2, 3);
}
Without virtual:
With virtual:
PoweredDevice: 3
Scanner: 1
PoweredDevice: 3
Printer: 2
Paris Saclay
PoweredDevice: 3
Scanner: 1
Printer: 2
Minh Quang NGUYEN
23
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Display
In main() function:
int main()
{
Copier cCopier(1, 2, 3);
}
Without virtual:
With virtual:
PoweredDevice: 3
Scanner: 1
PoweredDevice: 3
Printer: 2
Paris Saclay
PoweredDevice: 3
Scanner: 1
Printer: 2
Minh Quang NGUYEN
24
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Virtual base classes are created before non-virtual base
classes, which ensures all bases get created before their
derived classes.
Note that the Scanner and Printer constructors still have calls
to the PoweredDevice constructor. If we are creating an
instance of Copier, these constructor calls are simply ignored
because Copier is responsible for creating the PoweredDevice,
not Scanner or Printer. However, if we were to create an
instance of Scanner or Printer, the virtual keyword is ignored,
those constructor calls would be used, and normal inheritance
rules apply.
If a class inherits one or more classes that have virtual
parents, the most derived class is responsible for constructing
the virtual base class.
Paris Saclay
Minh Quang NGUYEN
25
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Virtual base classes are created before non-virtual base
classes, which ensures all bases get created before their
derived classes.
Note that the Scanner and Printer constructors still have calls
to the PoweredDevice constructor. If we are creating an
instance of Copier, these constructor calls are simply ignored
because Copier is responsible for creating the PoweredDevice,
not Scanner or Printer. However, if we were to create an
instance of Scanner or Printer, the virtual keyword is ignored,
those constructor calls would be used, and normal inheritance
rules apply.
If a class inherits one or more classes that have virtual
parents, the most derived class is responsible for constructing
the virtual base class.
Paris Saclay
Minh Quang NGUYEN
26
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Virtual base classes are created before non-virtual base
classes, which ensures all bases get created before their
derived classes.
Note that the Scanner and Printer constructors still have calls
to the PoweredDevice constructor. If we are creating an
instance of Copier, these constructor calls are simply ignored
because Copier is responsible for creating the PoweredDevice,
not Scanner or Printer. However, if we were to create an
instance of Scanner or Printer, the virtual keyword is ignored,
those constructor calls would be used, and normal inheritance
rules apply.
If a class inherits one or more classes that have virtual
parents, the most derived class is responsible for constructing
the virtual base class.
Paris Saclay
Minh Quang NGUYEN
27
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Virtual base classes are created before non-virtual base
classes, which ensures all bases get created before their
derived classes.
Note that the Scanner and Printer constructors still have calls
to the PoweredDevice constructor. If we are creating an
instance of Copier, these constructor calls are simply ignored
because Copier is responsible for creating the PoweredDevice,
not Scanner or Printer. However, if we were to create an
instance of Scanner or Printer, the virtual keyword is ignored,
those constructor calls would be used, and normal inheritance
rules apply.
If a class inherits one or more classes that have virtual
parents, the most derived class is responsible for constructing
the virtual base class.
Paris Saclay
Minh Quang NGUYEN
28
Chapter 7. INHERITANCE
Q&A
QUESTION and ANSWER
Paris Saclay
Minh Quang NGUYEN
29