12.13 HOMEWORK


12.13 HOMEWORK

  1. The following program elicits an "undefined symbol problem" message from the linker. What's the problem with this program? How can the problem be fixed?

          #include <iostream>      using namespace std;      class X {           friend ostream& operator<<( ostream&, X& );      };      ostream& operator<<( ostream& os, const X& xobj ) {           os << "message from an X object" << endl;      }      int main()      {           X xob;           cout << xob;           return 0;      } 
  2. The following program has a flaw similar to the one you saw in the program of the previous problem, but it results in a different error message from the compiler. Why?

          #include <iostream>      using namespace std;      class X {           int n;      public:           X() : n(10) {}           friend ostream& operator<<( ostream&, X& );      };      ostream& operator<<( ostream& os, const X& xobj ) {           os << "n: " << xobj.n << endl;      }      int main()      {           X xob;           cout << xob;           return 0;      } 
  3. Define a function object that would allow a vector of strings to be sorted in a case-insensitive manner.

  4. Modify the program for the sorting example of Section 12.10 for the case when the object to be sorted has private data members.

  5. Modify the program of the previous problem to sort a vector of user-defined class-type objects.

  6. The program of Section 12.11 shows how a list of class-type objects can be sorted by merely supplying an overload definition for the operator ‘<’. Modify that code for the case of a vector of class-type objects.

  7. Pretending that a programmer would not need a copy constructor or a copy assignment operator and as an exercise in giving additional smarts to a smart pointer, write a smart pointer class for your own string type, for example for the MyString type presented in Section 12.6. In addition to the usual smarts of automatic freeing up of the memory occupied by the string owned by a smart pointer, also give it the following smarts: Whenever a string goes out of scope, it should get dumped into a string archive.

  8. Generalize the smart pointer of the previous question so that it can be used to point to either a single object or to an array of objects. Overload the increment and the decrement operators so that an array can be stepped through in the same manner as with a regular pointer. Make sure that you range check the pointer as it is incremented or decremented so that there is no range violation.




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