Lecture 7 - Inheritance
Lecture 7 - Inheritance
Lecture 7
Inheritance
Dr C.F. Kwong
[email protected]
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 310
Topics
Inheritance
Class Access
Base Pointers
Constructors and Destructors Issues
Redefining Base Class Functions
Virtual Functions
What Is Inheritance?
Provides a way to create a new class from an existing
class
The new class is a specialized version of the existing
class
The "is a" Relationship
Inheritance establishes an "is a" relationship
between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
Inheritance – Terminology and Notation
apple lunch;
Fruit
Apple Strawberry
Inheritance - Hierarchies of Objects
Of course, although apple is a fruit, the reverse is not true. So apple might
have more member variables and functions than fruit.
class fruit {
public:
float cost;
void eat();
};
public: Fruit
void peel();
};
Apple Strawberry
Topics
Inheritance
Class Access
Base Pointers
Constructors and Destructors Issues
Redefining Base Class Functions
Virtual Functions
Class Access Specifiers
Square::Square(int side):Rectangle(side,side)
public:
In particular,
void eat();
}; my_lunch->eat();
class apple:public fruit;
calls exactly the same function,
class strawberry:public fruit;
main () { fruit::eat();
my_lunch=new apple;
} else {
my_lunch=new strawberry;
}
my_lunch->eat();
}
Virtual Functions
We know that apple and strawberry must somehow actually have their own
versions of eat() and what we want is that the compiler checks to see
what my_lunch actually points to when eat() is called at run time and to
find and call the most appropriate version of eat() for it.
We just add the word virtual before the function eat() as follows and re-
declare the function in each derived class.
Virtual Functions
class fruit {
public:
public:
virtual void eat(); // apple provides its own version of eat(); if not redefined
here then fruit’s eat() is used as a default
};
public:
virtual void eat(); // strawberry provides its own version of eat(); if not
redefined here then fruit’s eat() is used as a default
};
Virtual Functions
The various versions of the virtual function are defined as usual
Note the virtual does not appear in the definition, only the declaration
void fruit::eat() {
class fruit {
public:
if luck==1, my_lunch actually points to an apple and the compiler first looks for apple::eat()
and if it exists it uses it.
However, if apple::eat() didn’t exist it goes up the hierarchy to fruit::eat and uses that instead.
Similarly if luck==0, my_lunch actually points to a strawberry and the compiler first looks for
strawberry::eat() and if it finds it, uses it etc.
Virtual Functions
Note that the re-declaration of void eat() in the derived classes must
be of the same return type and arguments as the declaration in
the base class - different arguments means different function as
we know!
If we think about it, we realise that the derived classes are inheriting
the
interface of the eat() function from the base class, not its
implementation
One final point is that the destructor of base classes must always be
declared virtual for reasons of predictability in the order in which
the different destructors are called.
Virtual functions are great for code expansion as well as building into
the program the generality of certain functions
Virtual Destructors
It's a good idea to make destructors virtual if the
class could ever become a base class.
Otherwise, the compiler will perform static binding
on the destructor if the class ever is derived from.
See Program 15-14 for an example
Multiple Inheritance
class class
square rectSolid
class
cube
Multiple Inheritance
Arguments can be passed to both base classes'
constructors:
cube::cube(int side) : square(side),
rectSolid(side, side, side);
Base class constructors are called in order given in
class declaration, not in order used in class
constructor
Multiple Inheritance