This set of notes is adapted from that provided by “Computer Science – A Structured Programming...
-
date post
18-Dec-2015 -
Category
Documents
-
view
222 -
download
3
Transcript of This set of notes is adapted from that provided by “Computer Science – A Structured Programming...
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000.
InheritancInheritancee
Chapter 12
COMP 103 - Inheritance 2
Extending 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
COMP 103 - Inheritance 3
Figure 12-1
Case Study: Simple Polygons
COMP 103 - Inheritance 4
Polygon Base Class
Figure 12-2
COMP 103 - Inheritance 5
Figure 12-3, Part Iclass Polygon { protected: double area; double perimeter; public: Polygon() {}; ~Polygon() {}; void printArea(); void printPeri();}; // class Polygon
Class Definition of Polygon
COMP 103 - Inheritance 6
Private, 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”.
COMP 103 - Inheritance 7
Polygon : Function definition
void Polygon :: printArea (){ cout << "The area of your polygon is "
<< area << endl; } // Polygons printArea
void Polygon :: printPeri (){ cout << "The perimeter of your polygon is "
<< perimeter << endl;
} // Polygons printPeri
COMP 103 - Inheritance 8
Figure 12-3, Part III
class Rectangle : public Polygon { private: double sideA, sideB; void calcArea(); void calcPeri(); public: Rectangle(double a, double b);};
Rectangle Object & Polygon
COMP 103 - Inheritance 9
Derived Class Syntax
class derived_class_name : inheritance_type base_class_name
Example:class Rectangle : public Polygonsclass Rectangle : protected Polygonsclass 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. Constructorsb. Destructorsc. friend functionsd. Assignment operators
COMP 103 - Inheritance 10
Inheritance 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();};
InheritedData andFunctions
COMP 103 - Inheritance 11
Rectangle: Constructor and functions
Rectangle::Rectangle(double A, double B) { sideA = A; sideB = B; calcPeri( ); // update perimeter field calcArea( ); // update area field}
void Rectangle::calcPeri() { perimeter = 2*(sideA + sideB);}
void Rectangle::calcArea() { area = sideA * sideB;}
COMP 103 - Inheritance 12
Main( ) : using Rectangle type
void main() { Rectangle rect(3,4); rect.printArea( ); rect.printPeri( );}
COMP 103 - Inheritance 13
Figure 12-3, Part II
Polygon: Triangle Object
class Triangle : public Polygon { private: double sideA, sideB, sideC; void calcArea(); void calcPeri(); public: Triangle(double a, double b,
double c );};
COMP 103 - Inheritance 14
Inheritance 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();};
COMP 103 - Inheritance 15
Triangle: Constructor and Functions
Triangle::Triangle(double A,double B,double C){ sideA = A; sideB = B; sideC = C; calcPeri(); // update perimeter field calcArea(); // update area field}
void Triangle::calcPeri(){ perimeter = sideA + sideB + sideC;}
void Triangle::calcArea(){ // Compute the area of a triangle based on // the given side lengths}
COMP 103 - Inheritance 16
Main( ) : 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
COMP 103 - Inheritance 17
Base and Derived Class Access
Inheritance Type Base Access Type Derived Access Type
privateprivate inaccessible
protected private
public private
protectedprivate inaccessible
protected protected
public protected
publicprivate inaccessible
protected protected
public public
Note: This is confusing for C++ programmers
COMP 103 - Inheritance 18
Another Example
class EmpInfo{private:
int ID;protected:
int Salary;public:
char Name[200];};
Base Classclass EmpInfo : private SecurityEmp{ . . .}ID (private) inaccessibleSalary (protected) privateName (public) private
class EmpInfo : protected ImportantEmp { . . .};ID (private) inaccessibleSalary (protected) protectedName (public) protected
class EmpInfo : public RegularEmp{ . . .};ID (private) inaccessibleSalary (protected) protectedName (public) public
Derived Classes
COMP 103 - Inheritance 19
Inheritance 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!
COMP 103 - Inheritance 20
Constructors 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.
COMP 103 - Inheritance 21
Example: Employee Class (p.609-611) class Employee { protected: int id; public: Employee(int idIn);};
class SalaryEmp:public Employee { protected: int salary; public: SalaryEmp(int idIn, int sal);};
class HourlyEmp:public Employee { protected: float payrate, hours; public: HourlyEmp(int idIn, float rate);};
Figure 12-6(p.608)
Employee
id : integer
SalaryEmp
salary : integer
HourlyEmp
payRate : real
hours : real
COMP 103 - Inheritance 22
Constructors
Employee::Employee(int idIn) { id = idIn;}
SalaryEmp::SalaryEmp(int idIn, int sal) : Employee(idIn) { salary = sal;}
HourlyEmp::HourlyEmp(int idIn, float rate) : Employee(idIn) { payRate = rate; hours = 0.0;}
pass idIn to Employee(idIn) constructor
pass idIn to Employee(idIn) constructor
COMP 103 - Inheritance 23
Base-Class-Initialization
SalaryEmp::SalaryEmp(int idIn, int sal): Employee(idIn)
{
salary = sal;
}
Explicit “base-class-initialization” - Employee(idIn)
Inherited-class constructor SalaryEmp::SalaryEmp(int idIn, int sal) calls the explicit-value base-class constructor
Employee(idIn)
COMP 103 - Inheritance 24
Summary
Polygon Case Study Access types
Private, Protected, Public (Confusing part of C++)
Constructor initialization syntax Employee example