Lecture 9: Class Inheritance


Types of Extensions

The types of classes used so far do not take advantage of the power of classes. The power lies in the ability to build upon the classes that have already been created and debugged. For example, a class Date could be developed that would possess all of the properties that would be necessary to manipulate dates. Then other classes like Invoice could be defined to be based upon Date. Next a class Sales could be based upon the class Invoice. These classes could be placed in a parent/child relationship depicted by the following graphic:

image from book

This parent/child relationship is called a class extension. This particular type of extension listed in this example is called a serial class extension.

An example of a serial extension is: invclass.cpp. In this case one of the attributes of the class Invoice is an object of the class Date. A UML diagram for the two classes is:

image from book

Notice the composition graphic connection between the two classes. The 1 denotes that there is one attribute of the class Invoice that is an object of the class Date.

In the previous example, neither of the classes had an explicit constructor. In the example: bankdate.cpp both classes have explicit constructors. The class Date has only an explicit default constructor and the class BankAccount has a non-default explicit constructor. Remember that whenever an object of a class is defined, the constructor of that class is called. This is not only true for classes whose attributes are variables of the C++ data types but it is also true when the attributes are variables that are class objects. Therefore when an object of BankAccount is defined, an object of Date is also defined. Therefore in this example the definition of the object CheckingAccount of the class BankAccount calls the constructor BankAccount() but since the class BankAccount has as an attribute an object statementDate of the class Date, the constructor of Date is also called.

In addition to serial extensions, it is also possible to create another type of extension: multiple extensions. For example, suppose a class Employee could be created from classes: Date, People, Telephone, Address and Education that already existed. This relationship could be depicted as the following graphic:

image from book

As is apparent from these two examples, it is possible to define serial or even multiple extensions by using as data members of one class objects of other classes. Such extensions are called: nested classes or composition of classes. This type of an extension is sometimes called a "has-a" relationship or a "has-a" extension. In other words the class Invoice "has-a" Date.

 Note:  Some authors use the term: nested classes only if the definition of one class is contained entirely within the definition of another class. When an object of one class is an attribute, then this may be referred to as composition of classes.

In addition to this type of class extension there is another type called an "is-a" extension between the classes. In this type of relationship the objects of one class in addition to satisfying the properties of its class also satisfies all of the properties of another class. Such an object "is-a" object of the other class as well. This type of extension is called: inherited classes. Since inherited classes are the most important type of extension, the remainder of this lecture will be devoted to this type of extension.




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