Skill Building Exercises

 < Day Day Up > 



  1. 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.

  2. Reasoning About Polymorphic Behavior: Write a program that implements the inheritance hierarchy shown in the following diagram:

    click to expand

    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:

    1. What function(s) can you call on a Component object via an Object pointer?

    2. What function(s) can you call on a Component object via a Component pointer?

    Complete the program and test your answers.

  3. 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
    1. Can an object of class B be created? Explain your answer.

    2. Can an object of class A be created? Explain your answer.

    3. Can an object of class C be created? Explain your answer.

    4. 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; }
  4. 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:

    1. Declare a Base class pointer and create a Base class object with the new operator.

    2. Call the Base::f() function via the Base class pointer. Note the results. Delete the pointer.

    3. Create a new Derived class object and assign its address to the Base class pointer. Note the results.

    4. What happens when you tried to execute step c? Why does this happen?

    5. Declare a Derived class pointer and create a Derived class object with the new operator.

    6. Call the Derived::f() function via the pointer. What are the results?

    7. Call the Derived::g() function via the pointer. What are the results?

  5. 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
    1. What type of class is class A?

    2. What is the purpose of a virtual destructor?

    3. What type of class is class B? Explain your answer.

    4. Of the three classes A, B, and C, which class is considered the concrete class?

  6. 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?

  7. 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
    1. 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?

    2. What change(s) to class A could be made to achieve dynamic polymorphic behavior.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net