15.22 HOMEWORK


15.22 HOMEWORK

  1. Identify and correct the error in the derived-class constructor syntax in this Java code.

          class Employee {          private String fName, 1Name;          public Employee(String f, String 1) {              fName = f;              1Name = 1;          }      }      class Manager extends Employee {          private short level;          public Manager(String f, String 1, short 1v1) {              Employee(f, 1);              level = 1v1;          }          public Manager(String f, String 1) {              this(f, 1, 10);          }      } 

    State what the data members of the following object will be set to after the following line is executed:

          Employee e = new Manager ( "John", "Doe" ); 
  2. Is the following C++ syntax legal? Note that the base classXand the derived classYuse the same identifiers m and foo for class members. Pay careful attention to the syntax used for the function calls in main. If the code is legal, what's its output?

          class X {      public:          int m;          X( int mm ) : m( mm ) {}          void foo() { cout << "X's foo invoked" << endl; }      };      class Y : public X {      public:          int m;          Y( int a, int b ) : X(a), m( b ) {}          void foo() { cout << "Y's foo invoked" << endl; }      };      int main() {          X xobj( 10 );          xobj.foo();          Y yobj( 10, 20 );          yobj.foo();          yobj.X::foo();          return 0;      } 
  3. If the goal was to write correctly the copy constructor for a derived class in C++, is the following example correct? If not, how will you fix it?

          class X {         int m;      public:         X( int mm ) : m( mm ) {}         X( const X& other ) : m( other.m ) {}      };      class Y : public X {         int n;      public:         Y( int mm, int nn ) : X( mm ), n( nn ) {}         Y( const Y& other ) : n( other.n ) {}      }; 
  4. The following C++ program compiles without any problems. When run, it even prints out the "hello" called for in line (B) of main. But subsequently the program aborts with a memory segmentation fault. Why? (Hint: If you comment out the assignment statement in line (A), the program runs flawlessly.)

     
    //MysteryBug.cc #include <iostream> using namespace std; class X { int* p; int size; public: X() { p = 0; size = 0; } X( int* ptr, int sz ) : size( sz ) { p = new int[ size ]; for ( int i=0; i<size; i++ ) p[i] = ptr[i]; } ~X() { delete[] p; } }; class Y : public X { int n; public: Y() {}; Y( int* ptr, int sz, int nn ) : X( ptr, sz ), n( nn ) {} Y( const Y& other ) : X( other ), n( other.n ) {} Y& operator=( const Y& other ) { if ( this == & other ) return *this; X: :operator=( other ); n = other.n; return *this; } }; int main() { int data[ 3 ] = {3, 2, 1}; Y y1( data, 3, 10 ); Y y2; y2 = y1; //(A) cout << "hello" << endl; //(B) return 0; }
  5. While the following C++ program compiles fine

          #include <iostream>      using namespace std;      class X {      public:          void foo();          X() { }      };      class Y : public X {      public:          void foo() { cout << "Y's foo invoked" << endl; }           Y() {}      };      int main() {} 

    the following program does not compile and reports a problem from the linker. Why?

          #include <iostream>      #include <vector>      using namespace std;      class X {      public:          virtual void foo();          X() { }      };      class Y : public X {      public:          void foo() { cout << "Y's foo invoked" << endl; }          Y() {}      };      int main() {} 
  6. Will the following C++ program compile?

          class X {      public:          virtual void f();      };      class Y : public X { };            int main() { Y yobj; } 
  7. Will the following version of the previous program compile?

          class X {      public:           virtual void f() = 0;      };      class Y : public X { };      int main() { Y yobj; } 
  8. Will the following version of the previous program compile and run?

          class X {      public:          virtual void f() = 0;      };      class Y : public X { void f() {}; };            int main() { Y yobj; } 
  9. With regard to exception specifications for overriding functions in Java, is the following code legal?

          class MyException extends Exception {}      class X {          public void foo() throws MyException {                throw new MyException();          }      }      class Y extends X {          public void foo() {              System.out.printIn( "Y's foo invoked" );          }      } 
  10. Do you see any problems with the following Java code? Note that both the interfaces X and Y possess a function of the same signature. Will this code fragment compile?

          interface X { public int foo( int m ); }      interface Y { public int foo( int n ); }      class W implements X, Y {            public int foo( int x ) { return x; }      } 
  11. vis-à-vis the code in the previous problem, the Java code shown here shows two interfaces possessing function fooral> of the same name but different signatures. Do you see any problems with this code? Will it compile?

          interface X { public int foo( int m ); }      interface Y { public int foo( int n, int p ); }      class W implements X, Y {            public int foo( int x ) { return x; }      } 
  12. Will the following Java program compile?

          interface X { public int foo( int m ); }      class X { public int foo( int q ) { return q; } }      class W implements X {            public int foo( int x ) { return x; }      } 




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