Lecture Inheritance complete
Lecture Inheritance complete
Information Technology
Inheritance
OOP in C++ by Robert – Chapter#9
Quiz
Time 20 min - 10 marks
• For the Distance class, as discussed in
lectures, add an overloaded - operator that
subtracts two distances. It should allow
statements like dist3=dist1-dist2;
• Assume that the operator will never be
used to subtract a larger number from a
smaller one (that is, negative distances
are not allowed).
2
Inheritance
• Inheritance is an essential part of OOP
• permits code reusability
• Once a class is written and tested, it need
not be touched again, but, using
inheritance, we add different functionalities
3
Inheritance
4
5
Inheritance
6
Syntax
7
Types
8
Inheritance
not private or public
class employee //employee class
{
private:
char name[LEN]; //employee name
unsigned long number; //employee number
public:
void getdata()
{
cout << "\n Enter last name: "; cin >> name;
cout << "Enter number: "; cin >> number;
}
void putdata() const
{
cout << "\n Name: " << name;
cout << "\n Number: " << number;
}
};
9
Inheritance
class manager : public employee //management class
{
private: This line says that Manager
char title[LEN]; //”vice-president” etc. is derived from the base
double dues; //golf club dues class Employee
public:
void getdata()
{
employee::getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
}
void putdata() const
{
employee::putdata();
cout << "\n Title: " << title;
cout << "\n Golf club dues: " << dues;
}
};
10
Constructors in inheritance
• In C++, both the base class and the derived
class can have their own constructors.
• The constructor of a base class is used to
instantiate objects of the base class, while
the constructor of the derived class is used
to instantiate objects of the derived class.
• Inheritance in C++ allows the derived class
to inherit all the members (fields, methods)
of the base class.
11
When we are working with
the constructor in inheritance there are
different cases arise as follows:
1. If both classes contain no constructors (default)
2. If base class contains constructors(parameterless,
parameterized) but drive class not
3. If drive class contains constructors(parameterless,
parameterized) but base class not
4. If both classes have constructors (parameterless,
parameterized)
12
When we are working with
the constructor in inheritance there are
different cases arise as follows:
• Before discussing all the cases, keep the inheritance definition
in mind “a derived class inherits the characteristics of a base
class while retaining its own”
13
When we are working with
the constructor in inheritance there are
different cases arise as follows:
1. Default constructors are called for both classes without any
issues.
2. When the base class has constructors, it uses them to
initialize objects, and the derived class also utilizes the base
class constructors for initialization.
3. If the derived class contains constructors, it successfully
initializes its object, while the base class is unable to do so.
4. In cases where both classes have constructors, the base
class employs its constructors for object initialization, and the
derived class utilizes constructors from both the base and
derived classes for initialization..
14
Orders of Constructors
15
Orders of Destructors
16
Scenario 1: If both classes contain no
constructors
class Base {
// Compiler-provided default constructor
}; Both are instantiated
automatically as default
class Derived : public Base {
constructor.
// Compiler-provided default constructor
};
int main() {
// Objects of both base and derived classes are created
Base obj1;
Derived obj2;
// Output:
// Base class default constructor
// Derived class default constructor
return 0;
}
Scenario 2: Base Class Contains Constructor,
Derived Class Does Not
class Base {
public:
Base() {
cout << "Base class parameter-less constructor\n";
}
Base(int value) {
cout << "Base class parameterized constructor with value: " << value << "\n";
}
};
class Derived : public Base
{ };
int main() { // Output:
Base obj1; // Base class parameterless constructor
Base obj2_parameterized(42); // Base class parameterized constructor with value: 42
Derived obj3; // Base class default constructor
Derived obj4_parameterized(99); // Error
}
// Base class has default and parameterized constructors
// Derived class inherits these constructors
18
Scenario 2: Base Class Contains Constructor,
Derived Class Does Not
class Base {
public:
Base() {
cout << "Base class parameter-less constructor\n";
}
Base(int value) {
cout << "Base class parameterized constructor with value: " << value << "\n";
}
};
class Derived : public Base
{ };
Why Error?
int main() { // Output:
Base obj1; // Base class default constructor
Base obj2_parameterized(42); // Base class parameterized constructor with value: 42
Derived obj3; // Base class default constructor
Derived obj4_parameterized(99); // Error
}
// Base class has default and parameterized constructors
// Derived class inherits these constructors
19
Scenario 2: Base Class Contains Constructor,
Derived Class Does Not
class Base {
public:
Base() {
cout << "Base class parameter-less constructor\n";
}
Base(int value) {
cout << "Base class parameterized constructor with value: " << value << "\n";
};
}
It should work
class Derived : public Base
{ };
without any
int main() {
Base obj1;
error
// Output:
// Base class default constructor
Base obj2_parameterized(42); // Base class parameterized constructor with value: 42
Derived obj3; // Base class default constructor
Derived obj4_parameterized(99); // Error
}
// Base class has default and parameterized constructors
// Derived class inherits these constructors
20
Scenario 2: Base Class Contains Constructor,
Derived Class Does Not
class Base {
public:
Base() {
cout << "Base class parameter-less constructor\n";
}
Base(int value) {
cout << "Base class parameterized constructor with value: " << value << "\n";
}
};
class Derived : public Base
{
There is no way to send
Derived(int v1):Base(v1) value to Base class, we
{ need to modify code of
}
};
derive class constructor
int main() { if we want to make it
Base obj1; working.
Base obj2_parameterized(42);
Derived obj3;
Derived obj4_parameterized(99); 21
}
Scenario 3: Derived Class Contains
Constructor, Base Class Does Not
class Base {
};
class Derived : public Base {
public:
Derived() {
cout << "Derived class parameter-less constructor\n";
}
Derived(int value) {
cout << "Derived class parameterized constructor with value: " << value << "\n";
}
};
int main() {
Base obj1; Default constructor
Base obj2_parameterized(42); Error
Derived obj3; Derived class parameter-less constructor
Derived obj4_parameterized(99); Derived class parameterized constructor with value: 77
return 0; 22
}
Scenario 4: Both Classes Have Constructors
(Parameterless, Parameterized)
class Base {
public:
Base()
{ cout << "Base class parameter-less constructor\n"; }
Base(int value)
{cout << "Base class parameterized constructor with value: " << value << "\n";}
};
class Derived : public Base {
public:
Derived()
{cout << "Derived class parameter-less constructor\n";}
Derived(int value) : Base(value)
{cout << "Derived class parameterized constructor value:" << value << "\n";}
};
int main() {
Base obj1; Base class parameter-less constructor
Base obj2_parameterized(42); Base class parameterized constructor with value 42
Derived obj3; // Base class parameter-less constructor, Derived class
Derived obj4_parameterized(55); parameter-less constructor
// Base class parameterized constructor with value: 55, Derived
class parameterized constructor with value: 55
23
return 0;}
Overriding Member Functions
class Base
{
public:
void showdata()
{
cout<<"\nShow function of base class.";
}
}; When the same function exists in both the base
class and the derived class,
class Derived : public Base For example:
{
Derive d1;
d1.showdata(); //which showdata() will be called?
public:
void showdata()
{
cout<<"\nShow function of derived class.";
};
24
Overriding Member Functions
class Base
{
public:
void showdata()
{
cout<<"\nShow function of base class.";
}
}; how to call base class member function, from a
derived class?
class Derived : public Base
{
public:
void showdata()
{
Base::showdata();
cout<<"\nShow function of derived class.";
};
25
Inheritance and Accessibility
26
Access Modifiers
27
Access Modes in Inheritance
28
Public Inheritance
• With public inheritance, public and
protected members of the base class
become respectively public and protected
members of the derived class.
03/07/2025
Private Inheritance
• With private inheritance, public and
protected members of the base class
become private members of the derived
class.
objects of the derived class cannot access
public member functions of the base class.
03/07/2025
Protected Inheritance
• Public and protected members of the base
class become protected members of the
derived class.
03/07/2025
Levels of Inheritance
32
Single Inheritance
If we derive only Manager From Employee Class
33
Hierarchical Inheritance
If we derive Both Manager and Scientist From
Employee Class
34
Class Hierarchies
35
Read book Pages: 389-392
Multi- level Inheritance
36
Example
// Derived class from Manager
// Base class class SeniorManager : public Manager
class Employee { {
public: public:
void details() { void lead() {
cout << "I am an employee." << endl; cout << "I lead the managers." <<
endl;
}
}
};
};
// Derived class from Employee
int main() {
class Manager : public Employee {
SeniorManager sm;
public:
sm.details(); // Inherited from
void manage() {
Employee
cout << "I manage the team." <<
sm.manage(); // Inherited from
endl;
Manager
}
sm.lead(); // Own method
};
return 0;
} 37
Multiple Inheritance
38
Example
// Derived class inheriting from Teacher
and Researcher
// Base class 1 class Professor : public Teacher, public
Researcher {
class Teacher {
public:
public:
void work() {
void teach() {
cout << "Professor's work involves
cout << "Teaching students." << endl;
both teaching and research." << endl;
}
}
};
};
// Base class 2
int main() {
class Researcher {
Professor prof;
public:
prof.teach(); // Inherited from
void research() { Teacher
cout << "Conducting research." << prof.research(); // Inherited from
endl; Researcher
} prof.work(); // Professor's own
}; method
return 0; 39
}
Hybrid Inheritance
40
Diamond Problem
41
// Common base class // Derived class from both
class Employee { Developer and Manager
public: class TechLead : public Developer,
void showEmployee() { public Manager {
cout << "I am an employee." << endl; public:
} void showTechLead() {
}; cout << "I am a technical
lead." << endl;
// First derived class from Employee
}
class Developer : virtual public Employee {
};
public:
void showDeveloper() {
int main() {
cout << "I am a developer." << endl;
TechLead tl;
}
tl.showEmployee(); // Inherited
};
once from Employee due to virtual
// Second derived class from Employee inheritance
class Manager : virtual public Employee { tl.showDeveloper(); // Inherited
public: from Developer
void showManager() { tl.showManager(); // Inherited
cout << "I am a manager." << endl; from Manager
} tl.showTechLead(); // Own
}; method
return 0; 42
}
Access Combinations
43
“Abstract” Base Class
• we don’t define any objects of the base
class employee.
We use this as a general class whose only
purpose is to act as a base from which other
classes are derived
• Classes used only for deriving other
classes, as employee is in EMPLOYEE,
are called abstract classes
meaning that no actual instances (objects) of
this class are created 44