Overriding Base Class Functions


In chapter 5, "Function and Operator Overloading," you saw how C++ enables you to override constructors, functions, and operators. It also lets you do the same thing between base and derived classes. You can override base class functions in derived classes. You do this when you want to enhance or change the way the base class function operates. Listing 6.4 demonstrates how to override base class functions in a derived class.

Note

The entire program for Listing 6.4 is on the CD in the folder \Source\Chapter06\Prog_06_04. It's in a file called Prog_06_04.cpp.


Listing 6.4. Overriding the Print() function in the point2d class

 1   #include <cstdlib> 2   #include <iostream> 3 4   using namespace std; 5 6   class point2d 7   { 8   public: 9       point2d(); 10      point2d(int xValue, int yValue); 11 12      void X(int xValue); 13      int X(void); 14      void Y(int yValue); 15      int Y(void); 16 17      void Print(); 18 19   private: 20      int x, y; 21   }; 22 23 24 25   inline void point2d::Print() 26   { 27      cout << x << "," << y; 28   } 29 30 31   class point3d : public point2d 32   { 33   public: 34       point3d(); 35       point3d(int xValue, int yValue, int zValue); 36 37       void Z(int zValue); 38       int Z(void); 39 40       void Print(); 41 42   private: 43       int z; 44   }; 45 46 47   void point3d::Print() 48   { 49       point2d::Print(); 50       cout << "," << z; 51   } 52 53   int main(int argc, char *argv[]) 54   { 55       point3d rightHere(40,50,60); 56 57       cout << "(x, y, z)=("; 58       rightHere.Print(); 59       cout << ")"; 60       cout << endl; 61 62       rightHere.X(10); 63       rightHere.Y(20); 64       rightHere.Z(30); 65 66       cout << "(x, y, z)=("; 67       rightHere.Print(); 68       cout << ")"; 69       cout << endl; 70 71       rightHere.X(30); 72       rightHere.Y(20); 73       rightHere.Z(10); 74 75       cout << "(x, y, z)=("; 76       rightHere.Print(); 77       cout << ")"; 78       cout << endl; 79 80       system("PAUSE"); 81       return (EXIT_SUCCESS); 82   } 

Listing 6.4 gives a modified version of the program from Listing 6.1. It doesn't actually present the entire program. To save some space and focus on the discussion at hand, I've omitted all of the member functions you saw in Listing 6.1. The only member functions that appear in Listing 6.4 are two functions that are both named Print(). The point2d and point3d classes each contain their own version of Print().

The version of Print() in the point2d class prints the value in the private data member x, then prints a comma, and finally prints the value in the private data member y. The version of Print() in the point3d class begins on line 47. It calls the Print() function in the point2d class. The style of function call you see on line 49 is how functions in derived classes call the functions in base classes that they override. The call must specify the name of the base class, followed by the scope resolution operator, and then the name of the function.

Note

Functions in derived classes that override base class functions do not have to call the base class functions they override. They often do, but it is not required.


As a result of calling the Print() function in the point2d class, the x and y values for the point are printed to the screen. After the Print() in the point3d class calls the Print() function in the point2d class, it prints a comma. It then prints the value in the private data member z. You can see on line 58 that the main() function calls the Print() function for the point3d class. Looking at just that one line of code, you cannot tell (nor should you care) that the Print() function in the point3d class invokes the Print() function in the point2d class. The fact that it gets its work done properly is all that counts. However, when you're writing the point3d class, it saves you time and effort to utilize the Print() function from the point2d class. Again, that's precisely the purpose of inheritance.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net