Multiple Inheritance


C++ offers complete support for inheritance, unlike some other object- oriented programming languages. Specifically this means that C++ supports multiple inheritance. This means that a derived class can inherit from more than one base class. This is not nearly as complicated as it sounds. To inherit multiple base classes, you simply separate them with commas as you see in the following example.

class derivedclass:public baseclass1,public baseclass2,    public baseclass3 { }

The following example will show you that multiple inheritance is not much different from single inheritance.

Example 12.3

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

class baseclass1 { public:      float computetax(float); }; float baseclass1::computetax(float amount) { return amount * .075f; } class baseclass2 { public:      float computededuction(float); }; float baseclass2::computededuction(float amount) { return amount - (amount *.10f); } class derivedclass:public baseclass1,public baseclass2 { }; 

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

#include "12-03.h" #include <iostream> using namespace std;   int main() {  derivedclass myclass;  float answer, amount;  cout <<"Enter the amount to tax \n";  cin>> amount;  answer = myclass.computetax(amount);  cout << "The tax is " << answer << endl;  cout << "Enter the amount to reduce \n";  cin >> amount; answer = myclass.computededuction(amount); cout << "The amount after deduction is "<< answer <<  endl; return 0; }

Step 3: Compile the code.

click to expand
Figure 12.3: Multiple inheritance.

As you can see in this example, you can use multiple inheritance to get functions you need from more than one base class. The entire cornerstone of object-oriented programming is code reusability. This can only be realized through the use of inheritance, both standard and multiple.

Multiple inheritance can be a powerful feature but it does pose some unique problems. Assume that you are inheriting from base class A and base class B, and both have a method called funca(). When your derived class calls funcA(), which function is it calling? The answer to this unique problem is to simply override any functions that both base classes have in common. Basically you use the techniques covered in Section 1 to override the function that is duplicated. The following example is a bit more complex and illustrates handling multiple inherited functions.

Example 12.4

Step1: Enter the following code into your favorite text editor and save it as 12_04.h.

class bankaccount { protected:       float balance;       float penalty;// for overdraft       float interestrate;       float fee; // for various transactions public:       float withdraw(float);       float deposit(float);       bankaccount(); }; bankaccount::bankaccount() {      balance =1000.00f;      penalty =25.00f;// for overdraft      interestrate=.02f;      fee =10.00f; // for various transactions } float bankaccount::deposit(float amount) { balance -= amount; return balance; } float bankaccount::withdraw(float amount) { balance +=amount; return amount; } class transferfunds { protected:       float fee; // transfer fee public:       bool transfermoney(bankaccount *source,bankaccount   *dest,float);   transferfunds(); }; transferfunds::transferfunds() {   fee = 20.00f; } bool transferfunds::transfermoney(bankaccount   *source,bankaccount *dest,float amount)    {  source->withdraw(amount);  dest->deposit(amount);  return true;    } class checking:public bankaccount,public transferfunds {   public:       void deductmonthlyfee(); }; void checking::deductmonthlyfee() { balance -= fee; } class savings:public bankaccount,public transferfunds { }; 
Hint!

You see that both the bankaccount class and the transferfunds class have a variable called fee. When other classes inherit from both bankaccount and transferfunds, they must override that variable. This is the problem with multiple inheritance. If you compile this code, as you see it you will get an error because the compiler cannot tell which fee to use in the deductfee function. Thus, we will override fee in the checking class and add a constructor to the checking class. It is essential to prototype it as well. The following is the constructor. checking::checking() { fee = 5.00f; } You probably noticed the function transfermoney, which takes some parameters you have not seen before. These are pointers to classes, which will be explained in detail later in this chapter.

Step 2: Enter the following code into your favorite text editor and save it as 12_04.cpp.

Hint!

Whereas the header file demonstrates the multiple inheritance that we wanted to examine, the code we are about to look at provides a more practical, functional example with a scaled-down bank account application.

#include "12-03.h" #include <iostream> using namespace std;   void menu(); int main() {      void menu();      return 0; }// end main void menu() {  int imenu;  char acct;  float amount, balance;  savings mysavings;  checking mychecking;  cout << "1. Deposit funds \n";  cout << "2. Withdraw funds \n";  cout << "3. Exit \n";  cout << "Please enter your choice \n";  cin >> imenu;  switch(imenu)  {   case 1:    cout <<"From checking (c) or savings (s) \n";        cin >> acct;    cout << "How much ? \n";       cin >> amount;        if(acct == 'c')       balance = mychecking.withdraw(amount);        else       balance = mysavings.withdraw(amount);        cout << "The new balance is " << balance << endl;        break;  case 2:    cout <<"From checking (c) or savings (s) \n";        cin >> acct;    cout << "How much ? \n";        cin >> amount;        if(acct == 'c')       balance = mychecking.deposit(amount);        else       balance = mysavings.deposit(amount);        cout << "The new balance is " << balance << endl;        break;  case 3:       return;  default:        cout << "Invalid choice \n"; }//end switch  menu();    }// end menu 

If you take the time to carefully examine this code you will see that much of the code consists of techniques we have previously examined. The real item here we wanted to examine involved overriding multiple inherited variables; this was done in the header file. However, the program provided gives you a basic scaled-down bank account program. With a little thought and time, you could easily grow this into a full- fledged banking account program.




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