Title: Inheritance
1Inheritance
Chapter 12
2Extending Class from other classes
- We have seen basic class functions
- Data hiding (private, protected, public)
- Friend Functions
- Overloading (functions, operators)
- One of the most powerful elements of ADT is the
ability to derive classes from other classes - This provides the ability to create new classes
while retaining the basic characteristics of the
original - This concept is called Inheritance
3Case Study Simple Polygons
Figure 12-1
4Polygon Base Class
Figure 12-2
5Class Definition of Polygon
Figure 12-3, Part I
class Polygon protected double area
double perimeter public Polygon()
Polygon() void printArea() void
printPeri() // class Polygon
6Private, Protected and Public
- Private data and functions
- Can only be accessed within the class
- Protected data and functions
- Can be accessed within the class as well as the
derived class from this class - Public data and functions
- Can be accessed by everyone.
7Polygon Function definition
void Polygon printArea () cout ltlt "The
area of your polygon is " ltlt area ltlt endl
// Polygons printArea void Polygon printPeri
() cout ltlt "The perimeter of your polygon
is " ltlt perimeter ltlt endl // Polygons
printPeri
8Rectangle Object Polygon
Figure 12-3, Part III
class Rectangle public Polygon private
double sideA, sideB void calcArea()
void calcPeri() public Rectangle(double
a, double b)
9Derived Class Syntax
- class derived_class_name inheritance_type
base_class_name - Example
- class Rectangle public Polygons
- class Rectangle protected Polygons
- class Rectangle private Polygons
- Inheritance rules
- All data members of the base object are inherited
- All function members of the base object are
inherited, except a. Constructors b.
Destructors c. friend functions d. Assignment
operators
10Inheritance of data and functions
class Rectangle public Polygon private
double sideA, sideB void calcArea()
void calcPeri() protected double area
double perimeter public Rectangle(double
a, double b) void printArea() void
printPeri()
11Rectangle Constructor and functions
RectangleRectangle(double A, double B)
sideA A sideB B calcPeri( ) // update
perimeter field calcArea( ) // update area
field void RectanglecalcPeri() perimeter
2(sideA sideB) void RectanglecalcArea()
area sideA sideB
12Main( ) using Rectangle type
void main() Rectangle rect(3,4)
rect.printArea( ) rect.printPeri( )
13Polygon Triangle Object
Figure 12-3, Part II
class Triangle public Polygon private
double sideA, sideB, sideC void calcArea()
void calcPeri() public Triangle(double
a, double b, double c )
14Inheritance of data and functions
class Triangle public Polygon private
double sideA, sideB, sideC void
calcArea() void calcPeri() protected
double area double perimeter public
Triangle(double a, double b, double c) void
printArea() void printPeri()
15Triangle Constructor and Functions
TriangleTriangle(double A,double B,double C)
sideA A sideB B sideC C calcPeri()
// update perimeter field calcArea() //
update area field void TrianglecalcPeri()
perimeter sideA sideB sideC void
TrianglecalcArea() // Compute the area of a
triangle based on // the given side lengths
16Main( ) using Triangle type
void main() Triangle tri(3, 4, 5)
tri.printArea() tri.printPeri()
void main() Rectangle rect(3, 4)
rect.printArea() rect.printPeri()
compared with
17Base and Derived Class Access
Note This is confusing for C programmers
18Another Example
Derived Classes
class EmpInfo private SecurityEmp . . . ID
(private) ? inaccessible Salary (protected) ?
private Name (public) ? private
Base Class
class EmpInfo private int ID protected int
Salary public char Name200
class EmpInfo protected ImportantEmp . .
. ID (private) ? inaccessible Salary
(protected) ? protected Name (public) ?
protected
class EmpInfo public RegularEmp . . . ID
(private) ? inaccessible Salary (protected) ?
protected Name (public) ? public
19Inheritance types (p.601)
- For all types, Private member data/functions in
the base class are inaccessible in the derived
class - Private (default type)
- Protected and public data/functions in the base
class become private in the derived class - Protected
- Protected and public data/functions in the base
class become protected in the derived class - Public
- Protected and public types are preserved!
20Constructors and Derived Types
- Since Constructors are not inherited, derived
types must declare their own constructors, e.g.
Polygon case. - When a derived class is constructed, it must
first execute the constructor for the base class - Sometimes we need to pass data to the base class
constructor (initialize constructor). How? - ? with base-member-initialization list, e.g.
Employee case.
21Example Employee Class (p.609-611)
class Employee protected int id
public Employee(int idIn) class
SalaryEmppublic Employee protected
int salary public SalaryEmp(int idIn,
int sal) class HourlyEmppublic Employee
protected float payrate, hours
public HourlyEmp(int idIn, float rate)
22Constructors
EmployeeEmployee(int idIn) id
idIn SalaryEmpSalaryEmp(int idIn, int sal)
Employee(idIn) salary
sal HourlyEmpHourlyEmp(int idIn, float
rate) Employee(idIn) payRate rate
hours 0.0
pass idIn to Employee(idIn)
constructor
pass idIn to Employee(idIn)
constructor
23Base-Class-Initialization
SalaryEmpSalaryEmp(int idIn, int sal)
Employee(idIn) salary sal
- Explicit base-class-initialization -
Employee(idIn) - Inherited-class constructor
SalaryEmpSalaryEmp(int idIn, int sal) - calls the explicit-value base-class
constructor Employee(idIn)
24Summary
- Polygon Case Study
- Access types
- Private, Protected, Public (Confusing part of
C) - Constructor initialization syntax
- Employee example