A derived class can be considered an extension or inheritance of an existing class. The original class is known as a base or parent class and the derived class as a subclass or child class. As such, a derived class provides a simple means for expanding or customizing the capabilities of a parent class, without the need for re-creating the parent class itself. With a parent class in place, a common interface is possible to one or more of the derived classes.
Any C++ class can serve as a parent class, and any derived class will reflect its description. The derived class can add additional features to those of the parent class. For example, the derived class can modify access privileges, add new members, or overload existing ones. When a derived class overloads a function declared in the parent class, it is said to be a virtual member function. Throughout the remainder of this book, you will see that virtual member functions are very important to the concept of object-oriented programming.
Derived Class Syntax
You describe a derived class by using the following syntax:
For example, in creating a derived class, you might write
class retirement:public consumer { . . . .};
In this case, the derived class tag is retirement. The parent class has public visibility, and its tag is consumer.
A third visibility specifier is often used with derived classes—protected. A protected specifier is the same as a private specifier with the added feature that class member functions and friends of derived classes are given access to the class.
Using Derived Classes
The following example is used to illustrate the concept of a derived class. The parent class collects and reports information on a consumer’s name, address, city, state, and ZIP code. Two similar child classes are derived. One derived child class maintains information on a consumer’s accumulated airline mileage, while the second child class reports information on a consumer’s accumulated rental car mileage. Both derived child classes inherit information from the parent class. Study the listing and see what you can discern about these derived classes.
// // dercls.cpp // C++ program illustrates derived classes. // The parent class contains name, street, city, // state, and zip information. Derived classes add // either airline or rental car mileage information // to parent class information. // Copyright (c) Chris H. Pappas and William H. Murray, 1998 //
//get airline information cout << “\n—Airline Consumer—\n”; jetaway.airline_consumer( );
//get rental_car information cout << “\n—Rental Car Consumer—\n”; varooom.rental_car_consumer( );
//now display all consumer information cout << “\n—Airline Consumer—\n”; jetaway.disp_air_mileage( ); cout << “\n—Rental Car Consumer—\n”; varooom.disp_road_mileage( );
return (0); }
In this example, the parent class is of type consumer. The private part of this class accepts consumer information for name, address, city, state, and zip code. The public part describes two functions, data_output( ) and data_input( ). You have seen functions similar to these used to gather class information in earlier programs. The first derived child class is airline.
This derived child class contains two functions, airline_consumer( ) anddisp_air_mileage( ). The first function airline_consumer( ) uses the parent class to obtain name, address, city, state, and ZIP code, and attaches the airline type and accumulated mileage.
Do you understand how the derived class is being used? A call to the function data_input( )is a call to a member function that is part of the parent class. The remainder of the derived class is involved with obtaining the additional airline type and accumulated mileage.
The information on accumulated air mileage can be displayed for a consumer in a similar manner. The parent class function data_output( ) prints the information gathered by the parent class (name, address, and so on) while disp_air_mileage( ) attaches the derived child class’ information (airline type and mileage) to the output. The process is repeated for the rental car consumer.
Thus, one parent class serves as the data-gathering base for two derived child classes, each obtaining its own specific information.
The following is a sample output from the program:
—Airline Consumer— Name: Peter J. Smith Street: 401 West Summit Avenue City: Middletown State: Delaware Zip: 19804 Airline Type: US AIR Accumulated Air Mileage: 55321.0
—Rental Car Consumer— Name: Harry Z. Beener Street: 511 West Pacific Road City: Longtown State: New York Zip: 25888 Rental Car Type: Audi Accumulated Road Mileage: 33446.5
Experiment with this program by entering your own database of information. You might also consider adding additional member functions to the consumer class.
Now that you have learned about the class structure, you are ready for a complete look at I/O in C++ in the next chapter.