9.15 HOMEWORK


9.15 HOMEWORK

  1. In the following program, main calls a function g() in line (B) and passes the argument to this function by value. The function g() is defined in line (A). How many times will the destructor of class X be invoked as the program runs to completion.

     
    //CountDestructorInvoc1.cc #include <iostream> using namespace std; class X { int n; public: X(int nn) : n(nn) {} ~X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; void g(X x) {} //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  2. The program below differs from the program of the previous problem in only one small respect: the function call in line (B) passes the argument to the function in line (A) by reference. How many times will the destructor of class X be invoked in this case as the program runs to completion?

     
    //CountDestructorInvoc2.cc #include <iostream> using namespace std; class X { int n; public: X(int nn) : n(nn) {} ~X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; void g(X& x) {} //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  3. In a further variation on the programs shown in the previous two problems, the function g() in line (A) now takes an X argument by value and returns an object of type X. How many times will the destructor of X be invoked as the program runs to completion?

     
    //CountDestructorInvoc3.cc #include <iostream> using namespace std; class X { int n; public: X(int nn) : n(nn) {} ~X() { cout << "destructor invoked for x obj with n= " << n << endl; } }; X g(X x) { return x; } //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  4. In another variation on the programs shown in the previous homework problems, the function g() in line (A) below takes an argument of type X by reference and returns an object of type X. How many times will X's destructor be invoked as this program runs to completion?

     
    //CountDestructorInvoc4.cc #include <iostream> using namespace std; class X { int n; public: X(int nn) : n(nn) {} ~X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; X g(X& x) { return x; } //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  5. In yet another variation on the previous programs, the function g() in line (A) now takes its X argument by reference and also returns an X object by reference. How many times will X's destructor be now invoked as the program runs to completion?

     
    //CountDestructorInvoc5.cc #include <iostream> using namespace std; class X { int n; public: X( int nn ) : n(nn) {} X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; X g(X& x) { return x; } //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  6. In one last variation on the previous homework programs, the function g() in line (A) now takes its X argument by value. It then a returns a reference to the local variable x. Since, in general, returning a reference to a local variable is an error, will this program compile? If the program were to compile and it ran without problems, how many times would X's destructor be invoked?

     
    //CountDestructorInvoc6.cc #include <iostream> using namespace std; class x { int n; public: X( int nn ) : n(nn) {} X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; X& g(X x) { return x; } //(A) int main() { X xobj(100); g(xobj); //(B) return 0; }

  7. Provide implementations for the overloaded versions of the constructor, the member function setLastName(), and of the function modifyLastName() in the following code:

     
    class User { string firstName; string lastName; int age; public: User(string first, string last, int yy); //(A) User(const string& first, const string& last, int yy); //(B) void setLastName (string newLastName); void setLastName (string* newLastName); string getFirst(){ return firstName; } string getLast(){ return lastName; } }; User modifyLastName(User u, const string newName); //(C) User& modifyLastName(User& u, const string& newName); //(D) User* modifyLastName(User* p, string* const ptr); //(E)

    By inserting print statements in the implementations for the two constructors and the three version of the modifyLastName() function, find out what function calls will invoke which versions. In particular, state what you'd need to do to your program so that the version of the constructor in line (B) and the version of the function modifyLastName in line (D) will be invoked by the calls you see in the following main:

           int main()       {           User u1("joe", "schmo", 88);           User u2 = modifyLastName(u1, "sixpack");           User& u3 = u1;           User u4 = modifyLastName(u3, "smith");           User& u5 = modifyLastName(u3, "hardhat");           cout << "First name: " << u4.getFirst()                << " Last name: " << u4.getLast() << endl;           return 0;       } 
  8. The Java program shown below is a slight modification of the program Overload.java of Section 9.12. In the program of Section 9.12, the first call to foo in main invokes the second foo and, if we uncomment the commented out statement, the second call to foo results in a compile-time error. Now examine the code in the program shown below and answer the following question:

    Will any of the calls to foo in the lines (A), (B), and (C) elicit a compile-time error? Also, of the three overload definitions of foo provided, which foo will be selected for each call to the function by Java's overload resolution algorithm?

     
    //Overload2.java class Employee { String name; } class Manager extends Employee { int level; } class Test { // first foo: static void foo(Employee e1, Employee e2, double salary) { System.out.println ("first foo"); } // second foo: static void foo(Employee e, Manager m, int salary) { System.out.println ("second foo"); } // third foo: static void foo(Manager m, Employee e, int salary) { System.out.println ("third foo"); } public static void main(String [] args) { Employee emp = new Employee (); Manager man = new Manager (); foo(emp, man, 100.25); //(A) foo(emp, man, 100); //(B) foo(emp, man, 100.25); //(C) } }




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