Upload
mrvendre
View
221
Download
0
Embed Size (px)
Citation preview
8/7/2019 Lecture 19 (Inheritance)
1/22
INHERITANCELECTURE-19
Qasim M. Rajpoot
NUST School of Electrical Engineering
and Computer Science
1
Object Oriented
Programming using C++
8/7/2019 Lecture 19 (Inheritance)
2/22
WHAT TO STUDY
Inheritance Examples
Inheritance Syntax
Protected access specifierRedefining member functions
2
8/7/2019 Lecture 19 (Inheritance)
3/22
INHERITANCE
New classes created from existing classesy Absorb attributes and behaviors
Derived classy Class that inherits data members and member
functions from a previously defined base class
3
8/7/2019 Lecture 19 (Inheritance)
4/22
IINHERITANCENHERITANCE
Inheritance is a mechanism for
Building class types from other class types
Defining new class types to be a .
y Specialization
y Augmentation
of existing types
4
8/7/2019 Lecture 19 (Inheritance)
5/22
INHERITANCE EXAMPLES
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
5
8/7/2019 Lecture 19 (Inheritance)
6/22
THE STUDENT CLASS HIERARCHY
student
print()
graduate_student
print()
inherits (isa)
student_id,
year, name
dept,
thesis
6
8/7/2019 Lecture 19 (Inheritance)
7/22
A natural way to reuse code
y Programming by extension rather than
reinvention
y Object-oriented paradigm is well-suited forthis style of programming
Terminology
y Base class (superclass)
y Derived class (subclass)
INHERITANCE
Bicycle
Mountain
Bikes
Racing
Bikes
Tandem
Bikes
is-a relationships
7
8/7/2019 Lecture 19 (Inheritance)
8/22
CAN YOU BUILD INHERITANCE HIERARCHY
OUT OF THIS VENN-DIAGRAM
8
8/7/2019 Lecture 19 (Inheritance)
9/22
INHERITANCE SYNTAX
The simplest example of inheritance requires twoclasses: a base class and a derived class.
y The base class OR super-class does not needany special syntax.
y The derived class OR subclass on the otherhand, must indicate that its derived from thebase class.
This is done by placing a colon after the nameof the derived class, followed by a keywordsuch as public and then the base class name.
9
8/7/2019 Lecture 19 (Inheritance)
10/22
SYNTAX FOR INHERITANCE
class derivedClass : public baseClass {
private :
// Declarations of additional members, if needed.
public:
// Declarations of additional members, if needed.
protected:
// Declarations of additional members, if needed.
}
The derived class inherits from the base class: all public members,
all protected members (will see later)
The additional member functions defined can have the same name
as those of the base class (when some base members are
to be redefined)
10
8/7/2019 Lecture 19 (Inheritance)
11/22
class Teacher{ // Base classprivate:
string name;int age, numOfStudents;
public:void setName (const string & new_name){
name = new_name;}
};
class Principal : public Teacher{ // Derived classstring school_name;
int numOfTeachers;public:
void setSchool(const string & s_name){school_name = s_name;
}};
INHERITANCE SYNTAX
11
8/7/2019 Lecture 19 (Inheritance)
12/22
INHERITANCE SYNTAX
int main()
{
Teacher t1;
Principal p1;
p1.setName(" Principal 1");
t1.setName(" Teacher 1");
p1.setSchool(" Elementary School");
return 0;
} The p1 object can also access, in addition to its own
member function setSchool(), the member functionfrom Parent (Base), which is setName().
12
8/7/2019 Lecture 19 (Inheritance)
13/22
PROTECTED ACCESS
We have seen two access modes in C++classes:public and privatey Public members are directly accessible by
users of the classy Private members are NOT directly accessible
by users of the class, not even by inheritors
There is a 3rd access mode:protectedy Protected members are directly accessible by
derived classes but not by other users
13
8/7/2019 Lecture 19 (Inheritance)
14/22
EXAMPLE OF INHERITED CLASSES
class Shape {
protected:
int width, height;
public:
void setDims (int a, int b){
width=a; height=b;}
};
class Rectangle: public Shape {
public:
int area ( ) {
return (width * height);
}
};
class Triangle: public Shape {
public:
int area ( ) {
return (width * height/2);
}};
class Square: public Rectangle {
public:void setDims (int a){
width=a; height=a;}
};14
8/7/2019 Lecture 19 (Inheritance)
15/22
ACCESS CONTROL FOR PUBLIC INHERITANCE
An object of a derived class inherits all the
member data and functions of the base class.
y Private members
Not visible in the derived class. The derived class may access them only through the public
interface of the base class.
y Protected Data members
Visible in the derived class.
The derived class may access protected members directly.
15
8/7/2019 Lecture 19 (Inheritance)
16/22
REDEFINING MEMBERS
Some member functions of the base class may
not be suitable for the derived class. These
members should be redefined in the derived
class.
For example, assume that the Teacher class
has a print function that prints properties of
teachers on the screen. But this function is notsufficient for the class Principal, because
principals have more properties to be printed.
So the print function must be redefined. 16
8/7/2019 Lecture 19 (Inheritance)
17/22
REDEFINING MEMBERS
class Teacher{ // Base class
private:string name;
int age, numOfStudents;
public:
void setName (const string & new_name){
name = new_name;
}void print() const;
};
voidTeacher::print() const
{
cout
8/7/2019 Lecture 19 (Inheritance)
18/22
REDEFINING MEMBERSclass Principal : public Teacher{ // Derived class
string school_name;
int numOfTeachers;
public:
void setSchool(const string & s_name){
school_name = s_name;
}void print() const;
};
void Principal::print() const
{// We can write this only if base class has protected data members
//cout
8/7/2019 Lecture 19 (Inheritance)
19/22
REDEFINING MEMBER FUNCTION OR
OVERRIDING
print() function of the Principal class overrides (hides)the print() function of the Teacher class. Now thePrincipal class has two print() functions. The membersof the base class can be accessed by using the scoperesolution operator (::).
// Print method of Principal class
void Principal::print() const
{
//invoking the print function of the teacher class
Teacher::print();
cout
8/7/2019 Lecture 19 (Inheritance)
20/22
ACCESS CONTROL PROTECTED MEMBERS
A class can have public orprivate members
Once inheritance enters the picture, other accesspossibilities arise for derived classes. In addition toprivate and public members a base class can alsohave protected members
Derived class inherits all members of base classwhether public, private, or protected. Memberfunctions of a derived class can access publicand protected members of the base class, butnot private members.
Objects of a derived class in main can accessonly public members of the base class. 20
8/7/2019 Lecture 19 (Inheritance)
21/22
ACCESS CONTROL PUBLIC INHERITANCE
Access Specifier Accessible from Accessible from Accessible from
In Base Class Base Class Derived Class Outside Objects
public yes yes yes
protected yes yes no
private no no no`
21
8/7/2019 Lecture 19 (Inheritance)
22/22
WHAT IS NEXT?
Inheritance continued
22