< C++ Multi Inheritance 2 | Main | C++ Virtual Function 2 >| Site Index | Download >


 

 

 

 

 

 

MODULE 17 - THE C++ POLYMORPHISM 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

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

 

The C++ programming skills that should be acquired:

 

Able to understand and use:

 

  • Polymorphism concept.

  • Virtual function.

  • Late and early binding.

 

17.1    Introduction

  • Polymorphism is a technique that allows you to set a base object equal to one or more of its derived objects.

  • The interesting thing about this technique is that, after the assignment, the base acts in different ways, depending on the traits of the derived object that is currently assigned to it. The base object that acts in many different ways, hence the name "polymorphism," which translates literally to "many form."

  • Another way of looking at polymorphism:  A base class defines a certain number of functions that are inherited by all its descendants.  If you assign a variable of the derived type to one of its base, all the base's methods are guaranteed to be filled out with valid addresses of the pointers.

  • The issue here is that the derived object, by the fact of its being a descendant object, must have valid addresses for all the methods used in its base’s virtual method table (VMT).  As a result, you can call one of these methods and watch as the derived functions get called.

  • However, you cannot call one of the derived methods that do not belong to the base.  The base doesn't know about those methods, so the compiler won't let you call them.  In other words, the base may be able to call some of the derive functions, but it is still a variable of the base type.

  • A virtual method table, or VMT, is a table maintained in memory by the compiler; it contains a list of all the pointers to the virtual methods hosted by an object. If you have an object that is descended from, let say, TObject, the VMT for that object will contain all the virtual methods of that object, plus the virtual methods of TObject.

  • If some of the methods in a base class are defined as virtual, each of the descendants can redefine the implementation of these methods. The key elements that define a typical case of polymorphism are a base class and the descendants that inherit a base class methods. In particular, the fanciest type of polymorphism involves virtual methods that are inherited from a base class.

17.2   A Simple Program With Inheritance

  • Examine the program example named poly1.cpp, the basic program that will be use for our discussion in this Module.  The last program in this Module will illustrate the proper use of virtual functions.

1.     // program poly1.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a base class declaration

6.     // and the implementation part

7.     class   vehicle

8.     {

9.           int    wheels;

10.         float  weight;

11.         public:

12.             void  message(void)  // first message()

13.             {cout<<"Vehicle message, from vehicle, the base class\n";}

14.   };

15.   

16.  // a derived class declaration and implementation part

17.  class  car : public  vehicle

18.  {

19.         int   passenger_load;

20.         public:

21.             void   message(void)    // second message()

22.             {cout<<"Car message, from car, the vehicle derived class\n";}

23.  };

24.   

25.  class  truck : public  vehicle

26.  {

27.         int  passenger_load;

28.         float   payload;

29.         public:

30.             int  passengers(void) {return  passenger_load;}

31.  };

32.   

33.  class  boat : public  vehicle

34.  {

35.         int  passenger_load;

36.         public:

37.             int  passengers(void) {return  passenger_load;}

38.             void  message(void)     // third message()

39.             {cout<<"Boat message, from boat, the vehicle derived class\n";}

40.  };

41.   

42.  // the main program

43.  int  main()

44.  {

45.        vehicle  unicycle;

46.        car      sedan_car;

47.        truck    trailer;

48.        boat     sailboat;

49.      

50.        unicycle.message();

51.        sedan_car.message();

52.        trailer.message();

53.        sailboat.message();

54.       

55.       // base and derived object assignment

56.       unicycle = sedan_car;     

57.       unicycle.message();

58.      

59.       // system("pause”);

60.       return 0;

61.  }

 

61 Lines: Output:

 

C++ Polymorphism and inheritance program example

 

void  message(void)  // first message()

void   message(void)    // second message()

...

void  message(void)     // third message()

class  truck : public  vehicle

vehicle  unicycle;

car      sedan_car;

truck    trailer;

boat     sailboat;

unicycle = sedan_car;

unicycle.message();

17.3   Adding The Keyword virtual

1.     // program poly2.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a base class declaration

6.     // and the implementation part

7.     class   vehicle

8.     {

9.            int    wheels;

10.          float  weight;

11.          public:

12.             virtual void  message(void)

13.             // first message(), with virtual keyword

14.             {cout<<"Vehicle message, from vehicle, the base class\n";}

15.  };

16.   

17.  // the derived class declaration and implementation part

18.  class  car : public  vehicle

