6.2.1. Exercises: Derivation with Polymorphism

  1. Add the keyword virtual to the declaration of toString() in the Student class definition. Then build and run that program and explain the results.

  2. Be the computer and predict the output of the programs shown in Example 6.8 through Example 6.12. Then compile and run the programs to check your answers.

    1. Example 6.8. src/polymorphic1.cc

      #include <iostream>
      using namespace std;
      
      class A {
      public:
          virtual ~A() { }
          virtual void foo() {
              cout << "A's foo()" << endl;
              bar();
          }
          virtual void bar() {
              cout << "A's bar()" << endl;
          }
      };
      
      class B: public A {
      public:
          void foo() {
              cout << "B's foo()" << endl;
              A::foo();
          }
          void bar() {
              cout << "B's bar()" << endl;
          }
      };
      
      int main() {
          B bobj;
          A *aptr = &bobj;
          aptr->foo();
          cout << "-------------" << endl;
          A aobj = *aptr;
          aobj.foo();
          cout << "-------------" << endl;
          aobj = bobj;
          aobj.foo();
          cout << "-------------"<< endl;
          bobj.foo();
      }
      
      

      <include src="src/polymorphic1.cc" href="src/polymorphic1.cc" mode="cpp" id="ex-poly1"/>


    2. Example 6.9. src/polymorphic2.cc

      #include <iostream>
      using namespace std;
      
      class A {
      public:
          virtual void foo() {
              cout << "A's foo()" << endl;
          }
      };
      
      class B: public A {
      public:
          void foo() {
              cout << "B's foo()" << endl;
          }
      };
      
      class C: public B {
      public:
          void foo() {
              cout << "C's foo()" << endl;
          }
      };
      
      
      int main() {
          C cobj;
          B *bptr = &cobj;
          bptr->foo();
          A* aptr = &cobj;
          aptr->foo();
      }

      <include src="src/polymorphic2.cc" href="src/polymorphic2.cc" mode="cpp"/>


    3. Example 6.10. src/derivation/exercise/Base.h

      [ . . . . ]
      class Base {
      public:
          Base();
          void a();
          virtual void b() ;
          virtual void c(bool condition=true);
          virtual ~Base() {}
      };
      
      
      class Derived : public Base {
      public:
          Derived();
          virtual void a();
          void b();
          void c();
      };
      [ . . . . ]
      

      <include src="src/derivation/exercise/Base.h" href="src/derivation/exercise/Base.h" mode="cpp"/>


      Example 6.11. src/derivation/exercise/Base.cpp

      [ . . . . ]
      Base::Base() {
          cout << "Base::Base() " << endl;
          a();
          c();
      }
      void Base::c(bool condition) {
          cout << "Base::c()" << endl;
      }
      void Base::a() {
          cout << "Base::a()" << endl;
          b();
      }
      void Base::b() {
          cout << "Base::b()" << endl;
      }
      
      Derived::Derived() {
          cout << "Derived::Derived() " << endl;
      }
      
      void Derived::a() {
          cout << "Derived::a()" << endl;
          c();
      }
      void Derived::b() {
          cout << "Derived::b()" << endl;
      }
      
      void Derived::c() {
          cout << "Derived::c()" << endl;
      }
      [ . . . . ]
      

      <include src="src/derivation/exercise/Base.cpp" href="src/derivation/exercise/Base.cpp" mode="cpp"/>


      Example 6.12. src/derivation/exercise/main.cpp

      [ . . . . ]
      int main (int argc, char** argv) {
      
          Base b;
          Derived d;
      
          cout << "Objects Created" << endl;
          b.b();
          cout << "Calling derived methods" << endl;
          d.a();
          d.b();
          d.c();
          cout << ".. via base class pointers..." << endl;
          Base* bp = &d;
          bp->a();
          bp->b();
          bp->c();
          //d.c(false);
      }
      [ . . . . ]
      

      <include src="src/derivation/exercise/main.cpp" href="src/derivation/exercise/main.cpp" mode="cpp" id="ex-deriv3"/>


[ fromfile: inheritance-intro.xml id: None ]