Lecture 14
Recap
Introduction to Inheritance
Inheritance in C++
IS-A Relationship
Polymorphism in Inheritance
Classes in Inheritance
Visibility Rules
Constructor and Base Class
Adding Members
Overriding a Method
Methods can be override from the base class in the
derived class by simply providing a derived class method
with the same signature
A derived class method must have the same or
compatible return type
Sometimes it is required to invoke the derived class
method to the base class method to augment a base class
method rather than doing something entirely different.
This is known as partial overriding
The scope operator can be used to call a base class
method
Example
class Workaholic : public Worker
{
public :
void dowork( )
{
Worker::doWork( ); // Work like a Worker
drinkCof fee ( ) ; // Take a break
Worker::doWork( ); // Work like a Worker some more
}
Static and Dynamic Binding
Example Code
1 Worker w;
2 Workaholic wh;
3 . . .
4 w. doWork ( ) ; wh. doWork ( ) ;
The above code illustrates the fact that we can declare
Worker and Workaholic objects in the same scope
because the compiler can deduce which dowork method to
apply
w is a Worker and wh is a Workahol ic, so the
determination of which dowork is used in the two calls at