Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. C++ in Plain English (Publisher: IDG Books Worldwide, Inc.) Author(s): Brian Overland ISBN: 1558284729 Publication Date: 04/01/96 Search this book:
Previous | Table of Contents | Next
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. | Member functions can be declared public, private, or protected, just as data members can. You can also declare data members as public. Bear in mind, though, that public data members become part of the interface; if the class is already being used, changing the interface can potentially introduce errors in the rest of the program. ### Another Example: Fast Cars and Inheritance Trees One 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;
    }
| Here, I’ve included two arrays of char (make and model) to store character strings. I’ve used the char type so that the example can be entered easily from scratch. However, if you have declared and compiled the code for the string class, CStr, this would be an ideal place to use it. The declarations of the first two CAuto members could be replaced by:
         CStr make;
         CStr model;
| Then assignments to make and model could be made through simple assignments rather than a call to strcpy. To use the CStr class, remember that you would have to include the CSTR.H header file in your source code and link in CSTR.OBJ. 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, let’s 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 didn’t 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 won’t be adding any functions to it. Figure 8.4 shows the resulting inheritance hierarchy. Figure 8.4 A class hierarchy for automobiles. ### Base-Class Constructors As you’ve 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 constructors—to 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;
    }
Previous | Table of Contents | Next
[an error occurred while processing this directive]

| 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.