C++ Inheritance

Inheritance fosters reuse by allowing an application to take an already-tested class and derive a class from it that inherits the properties the application needs

 

 

 

Example: Banking

 

Class Loan inherits acctNum, name, SSN, and currBal and so on from its base class Account, and adds its own specifics – origBal, rate. Classes CarLoan and Mortgage in turn are derived from the Loan class. In addition to the inherited data members of acctNum, name, SSN, currBal, origBal, and rate, classes CarLoan and Mortgage include VIN and address data members respectively.

 

Class Definitions

void CarLoan::printLoanInfo() const
{  // calling the overridden base class Loan version of printLoanInfo()
   cout << Loan::printLoanInfo() << “; VIN: “ << VIN << endl;
}

The following code fragment illustrates the declaration and definition of the classes Account, CarLoan and Mortgage.

/*****************************************************/
/*   declaration of base class Account                                             */
/*****************************************************/
class Account
{
   protected:
      char* acctNum;   // acct number
      char* name;        // owner name
      char* SSN;
      float currBal;

   public:
      Account(const char* const num, const char* const name,
                     const char* const ssn=0, const float bal=0);
      ~Account(){delete[] ID; delete[] name; delete[] SSN;}
      void deposit(const float val);
      void withdraw(const float val);

   friend ostream& operator<<(ostream&, const Account&);
};

/*****************************************************/
/*   declaration of derived class Loan                                             */
/*****************************************************/
class Loan : public Account
{
   protected:
     float origBal;
     float rate;

   public:
     Loan(const char* const num, const char* const name,
               const char* const ssn=0, const float bal=0, const float r=0);
     void setLoan(const float bal, const float rate);
     void printLoanInfo() const;
};

/*****************************************************/
/*   implementation of derived class Loan                                      */
/*****************************************************/
Loan::Loan(const char* const num, const char* const name,
           const char* const ssn, const float bal, const float r)
    : Account(num, name, ssn, bal), rate(r)
{}

void Loan::setLoan(const float bal, const float r) 
{
   origBal = bal;
   rate = r;
}

void Loan::printLoanInfo() const
{
   cout << “Loan amt: “ << origBal << “; int rate: “ << rate << endl;
}

/*****************************************************/
/*   declaration of derived class CarLoan                                        */
/*****************************************************/
class CarLoan : public Loan
{
     char* VIN;

   public:
     CarLoan(const char* const num, const char* const name,
                    const char* const vin, const char* const ssn=0,
                    const float bal=0, const float r=0);
     ~CarLoan() {delete[] VIN;}
     void setLoan(const float bal, const float rate,
                           const char* const vin);  // overloading
     void printLoanInfo() const;     // overriding Loan::printLoanInfo()
};

/*****************************************************/
/*   implementation of derived class CarLoan                                */
/*****************************************************/
CarLoan::CarLoan(const char* const num, const char* const name,
                               const char* char vin, const char* const ssn,
                               const float bal, const float rate)
       : Loan(num, name, ssn, bal, rate)
{
   VIN = new char[strlen(vin) + 1];
   strcpy(VIN, vin);
}

// overloading
void CarLoan::setLoan(const float bal, const float r,
                                      const char* const vin) 
{
   origBal = bal;
   rate = r;
   VIN = new char[strlen(vin) + 1];
   strcpy(VIN, vin);
}

// overriding
void CarLoan::printLoanInfo() const
{
   cout << Loan::printLoanInfo() << “; VIN: “ << VIN << endl;
}

/*****************************************************/
/*   declaration of derived class Mortgage                                      */
/*****************************************************/
class Mortgage : public Loan
{
     char* address;

   public:
     Mortgage(const char* const id, const char* const name,
                     const char* const addr, const char* const ssn=0,
                     const float bal=0, const float r=0);
     ~Mortgage() {delete[] address;}
     void setLoan(const float, const float, const char* const);
     void printLoanInfo() const; // overriding
};

/*****************************************************/
/*   implementation of derived class Mortgage                               */
/*****************************************************/
Mortgage::Mortgage(const char* const num, const char* const name,
                                  const char* const addr, const char* const ssn,
                                  const float bal, const float rate) 
        : Loan(num, name, ssn, bal, rate)
{
   address = new char[strlen(addr) + 1];
   strcpy(address, addr);
}

// overloading
void Mortgage::setLoan(const float bal, const float r,
                                        const char* const addr) 
{
   origBal = bal;
   rate = r;
   address = new char[strlen(addr) + 1];
   strcpy(address, addr);
}

// overriding
void Mortgage::printLoanInfo() const
{
   cout << Loan::printLoanInfo() << “; address: “ << address << endl;
}

Main function

int main()
{
   Mortgage m(”00561790”, “John”, “11 Main St.”,
                       “101-32-8437”, 200000, 6.5);
   Loan* loanPtr = &m;   // ok, every mortgage is a loan

   Loan l(”025872959”, “Alex”, “330-89-2345”, 2500, 3.5);
   Mortgage* mortPtr = &l;   // error, not every loan is a mortgage

   MortPtr = (Mortgage*)loanPtr;   // ok, loanPtr does in fact
                                                        // point to Mortgage m
   // ...

   return 0;
}

 

 

Calling Parent's constructor

 

 

Java versus C++... on simple Inheritance

Java uses a singly-rooted hierarchy, so all objects are ultimately inherited from the root class Object.
In C++ you can start a new inheritance tree anywhere, so you end up with a forest of trees.