19.  {

20.         int   passenger_load;

21.         public:

22.             void   message(void)    // second message()

23.             {cout<<"Car message, from car, the vehicle derived class\n";}

24.  };

25.   

26.  class  truck : public  vehicle

27.  {

28.         int  passenger_load;

29.         float   payload;

30.         public:

31.             int  passengers(void) {return  passenger_load;}

32.  };

33.   

34.  class  boat : public  vehicle

35.  {

36.         int  passenger_load;

37.         public:

38.             int  passengers(void) {return  passenger_load;}

39.             void  message(void)     // third message()

40.             {cout<<"Boat message, from boat, the vehicle derived class\n";}

41.  };

42.   

43.  // the main program

44.  int   main()

45.  {

46.          vehicle   unicycle;

47.          car       sedan_car;

48.          truck     trailer;

49.          boat      sailboat;

50.        

51.         cout<<"Adding virtual keyword at the base class method\n";

52.         cout<<"-----------------------------------------------\n";

53.         unicycle.message();

54.         sedan_car.message();

55.         trailer.message();

56.         sailboat.message();

57.        

58.         // unicycle = sedan_car;

59.         // sedan_car.message();

60.        

61.        // system("pause");

62.        return  0;

63.  }

 

63 Lines: Output:

 

C++ Polymorphism and virtual function program example

 

  • But this program operates no differently than the last example. This is because we are using objects directly and virtual methods have nothing to do with objects, only with pointers to objects as we will see soon.

  • There is an additional comment in line 59 and 60 as shown below:

// unicycle = sedan_car;

// sedan_car.message();

  • Illustrating that since all four objects is of different classes, it is impossible to assign any object to any other object in this program with different result. We will soon see that some pointer assignments are permitted between objects of dissimilar classes.

  • Compile and run this program example to see if your compiler results in the same output as shown.

 

17.4    Using Object Pointers

1.     // program poly3.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a base class declaration

6.     // and the implementation part

7.     class   vehicle

8.     {

9.           int    wheels;

10.         float  weight;

11.         public:

12.             void  message(void)

13.             // first message()

14.             {cout<<"Vehicle message, from vehicle, the base class\n";}

15.   };

16.   

17.  // a derived class declaration and implementation part

18.  class  car : public  vehicle

19.  {

20.         int   passenger_load;

21.         public:

22.             void   message(void)    // second message()

23.             {cout<<"Car message, from car, the vehicle derived class\n";}

24.  };

25.   

26.  class  truck : public  vehicle

27.  {

28.         int  passenger_load;

29.         float   payload;

30.         public:

31.             int  passengers(void) {return  passenger_load;}

32.  };

33.   

34.  class  boat : public  vehicle

35.  {

36.         int  passenger_load;

37.         public:

38.             int  passengers(void) {return  passenger_load;}

39.             void  message(void)     // third message()

40.             {cout<<"Boat message, from boat, the vehicle derived class\n";}

41.  };

42.   

43.  // the main program

44.  int   main()   

45.  {

46.          vehicle *unicycle;

47.          car     *sedan_car;

48.          truck   *trailer;

49.          boat    *sailboat;

50.         

51.          cout<<"Omitting the virtual keyword. Using\n";

52.          cout<<"pointer variables, and new keyword\n";

53.          cout<<"-----------------------------------\n";

54.         

55.          unicycle = new vehicle;

56.          unicycle->message();

57.          sedan_car = new car;

58.          sedan_car->message();

59.          trailer = new truck;

60.          trailer->message();

61.          sailboat = new boat;

62.          sailboat->message();

63.         

64.          unicycle = sedan_car;

65.          unicycle->message();

66.         

67.         

68.          // system("pause");

69.          return  0;

70.  }

 

70 Lines: Output:

 

C++ Polymorphism and object pointer program output example

vehicle *unicycle;

car     *sedan_car;

truck   *trailer;

boat    *sailboat;

unicycle = new vehicle;

...

sedan_car = new car;

...

trailer = new truck;

...

sailboat = new boat;

17.5    A Pointer And A Virtual Function

1.     // program poly4.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a base class declaration

6.     // and the implementation part

7.     class   vehicle

8.     {

9.           int    wheels;

10.         float  weight;

11.         public:

12.             virtual void  message(void)

13.             // first message(), with virtual keyword

14.             {cout<<"Vehicle message, from vehicle, the base class\n";}

15.  };

