Object Oriented Programming
using C++
INHERITANCE
LECTURE-19
1 Qasim M. Rajpoot
NUST School of Electrical Engineering and
Computer Science
WHAT TO STUDY
Inheritance Examples
Inheritance Syntax
Protected access specifier
Redefining member functions
2
INHERITANCE
New classes created from existing classes
Absorb attributes and behaviors
Derived class
Classthat inherits data members and member functions
from a previously defined base class
3
INHERITANCE
Inheritance is a mechanism for
Building class types from other class types
Defining new class types to be a ….
Specialization
Augmentation
of existing types
4
INHERITANCE EXAMPLES
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
5
THE STUDENT CLASS HIERARCHY
student
student_id, print()
year, name
inherits (isa)
graduate_student
dept, print()
thesis
6
INHERITANCE
A natural way to reuse code
Programming by extension rather than reinvention
Object-oriented paradigm is well-suited for this
style of programming
Terminology
Base class (superclass)
Derived class (subclass) Bicycle
is-a relationships
Mountain Racing Tandem
7
Bikes Bikes Bikes
CAN YOU BUILD INHERITANCE
HIERARCHY
OUT OF THIS VENN-DIAGRAM
8
INHERITANCE SYNTAX
The simplest example of inheritance requires two
classes: a base class and a derived class.
The base class OR super-class does not need
any special syntax.
The derived class OR subclass on the other
hand, must indicate that it’s derived from the
base class.
This is done by placing a colon after the
name of the derived class, followed by a
keyword such as public and then the base
class name. 9
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
10
as those of the base class (when some base members are
to be redefined)
INHERITANCE SYNTAX
class Teacher{ // Base class
private:
string name;
int age, numOfStudents;
public:
void setName (const string & new_name){
name = new_name;
}
};
class Principal : public Teacher{ // Derived class
string school_name;
int numOfTeachers;
public:
void setSchool(const string & s_name){
school_name = s_name;
}
}; 11
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 function from Parent
(Base), which is setName().
12
“PROTECTED” ACCESS
Wehave seen two access modes in C++ classes:
public and private
Public members are directly accessible by users of the
class
Private members are NOT directly accessible by users
of the class, not even by inheritors
There is a 3rd access mode: protected
Protected members are directly accessible by derived
classes but not by other users
13
EXAMPLE OF INHERITED CLASSES
class Shape { class Triangle: public Shape {
protected: public:
int width, height;
int area ( ) {
public:
void setDims (int a, int b){ return (width * height/2);
width=a; height=b;} }
}; };
class Rectangle: public Shape {
class Square: public Rectangle {
public:
public:
int area ( ) {
void setDims (int a){
return (width * height);
width=a; height=a;}
}
};
}; 14
ACCESS CONTROL FOR PUBLIC INHERITANCE
An object of a derived class inherits all the member
data and functions of the base class.
Private members
Not visible in the derived class.
The derived class may access them only through the public
interface of the base class.
Protected Data members
Visible in the derived class.
The derived class may access protected members directly.
15
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 not sufficient for the class
Principal, because principals have more properties to
be printed. So the print function must be redefined.
16
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;
};
void Teacher::print() const
{
cout << "Name: " << name<< " Age: " << age
<< endl;
cout << "Number of Students: " << numOfStudents
<< endl;
} 17
REDEFINING MEMBERS
class 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 << "Name: " << name << " Age: " << age << endl;
//cout << "Number of Students: " << numOfStudents << endl;
cout << "Name of the school: " << school_name << endl; 18
}
REDEFINING MEMBER FUNCTION OR
OVERRIDING
print() function of the Principal class overrides (hides) the
print() function of the Teacher class. Now the Principal class
has two print() functions. The members of the base class can
be accessed by using the scope resolution operator (::).
// Print method of Principal class
void Principal::print() const
{
//invoking the print function of the teacher class
Teacher::print();
cout << "Name of the school: " <<
school_name;
}
19
ACCESS CONTROL – PROTECTED
MEMBERS
A class can have public or private members
Once inheritance enters the picture, other
access possibilities arise for derived classes. In
addition to private and public members a base
class can also have protected members
Derived class inherits all members of base
class whether public, private, or protected.
Member functions of a derived class can
access public and protected members of the
base class, but not private members.
Objects of a derived class in main can access
only public members of the base class. 20
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
WHAT IS NEXT?
Inheritance continued…
22