11.18 HOMEWORK


11.18 HOMEWORK

  1. Suppose we have three classes X, Y, and Z in a C++ program. Let's say each class depends on the other two. In what order will you declare/define each class in the source code?

  2. Complete the following interleaved C++ classes by adding constructors (at least a no-arg and one other constructor for each class), copy constructors, assignment operators, and destructors.

         class Course;     class Student {     public:         string fn, In;              // First name, Last name         int s;                      // Semester         Student* rm;                // Array of roommate students         int numRm;                  // How many roommates         Course* c;                  // Array of courses that this                                     //            student is taking         int numC;                   // How many courses         .....     };     class Course {     public:         string t;                   // Title of the course         int n;                      // Course number         int maxS;                   // Maximum number of students                                     // allowed to take this course         Student* s;                 // Array of students taking the course         int numS;                   // Number of students taking the course         .....     }; 

  3. The following C++ program will compile and run without any problems. But it has a deadly flaw embedded in it? What is it? Also, what is the output of the program?

         #include <iostream>     using namespace std;     class X {     public:         X() { cout << "X's no-arg constructor invoked" << endl; }      };          class Y {         X* x;     public:         Y() {             cout << "Y's no-arg constructor invoked" << endl;             x = new X();         }     };     int main()     {         Y arr[5];         return 0;     } 

  4. The following version of the program of the previous problem also has a deadly flaw in it. What is it? Will it create a compile-time problem or a run-time problem?

         #include <iostream>     using namespace std;           class X {};     class Y {         X* x;     public:         Y() { cout << "no-arg constructor invoked" << endl; }         ~Y(){             cout << "destructor invoked" << endl;             delete x;         }     };     int main()     {         Y arr [5];         return 0;     } 

  5. Does the following program fix all problems with the programs in the previous two questions? If so, what is the output of the program?

         #include <iostream>     using namespace std;     class X {};     class Y {         X* x;     public:         Y() {             cout << "no-arg constructor invoked" << endl;             x = 0;         }         ~Y() {              cout << "destructor invoked" << endl;              if ( x != 0 ) delete x;         }     };     int main()     {         Y arr[5];         return 0;     } 
  6. If the intent in the following C++ program is for ptr to be a pointer to an array of X objects, is there anything wrong with the program? (Of course, the compiler has no way of knowing directly if you intended for ptr to be a pointer to a single X object or a pointer to an array of X objects. But a human can, by looking at the implementation of the destructor. How is that possible?)

         #include <iostream>     using namespace std;           class X {};     class Y {         X* ptr;         int n;     public:         Y() {             cout << "no-arg constructor invoked" << endl;             ptr = 0;         }                  ~Y(){             cout << "destructor invoked" << endl;             delete ptr;         }     };     int main()     {         Y y;         return 0;     } 
  7. What is the output of the following C++ program? Note that it does not use pointers anywhere.

         #include <iostream>     using namespace std;     class X {     public:         X() { cout << "X's no-arg constructor invoked" << endl; }         ~X() { cout << "X's destructor invoked" << endl; }     };           class Y {         X x;     public:         Y() { cout << "Y's no-arg constructor invoked" << endl; }         ~Y() { cout << "Y's destructor invoked" << endl; }     };     int main()     {         Y arr[5];         return 0;     } 

  8. In the C++ code shown here, we have gratuitously supplied the class Dog with a copy constructor. If this program is run, how many times will the print statement in the copy constructor be executed?

         #include <string>     #include <iostream>     using namespace std;     class Dog {         string name;         int age;     public:         Dog( string nam, int a ) : name( nam ), age( a ) {}         Dog( const Dog& dog ) : name( dog.name ), age( dog.age ) {             cout << "invoking Dog copy constructor" << endl;         }     };     class Person {         string name;         Dog dog;     public:         Person( string nam, Dog d ) : name( nam ), dog( d ) {}     };     int main()     {         Dog dog( "Fido", 4 );         Person p1( "Zaphod", dog );         Person p2 = p1;         return 0;     } 

  9. In the code of the previous question, is there any way to reduce the number of times the copy constructor of Dog is invoked?

  10. What would happen if we did not overload the output operator ‘<<’ for the Dog class in the following C++ example code?

         #include<iostream>     #include<string>     using namespace std;     class Dog {         string name;         int age;     public:         Dog( string nam, int a ) : name( nam ), age( a ) {}         friend ostream& operator<<(ostream& os, const Dog dog) {             os << "Dog's Name: " << dog. name                << " Dog's Age: " << dog.age;             return os;         }     };          class Person {         string name;         Dog dog;     public:         Person(string nam, Dog& d) : name(nam), dog(d) {}         friend ostream& operator<<(ostream& os, const Person per) {             os << "Name: " << per.name << " Dog: " << per. dog;             return os;         }     };     int main()     {         Dog dog("Fido", 4);         Person p("Zaphod", dog);         cout << p << endl;         return 0;     } 

  11. In the program of the previous question, is there any way for the overload definition of the output operator ‘<<’ for Person to print out its Dog's information without overloading the same operator for the Dog class? The Dog's data members must remain private.

  12. Is Person p1's dog the same object as Person p2's dog in line (A) of the C++ code shown below? If we change person p1's dog, as in line (B), will person p2's dog also change?

         #include<iostream>     #include<string>     using namespace std;     class Dog {         string name;         int age;     public:         Dog(string nam, int a) : name(nam), age(a) {}         friend ostream& operator<<(ostream& os, const Dog dog) {             os << "Dog's Name: " << dog.name                << " Dog's Age: " << dog.age;             return os;         }     };           class Person {         string name;         Dog dog;     public:         Person(string nam, Dog& d) : name(nam), dog(d) {}         void changeDog(Dog newDog) { dog = newDog; }         friend ostream& operator<<(ostream& os, const Person per){             os << "Name: " << per.name << " Dog: " << per.dog;             return os;         }     };     int main()     {         Dog dog1( "Fido", 4 );         Dog dog2( "Zoro", 8 );         Person p1("Zaphod", dog1);         cout << p1 << endl;         Person p2 = p1;                                          //(A)         cout << p2 << endl;         p1.changeDog(dog2);                                      //(B)                    cout << p1 << endl;         cout << p2 << endl;         return 0;     }   

  13. The class Person in the following C++ program has a vector data member, but is not provided with a copy constructor of its own. When we say

         Person p2 = p1; 

    The system has no choice but to use the default copy constructor for Person. Is the vector data member of p2 the same as that of p1, or is it a copy?

         #include <string>     #include <vector>     using namespace std;           class Dog {         string name;         int age;     public:         Dog(string nam, int a) : name(nam), age(a) {}         friend ostream& operator<<(ostream& os, const Dog dog) {             os << "Dog's Name: " << dog.name                << " Dog's Age: " << dog.age;             return os;         }     };           class Person {         string name;         vector<Dog> dogs;     public:         Person(string nam, vector<Dog> dv)                                     : name(nam), dogs(dv) {}         void getNewDog(Dog newDog) {             dogs.pop_back();             dogs.push_back(newDog);         }         friend ostream& operator<<(ostream& os, const Person per) {             os << "Name: " << per.name << "   Dogs:   " << endl;             printDogs(os, per);             return os;         }         friend void printDogs(ostream& os, const Person& per);     };     void printDogs(ostream& os, const Person& per) {           vector<Dog>::const_iterator p = per.dogs.begin();           while ( p != per.dogs.end() )                os << *p++ << endl;      // needs overloaded '<<' for Dog           os << endl << endl;     }     int main()     {         Dog dog1( "Fido", 4 );         Dog dog2( "Zoro", 8 );         Dog dog3( "Ninja", 10 );         vector<Dog> dogvec;         dogvec.push_back(dog1);         dogvec.push_back(dog2);         Person p1( "Zaphod", dogvec );         cout << p1 << endl;         Person p2 = p1;           // uses system-supplied copy const.         cout << p2 << endl;         p1.getNewDog(dog3);         cout << p1 << endl;         cout << p2 << endl;         return 0;     } 




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