09 Inheritance
09 Inheritance
Yean-Ru Chen
EE, NCKU
• This existing class is called the base class, and the new class is referred to as the
derived class.
• A derived class contains behaviors inherited from its base class and can contain
additional behaviors.
• A derived class can also customize behaviors inherited from the base class.
• A direct base class is the base class from which a derived class
explicitly inherits.
• In the case of single inheritance, a class is derived from one base class.
3
Direct vs. Indirect Base Class and
Single vs. Multiple Inheritance (cont.)
indirect base class direct base class derived class
Human Student TA
single multiple
inheritance inheritance eating( )
eating( ) eating( )
coding( )
coding( )
grading( )
teaching( )
Grader
grading( )
4
public, private and protected Inheritance
5
is-a vs. has-a Relationship
6
Base Classes and Derived Classes
7
Base Classes and Derived Classes (cont.)
Human
Student Grader
TA 8
Tetrominos in Tetris Game
https://www.youtube.com/watch?v=8ArEKGDfg2E
Source: http://en.wikipedia.org/wiki/Tetris
http://en.wikipedia.org/wiki/Tetromino 9
IBlock.h
S_Block
Only paint() needs to be re-written.
13
tetris.cpp
IBlock2.h tetris3.cpp
0000
… …
13 class I_Block{ 4 int main() 0100
14 public: 5 { 0110
15 I_Block(int xx=0,int yy=0,int ri=0): 6 S_Block s(2,0,1); 0010
x(xx),y(yy),rotate_index(ri) {} ; … 0000
…
0000
SBlock_inh2.h 0011
… 0110
14 class S_Block: public I_Block{ 0000
15 public:
16 S_Block(int sx=0, int sy=0, int si=0): 0000
I_Block(sx,sy,si) {} 0011
… 0110
0000
0100
0110
16 0010
Passing Arguments to Constructors (cont.)
• As a derived class (formed with public inheritance), S_Block inherits all the
members of class I_Block, except for the constructor—each class provides its
own constructors that are specific to the class.
• The compiler generates errors because base class I_Block’s data members are
private—derived class S_Block’s member functions are not allowed to
access base class I_Block’s private data.
• The errors in derived class could have been prevented by using the get member
functions inherited from base class.
20
private Members
21
protected Members
• A base class’s private members are accessible only within its body
and to the friends of that base class.
22
protected Members (cont.)
23
Fake main Function Through
Inheritance
class A {
public:
A(int aa, int bb): x(aa), y(bb) { }
private:
int x,y; fake_main.cpp:5: error: 'int A::x' is private
}; fake_main.cpp:10: error: within this context
class B : public A { fake_main.cpp:5: error: 'int A::y' is private
public: fake_main.cpp:10: error: within this context
B(int a, int b): A(a,b) { }
void fake_main() { cout << x << '\t' << y << endl; }
};
int main()
{
B b(1,2);
b.fake_main();
}
24
Constructors and Destructors in Derived
Classes
25
Constructors and Destructors in Derived
Classes (cont.)
• When a derived class object is destroyed, the program calls that
object’s destructor.
• This begins a chain (or cascade) of destructor calls in which the
derived-class destructor and the destructors of the direct and
indirect base classes and the classes’ members execute in reverse of
the order in which the constructors executed.
I
• When a derived class object’s destructor is called, the destructor
performs its task, then invokes the destructor of the next base class
up the hierarchy.
S A • This process repeats until the destructor of the final base class at the
top of the hierarchy is called.
• Then the object is removed from memory.
26
Constructors and Destructors in Derived
Classes (cont.)
• Base-class constructors, destructors and overloaded assignment
operators are not inherited by derived classes.
27
Constructors and Destructors in Derived
Classes with Composition
• Suppose that we create an object of a derived class where both the base
class and the derived class contain (via composition) objects of other
classes. When an object of that derived class is created, first the
constructors for the base class’s member objects execute, then the base
class constructor executes, then the constructors for the derived class’s
member objects execute, then the derived class’s constructor executes.
• Destructors for derived class objects are called in the reverse of the
order in which their corresponding constructors are called.
28
Sequence of Constructors and Destructors
• When deriving a class from a base class, the base class may be
inherited through public, protected or private inheritance.
31
public, protected and private
Inheritance (cont.)
32