Chapter 7. Inheritance
Chapter 7. Inheritance
INHERITANCE
Chapter 7. INHERITANCE
Minh Quang NGUYEN
Hanoi National University of Education
September 2015
Paris Saclay
Chapter 7. INHERITANCE
Paris Saclay
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
Paris Saclay
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
Paris Saclay
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
Paris Saclay
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
Paris Saclay
Chapter 7. INHERITANCE
What is inheritance?
Concept
I
I
Paris Saclay
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
Chapter 7. INHERITANCE
Inheritance implementation
Example
Base class: class Shape
class Shape
{
protected:
float width, height;
public:
void set_data(float a,
float b)
{
width = a;
height = b;
}
};
Paris Saclay
Chapter 7. INHERITANCE
Inheritance implementation
Example (cont.)
Derive class 1: Triangle
main() function
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
10
Chapter 7. INHERITANCE
Inheritance implementation
Example
void Shape(float a, float b)
{
width = a;
height = b;
}
Paris Saclay
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);
}
12
Chapter 7. INHERITANCE
Upcasting and Downcasting
13
Chapter 7. INHERITANCE
Upcasting and Downcasting
14
Chapter 7. INHERITANCE
Upcasting and Downcasting
15
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Paris Saclay
16
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Paris Saclay
17
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
Paris Saclay
18
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
1. Clear declaration
2. Virtual base class
D d_object;
d_object.B::x;
d_object.C::x;
Paris Saclay
19
Chapter 7. INHERITANCE
Virtual base class
Paris Saclay
20
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
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
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
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
24
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
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.
Paris Saclay
25
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
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.
Paris Saclay
26
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
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.
Paris Saclay
27
Chapter 7. INHERITANCE
Multiple inheritance - diamond problem
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.
Paris Saclay
28
Chapter 7. INHERITANCE
Q&A
Paris Saclay
29