UML and Class Inheritance


Suppose that you were designing a program that was to have two classes: Date and newDate so that newDate is derived from Date. Suppose further that Date was defined as the following class:

image from book

 class Date {   private:     short month,          day,          year;   public:     Date();     Date(short,short,short);     void setDate();     void setDay(short);     void setMonth(short);     void setYear(short);     short getDay();     short getMonth();     short getYear();     void showMonthName(); }; 

image from book

As was discussed in a previous lecture, the UML chart for Date would be the following:

image from book

Suppose further that the class newDate was to be defined as the following:

image from book

 class newDate : public Date {   public:     void setNewDate(short,short,short);     short lastDay(short,short);     bool leapYear(short);     long daysSince(short,short,short);     long f(short,short);     long g(short);     bool incorrectDate(short,short,short);     newDate operator +(short);     short operator -(newDate);     friend ostream &operator << (ostream &stream, newDate aDate);     friend istream &operator >> (istream &stream, newDate &aDate); }; 

image from book

The question then arises as how to create the UML diagram for the class newDate.

Using the UML diagram for the class Date, add an additional class graphic directly below the UML for Date. For the name of the class use: newDate:Date to indicate that newDate is derived from Date. Connect an arrow from the newDate diagram to the Date diagram as in the following:

image from book

Since newDate has no attributes, the UML chart must have the attribute rectangle empty. However there needs to be nine methods and the two friend functions added to the diagram. So the next step is to add the first seven methods to the diagram for newDate (Visio calls them operations). When you do, you should get a diagram like the following:

image from book

The next step is to add the two operator methods: operator +() and operator -() to the diagram. The operator +() returns an object of newDate and the operator -() has as a parameter an object of newDate. If you have not noticed in the UML diagrams when you created classes before, that as soon as a class is defined, the name of the class appears as a data type to be returned by the methods and as a data type that can be used for the parameters. For example look at the following as operator +() is being added to the diagram (it may be necessary to move your mouse to the column division lines at the top and to move some of the columns to make the words visible.)

image from book

For some reason, when you type in the name operator +, Visio will not allow you to put a space between operator and +. However, when you click on the Properties button to add the parameters, the name appears in the new dialog box and it is possible to add a space between the word operator and the + symbol.

In the following diagram, the two operators have been added:

image from book

To complete the UML diagram, friend operator <<() and friend operator >>() need to be added to the diagram. These operators have references to objects of ostream and istream as input and output. For some reason, ostream and istream do not seem to be listed as data types in Visio. So what can be done is to create these classes off the diagram and then they will be available as both for parameters and for return data types. Look on the left side of the Visio and you will see Top Package in the Model Explorer:

image from book

Do a right mouse button on Top Package. You will see the following options:

image from book

Select the Class option. For the class names use: istream & and ostream & respectively. While the & is not a part of the class names, I found it is easier to place the & here than it is for the return type and for the parameters. If you look again on the left side in the Model Explorer, you will find both ostream & and istream & appearing as classes but yet they do not appear in the UML diagram being created.

image from book

While you can see the istream & in the diagram above, the ostream & is listed but not view able in this diagram because it would be necessary to scroll down to make it visible and then istream& would not be view able.

Now that these data types have been added to the Top Package, the operators: friend operator <<() and friend operator >>() may be added as can be seen in the following diagram:

image from book

To show that newDate is publicly derived from Date, the word public may be added to the connecting arrow. This completes the required UML diagram for the base class: Date and the derived class: newDate:Date




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