| < Day Day Up > |
|
Research: Procure a copy of Stanley B. Lippman's excellent book Inside The C++ Object Model and read it. Draw a diagram showing how C++ uses virtual tables to implement dynamic polymorphic behavior.
Reasoning About Polymorphic Behavior: Write a program that implements the inheritance hierarchy shown in the following diagram:
Make the Object::getObjectName() and Component::getComponentType() pure virtual functions. Implement both functions in the ComponentA and ComponentB classes. Ensure the ComponentA versions of the functions are implemented differently from the ComponentB versions.
Write a main() function that creates an Object pointer and a Component pointer. Dynamically create a new ComponentA object and assign it to the Object pointer, and create a ComponentB object and assign it to the Component pointer.
Answer the following questions before finishing the program:
What function(s) can you call on a Component object via an Object pointer?
What function(s) can you call on a Component object via a Component pointer?
Complete the program and test your answers.
Reasoning About Polymorphic Behavior: Study the following header and answer the questions that follow:
#ifndef ALL_CLASSES_H #define ALL_CLASSES_H #include <iostream> using namespace std; class A { public: A(){cout<<"A created!"<<endl;} virtual ~A(){cout<<"A destroyed!"<<endl;} virtual void f() = 0; }; class B : public A{ public: B(){cout<<"B created!"<<endl;} virtual ~B(){cout<<"B destroyed!"<<endl;} }; class C : public B{ public: C(){cout<<"C created!"<<endl;} virtual ~C(){cout<<"C destroyed!"<<endl;} virtual void f(){cout<<"C::f()"<<endl;} }; #endif
Can an object of class B be created? Explain your answer.
Can an object of class A be created? Explain your answer.
Can an object of class C be created? Explain your answer.
If an object of class C can be created what would be output of the following program:
#include <iostream> using namespace std; #include "allclasses.h" int main(){ A* aptr = new C; aptr->f(); delete aptr; return 0; }
Testing Private Inheritance: Write a program that tests the effects of private inheritance. Create a class named Base. In the Base class create a virtual destructor and a virtual function named f() and have it print a simple message to the console. Create another class named Derived that privately inherits the functionality of Base. Give the Derived class two functions: one named f() that has the same function signature as Base::f(), and g(). Have Derived::f() and Derived::g() print simple messages to the console. Create a main() function and write code to perform the following operations:
Declare a Base class pointer and create a Base class object with the new operator.
Call the Base::f() function via the Base class pointer. Note the results. Delete the pointer.
Create a new Derived class object and assign its address to the Base class pointer. Note the results.
What happens when you tried to execute step c? Why does this happen?
Declare a Derived class pointer and create a Derived class object with the new operator.
Call the Derived::f() function via the pointer. What are the results?
Call the Derived::g() function via the pointer. What are the results?
Deferring Implementation of a Pure Virtual Function: Study the following code and answer the questions:
#ifndef ALL_CLASSES_H #define ALL_CLASSES_H #include <iostream> using namespace std; class A { public: virtual ~A(){} virtual void f() = 0; virtual void g() = 0; }; class B : public A{ public: virtual ~B(){} void f(){cout<<"B::f()"<<endl;} }; class C : public B{ public: virtual ~C(){} void g(){cout<<"C::g()"<<endl;} }; #endif
What type of class is class A?
What is the purpose of a virtual destructor?
What type of class is class B? Explain your answer.
Of the three classes A, B, and C, which class is considered the concrete class?
Testing Protected Access: Consider the following source code and answer the question:
#ifndef ALL_CLASSES_H #define ALL_CLASSES_H #include <iostream> using namespace std; class A { public: virtual ~A(){} virtual void f(){cout<<"A::f()"<<endl;} }; class B : public A{ public: virtual ~B(){} protected: virtual void f(){cout<<"B::f()"<<endl;} }; #endif
a. Given a pointer of type A that points to an object of type B, what message will be printed to the screen when function f() is called via the A pointer?
Testing Non-Virtual Functions: Consider the following source code and answer the questions:
#ifndef ALL_CLASSES_H #define ALL_CLASSES_H #include <iostream> using namespace std; class A { public: virtual ~A(){} void f(){cout<<"A::f()"<<endl;} }; class B : public A{ public: virtual ~B(){} void f(){cout<<"B::f()"<<endl;} }; #endif
Given a pointer of type A that points to an object of type B, what message will be printed to the screen when function f() is called via the A pointer?
What change(s) to class A could be made to achieve dynamic polymorphic behavior.
| < Day Day Up > |
|