16.   

17.  // a derived class declaration and implementation part

18.  class  car : public  vehicle

19.  {

20.         int   passenger_load;

21.         public:

22.             void   message(void)    // second message()

23.             {cout<<"Car message, from car, the vehicle derived class\n";}

24.  };

25.   

26.  class  truck : public  vehicle

27.  {

28.         int  passenger_load;

29.         float   payload;

30.         public:

31.             int  passengers(void) {return  passenger_load;}

32.  };

33.   

34.  class  boat : public  vehicle

35.  {

36.         int  passenger_load;

37.         public:

38.             int  passengers(void) {return  passenger_load;}

39.             void  message(void)     // third message()

40.             {cout<<"Boat message, from boat, the vehicle derived class\n";}

41.  };

42.   

43.  // the main program

44.  int   main()

45.  {

46.      vehicle *unicycle;

47.      car     *sedan_car;

48.      truck   *trailer;

49.      boat    *sailboat;

50.   

51.      cout<<"Re add the virtual keyword. Using\n";

52.      cout<<"pointer variables, and new keyword\n";

53.      cout<<"-----------------------------------\n";

54.   

55.      unicycle = new vehicle;

56.      unicycle->message();

57.         sedan_car = new car;

58.         sedan_car->message();

59.      trailer = new truck;

60.      trailer->message();

61.         sailboat = new boat;

62.         sailboat->message();

63.     

64.      unicycle = sedan_car;

65.      unicycle->message();

66.     

67.      // system("pause");

68.      return  0;

69.  }

 

69 Lines: Output:

 

C++ Polymorphism object pointer and virtual function program output sample

17.6    A Single Pointer To The Parent Class

1.     // program poly5.cpp

2.     #include  <iostream>

3.     using namespace std;

4.      

5.     // a base class declaration

6.     // and the implementation part

7.     class   vehicle

8.     {

9.           int    wheels;

10.         float  weight;

11.         public:

12.             void  message(void)

13.             // first message()

14.             {cout<<"Vehicle message, from vehicle, the base class\n";}

15.  };

16.   

17.  // a derived class declaration and implementation part

18.  class  car : public  vehicle

19.  {

20.         int   passenger_load;

21.         public:

22.             void   message(void)    // second message()

23.             {cout<<"Car message, from car, the vehicle derived class\n";}

24.  };

25.   

26.  class  truck : public  vehicle

27.  {

28.         int  passenger_load;

29.         float   payload;

30.         public:

31.             int  passengers(void) {return  passenger_load;}

32.  };

33.   

34.  class  boat : public  vehicle

35.  {

36.         int  passenger_load;

37.         public:

38.             int  passengers(void) {return  passenger_load;}

39.             void  message(void)     // third message()

40.             {cout<<"Boat message, from boat, the vehicle derived class\n";}

41.  };

42.   

43.  // the main program

44.  int   main()

45.  {

46.   

47.      cout<<"Omitting the virtual keyword. Using\n";

48.      cout<<"pointer variables, new and\n";

49.      cout<<"delete keyword\n";

50.      cout<<"-----------------------------------\n";

51.   

52.      vehicle   *unicycle;

53.      unicycle = new vehicle;

54.      unicycle->message();

55.       delete unicycle;

56.   

57.      unicycle = new car;

58.      unicycle->message();

59.        delete unicycle;

60.   

61.     unicycle = new truck;

62.     unicycle->message();

63.        delete unicycle;

64.   

65.     unicycle = new boat;

66.     unicycle->message();

67.       delete unicycle;

68.   

69.      // unicycle = sedan_car;

70.      // unicycle->message();

71.    

72.     // system("pause");

73.     return  0;

74.  }

 

74 Lines: Output:

 

C++ Polymorphism a pointer from main() program to the base or parent class

tenouk C++ polymorphism programming

 

 

 

 

 

 

 

 

 

 

 

Further C++ Polymorphism related reading:

 

  1. The source code for this tutorial is available in C++ Polymorphism source code.

  2. Visual C++ .NET programming tutorial.

  3. Check the best selling C/C++, Object Oriented and pattern analysis books at Amazon.com.

  4. Find a lot of information about C++ history and evolution at Bjarne Stroustrup site.

 

 

 

 

 

 

|< C++ Multi Inheritance 2 | Main | C++ Virtual Function 2 >| Site Index | Download |


C++ Polymorphism-virtual function:  Part 1 | Part 2