UMBC CMSC 202, Computer Science II, Spring 1999, Sections 0101, 0102, 0103, 0104

Project 4: Inheritance

Due: Monday April 26, 1999


Objective

The objectives of this project are 1) to practice using inheritance in C++, 2) to learn how to overload the input and output operators, and 3) to continue using C++ I/O features.


Background

Inheritance is a key concept in object-oriented programming. It facilitates the reuse of existing classes, and allows us to write applications that are more easily extended. Inheritance also has a natual correspondence to the way humans organize knowledge in hiearchical categories, with the broader or more general categories at the higher levels of the hierarchy.


Assignment

  1. Begin with your String class from Project 2. Make corrections and improvements as needed. Test your class thoroughly before proceeding. (You do not have to turn in your test program.)

  2. If you are not already using the bool type, change your String class so that boolean functions have return type bool rather than int. Use the bool type for any other boolean functions in this project.

  3. If any documentation is missing from your String.H, add it now. Keep all documentation updated as you continue to add new functions and new classes in this project.

  4. Change the private section to protected. Add the const qualifier to member functions that don't change the state of the host object. (Note that const must be added to both the function declaration and definition.) Your declaration of the String class should resemble the one shown below: class String { protected: char* _cptr; int _length; public: String(); String (char* str); String (const String& str); ~String(); inline int Length() const; void Display () const; String& operator= (const String& str); String operator+ (const String& str) const; bool operator== (const String& str) const; bool operator!= (const String& str) const; bool operator< (const String& str) const; bool operator> (const String& str) const; bool operator<= (const String& str) const; bool operator>= (const String& str) const; };

  5. Test your String class again before proceeding.

  6. Revise the Display() function so that it takes an ostream reference as a parameter, with a default value of cout. The declaration for the Display() function should now look like: void Display (ostream& os = cout) const;
  7. Add declarations for the overloaded insertion (output) and extraction (input) operators in the class body of your String class. friend ostream& operator << (ostream& os, const String& str); friend istream& operator >> (istream& os, String& str); Then add the function definitions into String.C. Implement the functions as follows:

  8. Test the new operators. Note that they can be used both for console (keyboard and screen) as well as disk file I/O. Verify, for example, that you can read some words from a file and write them back to another. ifstream infile ("input.txt"); ofstream outfile ("output.txt"); // error checks not shown String temp; infile >> temp; while (!infile.eof()) { outfile << temp; infile >> temp; } infile.close(); outfile.close();

  9. Write a new class called IString. Put the class body in IString.H and function definitions in IString.C. The IString class will be derived from String. Objects of the IString class are just like String objects, with one added piece of data: an integer value that is associated with the character string. There are number of possible uses for this type of object:

    The declaration of the IString class is shown below. Add the necessary documentation and define all of the member functions.

    class IString : public String { protected: int _value; public: IString(); IString (char* str, int value = 0); IString (const IString& str); ~IString (); inline int GetValue() const; // Returns _value // Display() prints the string followed by a space and then // the integer value in parentheses void Display (ostream& os = cout) const; IString& operator= (const IString& str); }; The IString class will inherit all of the relational operator functions as they exist in its base class. This means that the comparison of two IString objects is based only on examining the character strings, and that the integer values are not considered in the comparison. For now, the IString class does not need overloadings for the insertion and extraction operators.

  10. Test the IString class thoroughly. For debugging and learning purposes, you may want to put some temporary print statements in the various functions (especially constructors, destructor, and operator =) in both base and derived class. This will show you exactly when these functions are getting called. Remove (or comment out) these statements before submitting your project.

  11. Now write a main() function in a file called Proj4.C. Your main() should contain a loop that satisifies the following requirements:

    Output:

    0 (0) 1 (1) 10 (2) 11 (3) 100 (4) 101 (5) 110 (6) 111 (7) 1000 (8) 1001 (9) 1010 (10) 1011 (11) 1100 (12) 1101 (13) 1110 (14) 1111 (15)

  12. Submit a makefile and five source files named as follows:

    The name of your executable must be Proj4.


Extra Credit

For up to ten points extra credit:
  1. Write another version of main() which gets two integer values from the command line and uses these to determine the range of digit strings to generate. Put this main() in a source file called Proj4-ex.C. Set up your makefile so that with the single command make, it generates both the Proj4 executable and another executable called Proj4-ex. The output shown above would be generated by the command line: Proj4-ex 0 15 . Other command lines should generate approriate output.

  2. Your Proj4-ex program should include error-checking for:


Last Modified: 11 Apr 1999 21:10:19 EDT by Alan Baumgarten, abaumg1@cs.umbc.edu

Back up to Spring 1999 CMSC 202 Section Homepage