0% found this document useful (0 votes)
78 views9 pages

WWW - Cbsua.edu - PH: Central Bicol State University of Agriculture

The document discusses inheritance in object-oriented programming. Inheritance allows a class to inherit attributes and behaviors from a parent or base class. This allows code reuse and faster development time. There are different types of inheritance - public, private, and protected - which determine how accessible members of the base class are to subclasses. A class can inherit from multiple base classes using multiple inheritance. Inheritance models real-world is-a relationships, like a dog is-a animal.

Uploaded by

Jason Asor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views9 pages

WWW - Cbsua.edu - PH: Central Bicol State University of Agriculture

The document discusses inheritance in object-oriented programming. Inheritance allows a class to inherit attributes and behaviors from a parent or base class. This allows code reuse and faster development time. There are different types of inheritance - public, private, and protected - which determine how accessible members of the base class are to subclasses. A class can inherit from multiple base classes using multiple inheritance. Inheritance models real-world is-a relationships, like a dog is-a animal.

Uploaded by

Jason Asor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

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.

4.1 CONCEPT OF INHERITANCE


A class can inherit from zero or more base classes. A class with at least one base class is said to
be a derived class. A derived class inherits all the data members and member functions of all
of its base classes and all of their base classes, and so on. A class's immediate base classes are
called direct base classes. Their base classes are indirect base classes. The complete set of
direct and indirect base classes is sometimes called the ancestor classes. A class can derive
directly from any number of base classes. The base-class names follow a colon and are
separated by commas. Each class name can be prefaced by an access specifier. The same class
cannot be listed more than once as direct base class, but it can appear more than once in the
inheritance graph.

Base and Derived Classes


A class can be derived from more than one classes, which means it can inherit data and
functions from multiple base classes. To define a derived class, we use a class derivation list
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

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>

using namespace std;

// 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);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

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

Access Control and Inheritance

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 −

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no

A derived class inherits all base class methods with the following exceptions −

• Constructors, destructors and copy constructors of the base class.


• Overloaded operators of the base class.
• The friend functions of the base class.

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

• Protected Inheritance − When deriving from a protected base


class, public and protected members of the base class become protected members of
the derived class.
• Private Inheritance − When deriving from a private base
class, public and protected members of the base class become private members of
the derived class.

4.2 Multiple Inheritance

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>

using namespace std;

// Base class Shape


class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}

protected:
int width;
int height;
};

// Base class PaintCost


class PaintCost {
public:
int getCost(int area) {
return area * 70;
}
};

// 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();

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting


cout << "Total paint cost: $" << Rect.getCost(area) << endl;

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
};

class Dog : public Animal {


// bark() 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.

class Dog : public 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

Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present


between the two classes.
Here are some examples:

• 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

Example 1: Simple Example of C++ Inheritance

// C++ program to demonstrate inheritance

#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;

// Calling members of the base class


dog1.eat();
dog1.sleep();

// Calling member of the derived class


dog1.bark();

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.

// Calling members of the Animal class


dog1.eat();
dog1.sleep();

Learning Resources
1. CBSUA Website
2. Student Handbook
3. Fundamentals of Computing 1 (SY 2020-2021) Syllabus
4. Google.com
5. PowerPoint Presentation

Uploaded in this Google Drive Folder.

Tasks/Activities

• Lecture Discussion (Zoom/Google Meet) – Recorded


• PowerPoint Presentation
• Video Clips
• Quiz via google doc

Discussion Board

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.

Competency Checklist (Formative Assessment/Diagnostic)

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:

1. This is an individual activity.


2. Login to their institutional account
3. Check updated assessment

Tasks/Activities

• Answer the questionnaire


• Video presentation PowerPoint Presentation
• Video Clips

You might also like