Programming Languages: Homework on Inheritance and Polymorphism    

 

After reading the assigned C++ code and playing with the C++ project & executable about the use of static members, virtual methods, constructors, destructors, inheritance, dynamic binding, and polymorphism, do the following things:

 

1.     The program produces output as seen here. For each non-empty line of the output (except for the  lines with “Press c to continue”), explain (i) which object generates the message, (ii) what method in what class is invoked by that object to generate the message, and (iii) why particular names (such as JOSEPH, ANONYMOUS, …), classes (such as baseClass, childClassA, cildClassB, …), and numbers( such as 1, 2, …)  are produced as the result of invoking the method.

 

2.     Explain your understanding of polymorphism in terms of (i) the class hierarchy composed of baseClass, childClassA and childClassB, (ii) the vector basePtrVector and the iterator i in the for loop and (iii) the behavior you see from the output of the program.

 

3.     Explain why in the main function when we clear the basePtrVector, it’ll still end in memory leakage (i.e. creation of garbage in the memory).

 

4.     What would happen if we remove the virtual keyword associated with the destructor and the output method from the class declaration of baseClass? Why?

 

 

5.     What would happen if we replace the public keyword by private in the declaration of childClassA and childClassB? What is the difference?