|< C++ Data Encapsulation 7 | Main | C++ Inheritance 1a >|Site Index |Download |


 

 

 

 

 

MODULE 14

C++ INHERITANCE 1: CONCEPTS WITH CODE SAMPLES

 

 

 

 

 

 

 

 

 

My Training Period: yy hours.

 

The source code for this tutorial is available in C++ Inheritance source codes.

 

The C++ programming abilities that supposed to be acquired:

 

  • Able to understand the inheritance concept.

  • Able to understand and use base class (parent class).

  • Able to understand and use derived class (child class).

  • Able to understand the use of the preprocessor directive to avoid the multiple inclusion of the same file.

  • Able to understand the class hierarchy.

 

10.1   Introduction To Inheritance

  • Inheritance permits to re-use code from the already available classes, also gives you the flexibility to do modification if the old code doesn’t exactly fit the task of the new project.

  • It doesn’t make sense to start every new project with new classes from scratch since most of the code will certainly be repeated in several programs such as common tasks or routines.

  • You are less likely to make an error if you leave the original alone and only add new features to it.  Another reason for using inheritance is if the project requires the use of several classes which are very similar but slightly different.

  • In this Module, we will concentrate on the mechanism of inheritance and how to build it into a program.

  • C++ allows you to inherit all or part of the members variables and methods of classes, modify some, and add new ones that not available in the base class.

  • Keep in mind that in the real programming it is rare for developers to develop their own classes because every development tool or compilers will have abundant of-the-shelf class library that have been tested and ready to be used. For example Microsoft Foundation Class (MFC), .NET class library, graphic library etc.

14.2    The Starting Point

  • Examine the file named vehicle.h for simple class which we will use to begin our study of inheritance.  It consists of four simple methods which can be used to manipulate data pertaining to our vehicle.

1.    // a class declaration part, vehicle.h, header file

2.    // save and include this file in your next project

3.    // do not compile or run

4.     

5.    #ifndef  VEHICLE_H   //preprocessor directive

6.    #define  VEHICLE_H

7.     

8.    // class declaration part – the interface

9.    class Cvehicle

10. {

11.        protected:

12.            int  wheels;

13.            int  weight;

14.        public:

15.            void  initialize(int input_wheels, float  input_weight);

16.            int   get_wheels(void);

17.            float get_weight(void);

18.            float wheel_load (void);

19. };

20. #endif

 

20 Lines of codes

 

Program 14.1:  vehicle.h program, declaration of Cvehicleclass

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------------

 

C++ cvehicle class inheritance illustration

 

14.3    The Cvehicle Class Implementation – vehicle.cpp

1.       // program vehicle.cpp, implementation part,

2.       // compile without error, generating object file, do not run

3.       #include "vehicle.h"

4.        

5.       // class implementation part

6.       // initialize to data input by user

7.       void  Cvehicle::initialize(int input_wheels, float input_weight)

8.       {

9.          wheels = input_wheels;

10.        weight = input_weight;

11.    }

12.     

13.    // get the number of wheels of this vehicle

14.    int  Cvehicle::get_wheels()

15.    {

16.       return wheels;

17.    }

18.     

19.    // return the weight of this vehicle

20.    float Cvehicle::get_weight()

21.    {

22.       return  weight;

23.    }

24.     

25.    // return the load on each wheel

26.    float  Cvehicle::wheel_load()

27.    {

28.       return  (weight/wheels);

29.    }

 

29 Lines of codes

 

Program 14.2:  vehicle.cpp, implementation program of Cvehicle class

14.4    Using The Vehicle Class – The main() program

1.        // program transprt.cpp, the main program,

2.        // compile and run

3.         

4.        #include <iostream>

5.         using namespace std;

6.        #include "vehicle.h"

7.        // a user define header file and put it in the same folder as this program

8.         

9.       void main()

10.     {

11.          Cvehicle    car, motorcycle, truck, sedan_car;

12.         // 4 objects instantiated

13.          

14.         // data initialization

15.          car.initialize(4,3000.0);

16.          truck.initialize(20,30000.0);

17.          motorcycle.initialize(2,900.0);

18.          sedan_car.initialize(4,3000.0);

19.        

20.        // display the data

21.        cout<<"The car has "<<car.get_wheels()<< " tires.\n";

22.        cout<<"Truck has load "<<truck.wheel_load()<<" kg per tire.\n";

23.        cout<<"Motorcycle weight is  "<<motorcycle.get_weight()<<" kg.\n";

24.        cout<<"Weight of sedan car is "<<sedan_car.get_weight()<<" kg, and has

25.                                      "<<sedan_car.get_wheels()<<" tires.\n";

26.      

27.      // system("pause");

28.    }

 

28 Lines: Output:

 

C++ cvehicle class inheritance main program

 

Program 14.3:  transprt.cpp the main program

  • Inheritance uses an existing class and adds new functionalities to the new class, to accomplish another, possibly more complex task.

  • This program declares four objects of the Cvehicle class as shown in the code segment below:

Cvehicle    car, motorcycle, truck, sedan_car;

// 4 objects instantiated

  • Initializes them as shown below:

    // data initialization

    car.initialize(4, 3000.0);

    truck.initialize(20, 30000.0);

    motorcycle.initialize(2,900.0);

    sedan_car.initialize(4, 3000.0);

  • And prints out a few of the values.  Compile and run this program.

14.5    A Derived Class

class Ccar : public Cvehicle

 

1.       // another class declaration car.h

2.       // save and include this file in your project

3.       // do not compile or run.

4.        

5.       #ifndef   CAR_H    // to avoid car.h re-inclusion/multi inclusion

6.       #define   CAR_H

7.        

8.       #include  "vehicle.h"

9.        

10.     // a derived class declaration part

11.     // Ccar class derived from Cvehicle class

12.     class Ccar : public Cvehicle

13.     {

14.        int passenger_load;

15.        public:

16.           // this method will be used instead of the same

17.           // method in Cvehicle class - overriding

18.            void initialize(int input_wheels, float input_weight, int people = 4);

19.            int passengers(void);

20.     };

21.     

22.    #endif

 

22 Lines of codes

 

Program 14.4:     car.h program, declaration of derived class Ccar

14.6    A Derived Class Declaration

#include "vehicle.h"

class Ccar : public Cvehicle

         int wheels;

         int weight;

         int passenger_load;

         get_wheels( ) { }

         get_weight( ) { }

         wheel_load( ) { }

         initialize( ) { }

         passengers( ) { }

C++ Ccar class illustration

a very simple C++ class hierarchy

 

tenouk C++ on  object oriented: Inheritance principles

 

 

 

 

 

 

 

Further C++ inheritance related reading:

 

  1. TheVisual C++ .NET.
  2. Check the best selling C / C++ and object oriented books at Amazon.com.
  3. See MFC library class hierarchy chart here.
  4. The source code for this tutorial is available in C++ Inheritance source codes.
  5. Find a lot of information about C++ history and evolution at Bjarne Stroustrup site(http://www.stroustrup.com/).

 

 

 

 

|< C++ Data Encapsulation 7 | Main | C++ Inheritance 1a >|Site Index |Download |


C++ Class Inheritance: Part 1 | Part 2 |