INHERITANCE
(OOP)
DR. D.D ALEBURU
Introduction
• Inheritance in C++ language is a core concept of object-
oriented programming (OOP) that allows you to create new
classes (derived classes or child classes) based on existing
classes (base classes or superclasses).
• It enables a subclass to inherit all the properties of the base
class and add some new features to itself.
• Let’s understand it clearly with the help of a real-life example:
• To begin with, let's assume that inheritance does not exist.
Then, consider the term animal, which consists of all types of
animals. Here, you will have to create classes for all different
types of animals, like Dogs, Cats, and Tigers. The methods
speed(), eating(), and running() will be the same for all three
classes. If we avoid inheritance while creating these classes,
then we have to write all these methods for each class as
shown below:
• You can see that this process leads to replication of the same
code 3 times. That is, we created 3 individual/ independent
classes, each of which has a repetition of the same methods.
This increases the redundancy in code and makes it prone to
error.
• In contrast, if inheritance existed, then we could avoid this
kind of situation by creating a parent class called Animal with
the three methods. You could then inherit the three classes
(Dogs, Cats, and Tigers) from the Animal class. This reduces
unnecessary repetitions
• The Is-A relationship
• Inheritance in C++ represents an is-a relationship between the
child and parent classes. This means that an object of a child
class is also an object of the parent class. For instance, if the
Car class is derived from the Vehicle class, you can say that a
car is-a vehicle, or if Cat is derived from Animal, you can also
say that a cat is-a(an) animal.
• Access Modes in C++ Inheritance
• Public Mode
• If the subclass is derived from a public base class, the public members
of the base class also become public in the derived subclass. Similarly,
the protected members of the based class become protected members
in the derived class too.
• Protected Mode
• If the subclass is derived from a protected base class, then both – the
public, as well as protected members of the base class, become
protected members in the derived class.
• Private Mode
• If the subclass is derived from a private base class, then both – the
public, as well as protected members of the base class, become private
members in the derived class.
• However, remember that the private members of the base class cannot
be directly accessed by the derived class.
• Syntax And Structure Of Inheritance In C++
• The structure of inheritance in C++ contains a child class, which inherits
the attributes and methods, and a parent class. The syntax for defining
a parent and child class in all types of inheritance in C++ is explained
below.
class ParentClassName {
// parent class definition
};
class ChildClassName : visibility_mode ParentClassName {
// child class definition
};
• Here,
• class: It is the keyword for creating a new class.
• ParentClassName: It is the name of the base class or parent class.
• ChildClassName: It is the name of the derived class or child class.
• Visibility_mode: It is the access specifier that determines how inherited
members are visible in the derived class (public, protected, or private).
• Note:
• If neither visibility mode is specified, it takes private as the
default mode.
• Private members of the base class never get inherited by the
child class.
• If the public members of the base class are privately inherited
by the child class, then they become the private members of
the child class. Thus, the public members of the base class are
only accessible by the member functions of the child class,
not by the object of the child class.
• Implementing Inheritance In C++
• When implementing inheritance in C++ programs, you must
clearly indicate which class is inheriting from the other class. The
syntax to be used when creating a child class that inherits the
properties of the base class is given below.
• Syntax For Implementing Inheritance In C++:
class <derived_class_name> : <access_specifier> <base_class_name>
{
// body of the child class - class definition
}
• Here,
• The class keyword indicates that we are creating a new class
and the colon symbol (:) is the indicator of inheritance in C++.
• base_class_name is the name of the base class.
• derived_class_name is the name of the class that will inherit
the properties of a base class.
• access_specifier defines the visibility mode through which the
derived class has been created (public, private, or protected
mode).
#include <iostream>
using namespace std;
// parent class
class Animal {
public:
void speak() {
cout << "Animal makes a sound" << endl;}
};
// child class
class Dog : public Animal {
public:
void speak() {
cout << "Dog barks" << endl;}
};
int main() {
Animal animal;
Dog dog;
animal.speak();
dog.speak();
return 0;
}
• Types Of Inheritance In C++
• There are five different types of inheritance in C++ based on
how the derived class inherits the base class features. These
five types are as follows:
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hybrid inheritance
• Hierarchical inheritance
• Single Inheritance in C++
• In single inheritance, a derived class inherits from a single
base class, i.e., a child class inherits from a single parent class.
As shown in the diagram above, class B (which is the subclass)
inherits the properties of class A, which is the only parent
class.
• Syntax For Single Inheritance In C++:
class BaseClass {
// Body of the class
};
class DerivedClass : access_modifier BaseClass {
// Body of the class
};
#include
using namespace std;
//Base class //Main function
class Mother { int main() {
public: //Create object of the class baby
void eat() { Baby b;
cout << "Eats.\n";
} //Calling members of the base class
void sleep() { b.eat();
cout << "Sleeps.\n"; b.sleep();
}
}; //Calling member of the derived class
b.cry();
//Derived class
class Baby : public Mother { return 0;
public: }
void cry() {
cout << "Cries.\n";
}
};
• Multiple Inheritance in C++
• In multiple inheritance, a subclass inherits from multiple base classes,
i.e., a child class inherits from more than one parent class. For example,
say there are two base classes, Animal and FlyingObject. The former
includes all types of animals, and the latter includes all types of flying
objects. Then, a Bird class can be said to inherit from both Animal and
FlyingObject classes.
class B1 {
// Base class members
};
class B2 {
// Base class members
};
class DerivedClass : access_modifier B1, access_modifier B2 {
// Derived class members
};
#include <iostream>
using namespace std;
class Animal {
public:
void animal(string name) {
cout << name << " is an animal.\n";}
};
class Aerial {
public:
void aerial(string name) {
cout << name << " can fly in the air.\n";}
};
class Bird : private Animal, private Aerial {
public: int main(){
void display(string name){ Bird B;
animal(name); B.display("Eagle");
aerial(name);} return 0;
}; }
class Base1 {
public:
float salary = 900;
};
class Base2 {
public:
float bonus = 100;
};
class Derived: public Base1, public Base2 {
public:
void sum() {
cout << "Your Total Salary is: " << (salary + bonus) << endl;
}
};
int main() {
// Creating an object of the derived class and calling the sum() function.
Derived x;
x.sum();
return 0;
}
• Multilevel Inheritance In C++
• In multilevel inheritance, a derived class becomes the base class for
another derived class. As shown in the diagram above, class C inherits
from class B, which in itself inherits from the class A.
• To better understand the concept, let's consider a simple example of
the human biological relationships where we have three classes- Child,
Parent, and Grandparents. Here, the Child class inherits from the
Parent class, and the Parent class inherits from the Grandparent class.
class B1 {
// Base class 1 members
};
class B2 : access_modifer B1 {
// Base class 2 members
};
class DerivedClass : access_modifier B2 {
// Derived class members
};
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle.\n";}
};
class FlyingObject : public Vehicle {
public:
FlyingObject(){
cout << "This vehicle can fly in the air.\n";}
};
class Aeroplane : public FlyingObject {
public:
Aeroplane() {
cout << "Aeroplane has wings.\n";}
};
int main(){
Aeroplane obj;
return 0;
}
#include
using namespace std;
//Base class //Main function
class Grandmother { int main() {
public: //Create object of the class baby
void bake() { Baby b;
cout << "Bakes.\n"; }
}; //Calling members of the base class
b.bake();
//Intermediate class
class Mother : public Grandmother { //Calling members of the intermediate class
public: b.eat();
void eat() { b.sleep();
cout << "Eats.\n";
} //Calling member of the derived class
void sleep() { b.cry();
cout << "Sleeps.\n"; }
}; return 0;
}
//Derived class
class Baby : public Mother {
public:
void cry() {
cout << "Cries.\n"; }
};
• Hybrid Inheritance In C++
• Hybrid inheritance in C++ is a combination of more than one
inheritance type within the same program. This is also called
virtual inheritance in C++. Since it is a combination of more
than one inheritance, there is no fixed syntax for it.
#include<iostream>
using namespace std;
class World {
public:
World() {
cout << "This is World!\n";
}
};
// Here is Single Inheritance.
class Continent: public World { // Here is multiple Inheritance.
public: class India: public Continent, public Country {
Continent() { public:
cout << "This is Continent\n"; India() {
} cout << "This is India!";
}; }
};
class Country {
public: int main() {
Country() { India myworld;
cout << "This is the Country\n"; return 0;
} }
};
include <iostream>
using namespace std;
class A : public Y, public Z{
class X { public:
public: int d;
int x;
void Product(){
void getX(){
cout << "Enter the value of x: "; getX();
cin >> x;} getY();
}; getZ();
cout << "The product of x, y, and z is: " <<
class Y : public X{ x * y * z << endl;}
public: };
int y;
void getY(){
int main(){
cout << "Enter the value of y: ";
cin >> y;} A obj;
}; obj.Product();
return 0;
class Z{ }
public:
int z;
void getZ(){
cout << "Enter the value of z: ";
cin >> z;}
};
Classwork
• You are asked to create a database for staff management in
anchor university. Use the diagram below to create your base
class and derived classes
• You are asked to create a database for students in anchor
university. Use the diagram below to create your base class
and derived classes