programming.nbk: Home | Index | Next Page: C++ Class Static data and functions | Previous Page: C++ class friends


 C++ Class inheritance

http://www.codersource.net/cpp_tutorial_inheritance.html

Creating or deriving a new class using another class as a base is called inheritance in C++. The new class created is called a Derived class and the old class used as a base is called a Base class in C++ inheritance terminology.

The derived class will inherit all the features of the base class in C++ inheritance. The derived class can also add its own features, data etc., It can also override some of the features (functions) of the base class, if the function is declared as virtual in base class.

C++ inheritance is very similar to a parent-child relationship. When a class is inherited all the functions and data member are inherited, although not all of them will be accessible by the member functions of the derived class. But there are some exceptions to it too. Some of the exceptions to be noted in C++ inheritance are as follows.

There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class.

It is a well known fact that the private and protected members are not accessible outside the class. But a derived class is given access to protected members of the base class.

Let us see a piece of sample code for C++ inheritance. The sample code considers a class named vehicle with two properties to it, namely color and the number of wheels. A vehicle is a generic term and it can later be extended to any moving vehicles like car, bike, bus etc.,

    class vehicle //Sample base class for c++ inheritance tutorial
    {
     protected:
        char colorname[20];
        int number_of_wheels;
     public:
        vehicle();
        ~vehicle();
        void start();
        void stop();
        void run();
      };


     class Car: public vehicle //Sample derived class for C++ inheritance tutorial
     {
       protected:
          char type_of_fuel;
       public:
          Car();
     }; 

The derived class Car will have access to the protected members of the base class. It can also use the functions start, stop and run provided the functionalities remain the same.

In case the derived class needs some different functionalities for the same functions start, stop and run, then the base class should implement the concept of virtual functions.


http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/05.10.VirtualFns/html/text.html

Recall that public inheritance implies an "is a" relationship from child to parent

class Vehicle
{
public:
	Vehicle(char* regnum)
        : myRegNum(strdup(regnum))
        {}

	~Vehicle(void)
        { delete[] myRegNum; }

	void Describe(void)
        {
                cout << "Unknown vehicle, registration "
                << myRegNum << endl;
        }

protected:
	char* myRegNum;
};

class Car : public Vehicle
{
public:
	Car(char* make, char* regnum)
        : Vehicle(regnum), myMake( strdup(make) )
        {}

	~Car(void)
        { delete[] myMake; }

	void Describe(void)
        {
                cout << "Car (" << myMake
                     << "), registration "
		     << myRegNum << endl;
        }

protected:
	char* myMake;
};

One consequence of that relationship is that a base-class pointer can point at a derived object without needing a cast

Vehicle* vp1 = new Vehicle ("SGI 987");
Vehicle* vp2 = new Car ("Jaguar","XJS 012");

vp1->Describe();		// PRINTS "Unknown vehicle....."
vp2->Describe();		// PRINTS "Unknown vehicle....."

Likewise, a base-class reference can refer to a derived object without a cast

Vehicle v1 ("SGI 987");
Car	c1 ("Jaguar","XJS 012");

Vehicle& vr1 = v1;
Vehicle& vr2 = c1;

vr1.Describe();		// PRINTS "Unknown vehicle....."
vr2.Describe();		// PRINTS "Unknown vehicle....."

The problem is that when the compiler is working out which Describe() member function to call, it selects according to the type of the pointer (Vehicle* or Vehicle& in both cases), rather than the type of the object being pointed at or referred to (Vehicle or Car)

So all the above calls to a Describe() member are dispatched to Vehicle::Describe(), even when the pointer of reference actually points to a Car object!

What we need here is polymorphism.


programming.nbk: Home | Index | Next Page: C++ Class Static data and functions | Previous Page: C++ class friends


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time