Inheritance Discovery

Syntax and facts:

Inheritance syntax on a class:

One class inherits another by placing " : modifier classname being inherited " after the classname and including the base class header.

C++ allows multiple inheritance using

public / protected / private:

constructor:

defining functions in the derived (child) class:

should class inherit or contain:

polymorphism: pointers and references of base types can hold derived types, but not the reverse:

virtual:

Arrays (vectors)

Dynamic downcast:

Determine type:


Task: Fill in the grid below using a set of classes and test driver programs that you create:

Create a set of shape classes with Shape being an abstract class from which triangle and rectangle are derived. (You can also derive square from rectangle as well to add one more level.)

Use these classes to fill in the grids below: To do this, do not use pure virtual functions.

  Base Class Derived Class function variable pointer type variable type used base or derived function?
1 virtual function virtual function base

base

 
2 not a virtual function virtual function base base  
3 not a virtual function not a virtual function base base  
4 virtual function virtual function base derived  
5 not a virtual function virtual function base derived  
6 not a virtual function not a virtual function base derived  
           
           

First verify you can create an object of the base type. Then make your virtual function in your base class a pure virtual function by adding = 0 and removing the implementation. Fill out the grid below:

  Can you create a variable of the type (not a pointer to the type) of the base? Can you create a variable of the type (not a pointer to the type) of the derived?
base contains pure virtual    
base does not contain pure virtual    

public/ private / protected exploration: optional

Make three base variables private, protected and public access levels. Inherit the base in a derived class at a private level. Create a driver program to create one object of the base and one of the derived class. Experiment to fill in the grid below

and then fill in this grid about a private derived inheritance:

base Can the derived class access the variables using dot notation? Can the driver program access the variables of a base object using dot notation? Can the driver program access the variables of a derived object using dot notation?
public      
private      
protected      

If you change the derived class to public inheritance (on the class line) does it change your answers?