![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
C++ in Plain English
In this case, the interface (the set of public functions) completely encapsulates the data. If you were to give the class to other programmers, there would probably be no reason to let them directly alter nLength in their derived classes. Instead, they should set data properly by using the cpy function. Providing direct access to pData or nLength could result in errors. Yet in other cases, it might be useful to make members visible to derived classes. Declaring members as protected provides maximum flexibility to derived classes while retaining most of the advantages of private access.
Another Example: Fast Cars and Inheritance TreesOne of the central ideas of inheritance is that you can declare a general type and then derive any number of more specific types from it. These types can be the basis for even more specific types, so a family tree of classes can be as elaborate as you like. For example, you might define a general class to store information about various cars: class CAuto { public: char make[20]; char model[20]; int year; int color_selection; // Constructors CAuto() { } CAuto(char mak[], char mod[], int y, int c); }; #include <string.h> CAuto::CAuto(char mak[], char mod[], int y, int c) { strcpy(make, mak); strcpy(model, mod); year = y; color_selection = c; }
CStr make; CStr model;
For cars in general, this information might be adequate. But for certain kinds of cars, you might want to store more information. With a sports car, you are interested in everything in the CAuto class (make, model, year, and color), but you might also want to keep track of horsepower and acceleration time from 0 to 60 mph. The following CSportsCar definition inherits CAuto members and adds some members of its own. CSportsCar : public CAuto { public: double horse-power; double accel_0_60; // Constructor CSportscar() { } }; You can say that sports cars (CSportsCar) are one subspecies of cars in general (CAuto). Another subspecies of CAuto might be station wagons (CWagon). With this class, other information might be appropriate, such as storage capacity and maximum number of passengers. CWagon : public CAuto { public: double storage; int passengers; // Constructor CWagon() { } }; So far, we have one base class (CAuto) with two derived classes (CSportsCar and CWagon). To make things more interesting, lets add one more class: CRaceCar. The first thing to do in designing the CRaceCar class is to decide where it fits in the inheritance hierarchy. (This is one of the basic principles of object-oriented design, by the way.) You can consider race cars, for the sake of this example, a subspecies of sports car. The most interesting difference is that a race car is driven on a racing track and is entered into competitions. For a race car, you want to store all the information that you would for sports cars in general as well as one other piece of information: the number of racing competitions won. CRaceCar : public CSportsCar { public: int races_won; }; Because the class is designed as part of an existing inheritance hierarchy, the amount of code to write is quite small. Most CRaceCar data members are simply inherited from existing classes. You may have noticed that I didnt include a constructor here, although I did for the other classes. As I pointed out in Chapter 5, A Touch of Class, usually it is a good idea to add a default constructor, particularly if there is any chance that you will add other constructors later. However, CRaceCar is a simple class and I wont be adding any functions to it. Figure 8.4 shows the resulting inheritance hierarchy.
Base-Class ConstructorsAs youve seen in this chapter, inheritance is a way of including class members (both data and functions) previously declared in another class. C++ supports a special feature base-class constructorsto support efficient initialization of these inherited members. The previous section introduced a sports-car class, CSportsCar, that inherits members from the CAuto class and adds its own. To this declaration, it would be nice to add a second constructor to initialize all the data members, including those inherited from CAuto. CSportsCar : public CAuto { public: double horse-power; double accel_0_60; // Constructors CSportsCar() { } CSportsCar(char mak[], char mod[], int y, int c, double hp, double a); }; The new constructor initializes all the data members, one by one. #include <string.h> CSportsCar::CSportsCar(char mak[], char mod[], int y, int c, double hp, double a) { strcpy(make, mak); strcpy(model, mod); year = y; color-selection = c; horse_power = hp; accel_0_60 = a; }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |