WWW - Cbsua.edu - PH: Central Bicol State University of Agriculture
WWW - Cbsua.edu - PH: Central Bicol State University of Agriculture
Week 9 - 11
Chapter 4 INHERITANCE
Introduction
One of the most important concepts in object-oriented programming is that of inheritance.
Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality
and fast implementation time. When creating a class, instead of writing completely new data
members and member functions, the programmer can designate that the new class should
inherit the members of an existing class. This existing class is called the base class, and the new
class is referred to as the derived class. The idea of inheritance implements the is a relationship.
For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Inheritance is one of the core features of an object- oriented programming language. It allows
software developers to derive a new class from the existing class. The derived class inherits the
features of the base class (existing class). The technique of deriving a new class from an old
one is called inheritance. The old class is referred to as base class and the new class is referred
to as derived class or subclass. Inheritance concept allows programmers to define a class in
terms of another class, which makes creating and maintaining application easier. When writing
a new class, instead of writing new data member and member functions all over again,
programmers can make a bonding of the new class with the old one that the new class should
inherit the members of the existing class. A class can get derived from one or more classes,
which means it can inherit data and functions from multiple base classes.
to specify the base class(es). A class derivation list names one or more base classes and has
the form −
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a
previously defined class. If the access-specifier is not used, then it is private by default.
Consider a base class Shape and its derived class Rectangle as follows −
#include <iostream>
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
When the above code is compiled and executed, it produces the following result −
Total area: 35
A derived class can access all the non-private members of its base class. Thus base-class
members that should not be accessible to the member functions of derived classes should be
declared private in the base class.
We can summarize the different access types according to - who can access them in the
following way −
A derived class inherits all base class methods with the following exceptions −
Type of Inheritance
When deriving a class from a base class, the base class may be inherited through public,
protected or private inheritance. The type of inheritance is specified by the access-specifier
as explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used.
While using different type of inheritance, following rules are applied −
• Public Inheritance − When deriving a class from a public base class, public members of
the base class become public members of the derived class and protected members
of the base class become protected members of the derived class. A base
class's private members are never accessible directly from a derived class, but can be
accessed through calls to the public and protected members of the base class.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
A C++ class can inherit members from more than one class and here is the extended syntax −
class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would be given for every base class
and they will be separated by comma as shown above. Let us try the following example −
#include <iostream>
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape, public PaintCost {
public:
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
return 0;
}
When the above code is compiled and executed, it produces the following result −
Total area: 35
Total paint cost: $2450
The derived class inherits the features from the base class and can have additional features
of its own. For example,
class Animal {
// eat() function
// sleep() function
};
Here, the Dog class is derived from the Animal class. Since Dog is derived from Animal,
members of Animal are accessible to Dog.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
Inheritance in C++
Notice the use of the keyword public while inheriting Dog from Animal.
We can also use the keywords private and protected instead of public. We will learn about
the differences between using private, public and protected later in this tutorial.
is-a relationship
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
return 0;
}
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
Here, dog1 (the object of derived class Dog) can access members of the base class Animal.
It's because Dog is inherited from Animal.
Learning Resources
1. CBSUA Website
2. Student Handbook
3. Fundamentals of Computing 1 (SY 2020-2021) Syllabus
4. Google.com
5. PowerPoint Presentation
Tasks/Activities
A Facebook group chat will be created where the students can discuss their ideas and thoughts. Chat
in Facebook Group allow us to communicate between users in a chat channel. Virtual Learning Portal
(VLP) will be the main source of lessons on the topic. This platform will be utilized throughout the
course.
The student will be assessed by filling out a questionnaire in google doc regarding the lessons that
have been presented. This assessment will determine the knowledge that they’ve learned with the
Video Lectures Presentation.
Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
San Jose, Pili, Camarines Sur 4418
ISO 9001:2015 www.cbsua.edu.ph
TÜV-R 01 100 1934918
Instructions:
Tasks/Activities