Class Extension: An Exercise


Suppose your company has been programming with the class Date that is defined in the header file: date.h and its supporting resource file: date.cpp. Further suppose that there is a program that utilizes this class is: usingDates.cpp.

Suppose that your supervisor has assigned you a new job that requires work with dates. However, one of the features needed requires that the program output the dates like:

image from book

 February 24, 2006 

image from book

instead of

image from book

 The date is February 24, 2006 

image from book

that is used in the class Date.

After consideration of this modification, you have come to the conclusion that several additional properties would also be helpful. One choice would be to redefine the class Date that you have been working with. You know that you could write a definition for the class that would better serve your needs. However this is dangerous in that you may create a class definition that no longer works in some of the programs you have already written. Therefore you do not want to rewrite the class' definition because it already does some of the things you want. What is needed is a class extension.

You have decided to create a class: NewDate that is a serial inheritance extension of Date and that it has the following additional properties:

  • overload the operator: >> so that dates could be entered when you wrote cin >> aDate;

  • overload the operator: << so that the dates would appear as: February 24, 2004 when you wrote: cout << aDate;

  • overload the binary operator: +() so that the dates would appear as: dueDate = invoiceDate + 30;

  • overload the binary operator: -() so that the dates would appear as: daysOpen = invoiceDate - datePaid; where daysOpen is an int.

The assignment will be done by defining this class in the header file: newdate.h and define the methods in the resource file: newdate.cpp. In addition you need to create a test program: testNewDates.cpp that could ensure that all of the original features of the base class work as well as the new features of the derived class.

The first step in the design will be to view the UML diagram for the class Date. It is the following:

image from book

Notice how the private members are preceded by a - and the public members are preceded by a +. In particular look at those methods that have input and output. Notice how the properties are represented in the UML graphic. The next step is to design the UML diagram for the class newDate.

image from book

Notice the connection between the two classes and that it indicates a public extension. Notice that there are no attributes in the class newDate and that there are several methods as well as the two binary operators +() and -(). Recall that the class newDate is to overload the operators: << and >>. Since the data members are in the class Date and that they are private, therefore they are not accessible in the class newDate. So these friend operators are dependent on methods of Date to be defined. The definition of the class newDate is therefore stored in the header: newdate.h and the definitions of the methods of the class newDate as well as the operators << and >> are stored in the source file: newdate.cpp and the test program is testNewDates.cpp




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net