Inheritance
Inheritance
Why?
What?
How?
INHERITANCE
Inheritance is the process by which a new class known
as a derived class is created from another class, called
the base class.
A derived class automatically has all the member
variables and functions that the base class has and can
have additional member functions and/or additional
member variables.
The definition of a derived class begins like any other
class definition
but is followed by:
a colon
the reserved word public
Indicating inheritance
Example:
class HourlyEmployee : public Employee
NOTE:
The definition of the derived class
(HourlyEmployee) only lists the added
member variables. The member variables
defined in the base class (Employee) are
not re-defined. They are automatically
available to the derived class.
You do not give the declarations of the
inherited member functions except for
those whose definitions you want to
change (redefine).
Constructors in Derived Classes
C
Constructors in Derived Classes
A Destructor A
C Destructor C
PRIVATE MEMBER VARIABLES
#ifndef EMPLOYEE_H
#define EMPLOYEE_H #include <iostream>
using namespace std;
class Employee
{
public:
Employee();
~Employee();
Employee(double basic, double pension, double medical, double
tax);
double getBasic() const;
double getPension() const;
SOLUTION: using separate compilation
Interface or header file Employee.h (continued)
protected:
double BasicSalary;
double PensionAmt; //the pension amount that the employee pays.
//The company adds the same amount.
double MedicalAmt; //the medical aid amount that the employee pays.
double TaxPercentage;
};
#endif
SOLUTION: using separate compilation
Employee.cpp
//Employee.cpp
#include "Employee.h"
#include <iostream>
using namespace std;
Employee::Employee()
{
BasicSalary = 0;
PensionAmt = 0;
MedicalAmt = 0;
TaxPercentage = 0.00;
}
SOLUTION: using separate compilation
Employee.cpp(continued)
Employee::Employee(double basic, double pension, double medical, double tax)
{
BasicSalary = basic;
PensionAmt = pension;
MedicalAmt = medical;
TaxPercentage = tax;
}
Employee::~Employee()
{
//destructor – do nothing
}
SOLUTION: using separate compilation
Employee.cpp(continued)
double Employee::getBasic() const
{
return BasicSalary;
}
double Employee::getPension() const
{
return PensionAmt;
}
}
double Employee::getTax() const
{
return TaxPercentage;
}
SOLUTION: using separate compilation
Employee.cpp(continued)
Use the accessor functions to display the values of the member variables
of the instantiated object.
//test program
#include "Employee.h"
#include <iostream>
using namespace std;
int main()
{
Employee MrSmith(25110.00, 2350.00, 2400.00, 00.08);
cout.setf(ios::fixed);
cout.precision(2);
cout.setf(ios::showpoint);
cout << "An employee's salary details are as follows : " << endl <<
endl;
cout << "Basic salary : R" << MrSmith.getBasic() << endl;
SOLUTION: using separate compilation
Main.cpp (continued)
Manager.h:
//Manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include "Employee.h“
#include <iostream>
private:
double Allowance;
};
#endif
SOLUTION: using separate compilation
Implementation file Manager.cpp
//Manager.cpp #include "Employee.h"
#include "Manager.h"
#include <iostream> using namespace std;
Manager::~Manager()
{
//destructor – do nothing
}
SOLUTION: using separate compilation
Implementation file Manager.cpp (continued)
//overloaded constructor
double Manager::getAllowance() const
{ Return the value of the child
return Allowance; class member variable
void Manager::setAllowance(double a)
Changing the value of the
{ child class member variable
Allowance = a;
}
SOLUTION: using separate compilation
Implementation file Manager.cpp (continued)
Allowance));
}
void Manager::Display()
{
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << "Manager's salary details:" << endl << endl;
cout << "Basic Salary: R" << BasicSalary << endl;
cout << "Pension contribution: R" << PensionAmt << endl;
cout << "Medical Aid contribution amount: R" << MedicalAmt <<
endl;
cout << "Entertainment allowance: R" << Allowance << endl; cout
<< "% tax payable: " << TaxPercentage << endl;
}
Implementation: main.cpp
Testing the class Manager derived from class Employee in a
driver program.
Note that the implementation of the parent class (Employee.cpp)
must be included as part of the project files, and the header file
Employee.h must be included in the .h and .cpp files for class
Manager with the #include "Employee.h" directive.
N.B: the #include "Employee.h" directive should not be included
in the test implementation file main.cpp.
Employee.h and Employee.cpp need to be in the same folder as
the other files for class Manager.
11-40
Implementation: main.cpp
main.cpp:
//test program
#include "Manager.h“
#include <iostream>
using namespace std;
int main()
{
Manager theManager (60560.00, 5500.00, 2800.00, 00.14, 2100.00);
cout.setf(ios::fixed);
cout.precision(2);
11-41
Implementation: main.cpp
main.cpp (continued)
theManager.Display();
cout.setf(ios::showpoint);
cout << "A manager gets a net salary of R"
<<theManager.calcGrossPay() – theManager.calcTax()
- theManager.calcDeductions() << endl;
return 0;
}
11-42