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();
      }
      
      

    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();
      }

    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();
      };
      [ . . . . ]
      

      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;
      }
      [ . . . . ]
      

      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);
      }
      [ . . . . ]
      

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