16.13 HOMEWORK


16.13 HOMEWORK

  1. In the following program, X is a virtual base for Y, which in turn is a regular base for Z. The constructors for both Y and Z invoke X's constructor. Notice that Z's constructor in line (B) invokes its two superclass constructors with two different arguments, 22 and 11. While the argument of 11 gets passed on to X constructor directly in line (B), the argument 22 gets passed on to X constructor in line (A). What will be printed out in line (C) for the data member x inherited by Z? Will it be 11 or 22?

          #include <iostream>      using namespace std;      class X {      public:           int x;           X( int xx ) : x(xx) {}      };      class Y : virtual public X {      public:           Y(int xx ) : X(xx) {}                 //(A)      };      class Z : public Y {      public:           Z() : Y(22), X(11) {}                 //(B)      };      int main()      {           Z zobj;           cout << zobj.x << endl;               //(C)           return 0;      } 
  2. In the following program, even though the derived class Z does not make a direct invocation of the constructor of the virtual base X, the program compiles fine. Why?

          #include <iostream>      using namespace std;      class X {           int x;      public:           X() {}           X(int xx) : x(xx) {}      };      class Y : virtual public X {      public:         Y( int xx ) : X( xx ) {} };      class Z : public Y {      public:           Z( int xx ) : Y( xx ) {}      };      int main() {} 
  3. In the following program, X is a virtual base for Y, which in turn is a regular base for Z. The compiler outputs the following error message for this program:

    No matching function for call to X::X()

    Why?

          #include <iostream>      using namespace std;      class X {           int x;      public:           X( int xx ) : X( xx ) {}      };      class Y : virtual public X {      public:           Y( int xx ) : X( xx ) {}      };      class Z : public Y {      public:           Z( int xx ) : Y( xx ) {}      };      int main() {} 
  4. In the following program, X is a virtual base for Y, which in turn is a regular base for Z. The output statement in line (A) causes the number -4261484 ( or some other equally unexpected and bizarre looking number ) to be printed out on the terminal. Why?

          #include <iostream>      using namespace std;      class X {           int x;      public:           X() {}           X( int xx ) : x( xx ) {}           int getx() const { return x; }      };      class Y : virtual public X {      public:           Y( int xx ) : X( xx ) {}      };      class Z : public Y {      public:           Z( int xx ) : Y( xx ) {}      };      int main()      {           Z zobj( 100 );           cout << zobj.getx() << endl;             //(A)           return 0;      } 
  5. The following program has a diamond hierarchy of classes. X is a public base of Y and T. And, the classes Y and T are both superclasses of U. Note that X at the apex of the hierarchy is not a virtual base. This program does not compile. Why?

          #include <iostream>      using namespace std;      class X {      public:           int x;           X( int xx ) : x( xx ) {}      };      class Y : public X {      public:           Y( int xx ) : X( xx ) {}      };      class T : public X {      public:           T( int xx ) : X( xx ) {}      };      class U : public Y, public T {      public:           U( int xx ) : Y( xx ), T( xx ) {}      };      int main()      {           U uobj( 100 );           cout << uobj.x << endl;           return 0; } 
  6. Write a Java version of the mixin classes example of Section 16.9. Use Java interfaces for serving the same purpose as the mixin classes of that section.

  7. Write a Java version of the role playing classes of Section 16.10. Use Java interfaces for the abstract role types, SalesType and ManagerType, of that section.

  8. C++ also makes available a function typeid() that can be used to ascertain the class identity of an object. Using this function, the implementation of the checkReadyForPromotion() function in Section 16.10 would become

          bool checkReadyForPromotion( Employee* e ) {           Role* r = e->getActiveRole();           if ( typeid( *r ) == typeid( Manager ) ) {              if ( r->getRoleExperience()      >= MinYearsForPromotion ) {                  cout << "yes, ready for promotion in the active role"                       << endl;                  return true;               }               else {                    cout << "No, not yet ready for promotion "                         << "in the active role" << endl;                    return false;               }           }           else if ( typeid( *r ) == typeed( Salesperson ) ) {               if (s->productivityGainYtoY() > 50 ) {                   cout << "yes, ready for promotion in the active role"                        << endl;                   return true;               }                     else {                          cout << "Not ready for promotion in the active role"                               << endl;                          return false;                     }               }           else {                cout << "Unable to determine if ready for promotion"                     << endl;                return false;           }   }

    Incorporate this version of checkReadyForPromotion() in the source code in Section 16.10.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

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