Features Specific to Classes

Chapter 17 - Classes in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Derived Classes
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:
class derived-class-type :(public/private/protected) . . .
       
parent-class-type { . . . .};
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
//

#include <iostream.h>
#include <string.h>

char newline;

class consumer {
 char name[60],
      street[60],
      city[20],
      state[15],
      zip[10];
public:
 void data_output(void);
 void data_input(void);
};

void consumer::data_output( )
{
 cout << “Name: ” << name << endl;
 cout << “Street: ” << street << endl;
 cout << “City: ” << city << endl;
 cout << “State: ” << state << endl;
 cout << “Zip: ” << zip << endl;
}
void consumer::data_input( )
{
 cout << “Enter The Consumer’s Full Name: ”;
 cin.get(name,59,’\n’);
 cin.get(newline);      //flush carriage return
 cout << “Enter The Street Address: ”;
 cin.get(street,59,’\n’);
 cin.get(newline);
 cout << “Enter The City: ”;
 cin.get(city,19,’\n’);
 cin.get(newline);
 cout << “Enter The State: ”;
 cin.get(state,14,’\n’);
 cin.get(newline);
 cout << “Enter The Five Digit Zip Code: ”;
 cin.get(zip,9,’\n’);
 cin.get(newline);
}
class airline:public consumer {
 char airline_type[20];
 float acc_air_miles;
public:
 void airline_consumer( );
 void disp_air_mileage( );
};

void airline::airline_consumer( )
{
 data_input( );
 cout << “Enter Airline Type: ”;
 cin.get(airline_type,19,’\n’);
 cin.get(newline);
 cout << “Enter Accumulate Air Mileage: ”;
 cin >> acc_air_miles;
 cin.get(newline);      //flush carriage return
}
void airline::disp_air_mileage( )
{
 data_output( );

 cout << “Airline Type: ” << airline_type
      << endl;
 cout << “Accumulated Air Mileage: ”
      << acc_air_miles << endl;
}
class rental_car:public consumer {
 char rental_car_type[20];
 float acc_road_miles;
public:
 void rental_car_consumer( );
 void disp_road_mileage( );
};

void rental_car::rental_car_consumer( )
{
 data_input( );
 cout << “Enter Rental_car Type: ”;
 cin.get(rental_car_type,19,’\n’);
 cin.get(newline);      //flush carriage return
 cout << “Enter Accumulated Road Mileage: ”;
 cin >> acc_road_miles;
 cin.get(newline);
}
void rental_car::disp_road_mileage( )
{
 data_output( );

 cout << “Rental Car Type: ”
      << rental_car_type << endl;
 cout << “Accumulated Mileage: ”
      << acc_road_miles << endl;
}
main( )
{
 //associate variable names with classes
 airline jetaway;   
 rental_car varooom;

 //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.
class airline:public consumer {
 char airline_type[20];
 float acc_air_miles;
public:
 void airline_consumer(void);
 void disp_air_mileage(void);
};
This derived child class contains two functions, airline_consumer( ) and disp_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.
void airline::airline_consumer( )
{
 data_input( );
 cout << “Enter Airline Type: ”;
 cin.get(airline_type,19,’\n’);
 cin.get(newline);
 cout << “Enter Accumulated Air Mileage: ”;
 cin >> acc_air_miles;
 cin.get(newline);      //flush carriage return
}
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.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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