Chapter 12: Advanced Object-Oriented Concepts


Download CD Content

Chapter 11 introduced you to inheritance, the process whereby one class gets a copy of the public and protected methods of another class. In the previous two chapters, you have seen three of the four principle concepts of object-oriented theory. You have seen abstraction, encapsulation, and inheritance. This chapter will show you how to use the fourth principle, that of polymorphism. The word polymorphism literally means “many forms.” It simply means that once you have inherited a function, you can override the function you inherited and change it.

Polymorphism

When a class inherits a base class, it receives all the base class’s public and protected methods and properties. However, it does not have to take everything it gets as is. The derived class can override any function to suit the needs of the derived class. In other words, you are not stuck with the functions you inherit. You can override them. Overwriting a function is not the same as overloading a function. When you overload a function it must have different parameters (either a different number or different types). When you override a function it should have the same parameters, but the actual code in the function will be different. The function declaration (the name, return type, and parameters) is called the interface; the actual code in the function is called the implementation. Thus, your interface will be the same when you override an inherited function, but its implementation will be the same. The following example will clarify this.

Example 12.1

Step 1: Enter the following code into you favorite text editor. Save it as 12-01.h.

class baseclass { public:      void testfunction(); }; void baseclass::testfunction() {  cout << "Hey this is the base class!\n"; } class childclass:public baseclass { public:      void testfunction(); }; void childclass::testfunction() {  cout << "Hey this is in the child class!!\n"; } 

Step 2: Enter this code into your favorite text editor and save it as 12-01.cpp.

#include "test.h" #include <iostream> using namespace std;   int main() {  childclass myclass;  myclass.testfunction(); return 0; }

Step 3: Compile your code.

Step 4: Run your application, you should see something similar to what is shown in Figure 12.1.

click to expand
Figure 12.1: Overwriting an inherited function.

This example illustrates the basics of polymorphism. You can change the form of any function you inherit. As you can see in Example 12.1, the function that is called is the one in the derived class, not the one in the base class.

Although this does show you the basics of polymorphism, it does not show you the power of polymorphism. This comes into play when you have many derived classes, and each needs to implement a particular method in a slightly different way. Then, each class can overload the method inherited and have the method do what they wish. Consider this example.

Example 12.2

Step 1: Enter the following code into your favorite text editor and save it as 12-02.h.

class baseclass { public:       void computetax(float); }; void baseclass::computetax(float price) {  cout << "Hey this is the base class!\n";  cout << price * .075f; } class childclass1:public baseclass { public:      void computetax(float);      }; void childclass1::computetax(float price) {  cout << "Hey this is the child class 1\n";  cout << price * .010f << endl; } class childclass2:public baseclass { public:  void computetax(float);      }; void childclass2::computetax(float price) {  cout << "Hey this is the child class 2\n";  cout << price * .005f << endl; }

Step 2: Enter this code into your favorite text editor and save it as 12-02.cpp.

#include "test.h" #include <iostream> using namespace std;   int main() {  childclass1 myclass;  childclass2 anotherclass;  myclass.computetax(100);  anotherclass.computetax(100);  return 0; }

Step 3: Compile the code.

Step 4: Run the code, you should see something much like what is shown in Figure 12.2.

click to expand
Figure 12.2: Polymorphism with multiple derived classes.

As you can see, each of the child classes has a separate implementation for the function computetax. This is how polymorphism is generally used. You have different classes with a function of the same name, and even the same parameters, but with different implementations.

Hint!

You only need to redefine a function in the derived class if you intend to override it. If you will use it as it is in the base class then you do not need to take any action at all.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

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