Lec4 Inheritance
Lec4 Inheritance
1
In this class, we will cover:
• Introduction to inheritance
• Derived class and base class
• Derived class constructors
• Implementing Single level, multilevel, and multiple
inheritance
2
Introduction
• Inheritance is another strong feature of object-oriented
programming
• Inheritance is the process of creating new classes, called
derived classes, from existing or base classes.
• The derived class inherits all the capabilities of the base class
but can add new properties and methods, and refinements of
its own.
• Inheritance permits code reusability, once a class has been
written and tested, it can be adopted by other programmers.
This is basically created by defining the new classes, reusing the
properties of existing ones.
• Inheritance is often referred to as IS-A relationship because very
object of the class being defined "is" also an object of inherited
class.
3
Real World Example of
Inheritance
• Consider a transportation company that operates various types of vehicles, such as cars, trucks, buses,
and motorcycles.
• Each type of vehicle shares some common characteristics, such as a vehicle identification number
(VIN), make, model, and year of manufacture.
• However, each type of vehicle also has unique features and functionalities specific to its category.
• In this scenario, inheritance can be used to model the relationship between the different types of
vehicles. Here's how it could be structured:
Base Class: Vehicle
Properties: VIN, make, model, year
Methods: getters and setters for properties, displayVehicleInfo()
Derived Classes: Car, Truck, Bus, Motorcycle inherit from the Vehicle base class.
Additional properties and methods specific to each type of vehicle:
Car: number of doors, fuel type, trunk capacity
Truck: cargo capacity, towing capacity, number of axles
Bus: passenger capacity, route number, driver's name
Motorcycle: engine displacement, type of motorcycle (e.g., sport, cruiser)
• Inheritance allows for defining shared properties and methods in a base class (Vehicle), while unique
characteristics of each vehicle type are specified in derived classes, fostering code reuse.
• This approach centralizes common functionalities such as displaying vehicle information in the base
class, streamlining their implementation across all derived classes.
• Exercise: Draw inheritance diagram
4
Defining Derived Classes
• A derived class is specified by defining its relationship with the base
class in addition to its own details.
• The general syntax of defining a derived class is as follows:
class derived_classname:Access_specifier base_classname{
__
__ // members of derived class
};
• The colon indicates that the derived_classname is derived
from the base_classname.
• The access specifier is optional and, if present, may be public,
private or protected. By default it is private.
• Visibility mode describes the status of derived features
• When you derive features (i.e., member variables and member
functions) from a base class into a derived class, you can specify the
access level of those derived features using public, private, or
protected inheritance.
5
Status of Derived Features
• Public Inheritance: In public inheritance, all public members of the base
class remain public in the derived class, all protected members of the base
class remain protected in the derived class, and all private members of the
base class remain inaccessible in the derived class. This means that the
interface of the base class is preserved in the derived class.
6
Example 1 // Derived class using protected
using namespace std;
inheritance
// Base class representing a student class Graduate : protected Student {
class Student { public:
public: int researchTopicID;
string name; void displayResearchTopic() {
cout << "Research Topic ID: " <<
void display() {
researchTopicID << endl;
cout << "Name: " << name << endl; }
} };
};
// Derived class using public inheritance // Derived class using private
inheritance
class Undergraduate : public Student {
class PhDStudent : private Student {
public: public:
int year; string supervisor;
void displayYear() { void displaySupervisor() {
cout << "Year: " << year << endl; cout << "Supervisor: " << supervisor
<< endl;
}
}
}; }; 7
Example 1 Cont.
int main() { // Using private inheritance
// Using public inheritance PhDStudent phd;
phd.name = "Bob"; // Error: 'name'
Undergraduate ugrad; is inaccessible
ugrad.name = "John"; phd.supervisor = "Dr. Smith";
ugrad.year = 2; phd.displaySupervisor();
ugrad.display(); return 0;
ugrad.displayYear(); }
8
Example 1 Cont.
In this example:
• Undergraduate class inherits publicly from Student, so name remains
accessible in Undergraduate and can be used directly.
• Graduate class inherits protectedly from Student, so name becomes
protected within Graduate, meaning it can't be accessed directly from
outside Graduate.
• PhDStudent class inherits privately from Student, so name becomes private
within PhDStudent, making it inaccessible from outside PhDStudent.
9
Example 1 Cont.
• By adding using Student::displayName; in both Graduate and
PhDStudent classes, we explicitly make the displayName() function accessible
in these derived classes, allowing us to call it without errors.
10
Example 1 Cont.
// Derived class using private inheritance
class PhDStudent : private Student {
public:
using Student::name;
using Student::displayName; // Making displayName accessible in PhDStudent
string supervisor;
void displaySupervisor() {
cout << "Supervisor: " << supervisor << endl;
}
};
11
Example 1 Cont.
int main() {
// Using protected inheritance
Graduate grad;
grad.name = "Alice"; // Accessing protected member name
grad.displayName(); // Accessing displayName through public member
function
grad.researchTopicID = 123;
grad.displayResearchTopic();
// Using private inheritance
PhDStudent phd;
phd.name = "Bob"; // Accessing private member name
phd.displayName(); // Accessing displayName through public member function
phd.supervisor = "Dr. Smith";
phd.displaySupervisor();
return 0;
}
12