Professional Documents
Culture Documents
COM265 - Part 05 - Inheritance
COM265 - Part 05 - Inheritance
COM265 - Part 05 - Inheritance
Computer Programming II
Spring 2024
Part 05 – Inheritance
Instructor: Dr. Ahmed Alenany
Credits to: Prof. Roman Starosolski, Silesian University of
Technology, Poland
Why to inherit?
class Person class Adult
{ {
string name, surname; string name,surname;
int ID_card;
public: public:
void print_name();
void print_name();
void print_surname();
void print_surname();
void print_ID_card();
void print_all();
void print_all();
}; };
2
class access specifier or
Why to inherit? mode of inheritance
public: public:
void print_ID_card();
void print_name();
void print_all();
void print_surname();
};
void print_all();
};
3
Inheritance
Inheritance is a form of software reuse in which you
derive a class that inherits an existing base class’s data
and behaviors (except the constructors and
destructor) and defines only additional required
features.
Inheritance establishes an "is a" or “is a kind of ”
relationship between classes.
An adult is a person
A car is a vehicle
A flower is a plant
4
Inheritance class diagram
Base
ase classclass (parent or Super Class) Person
5
Example
#include <iostream>
#include <string.h> The base class has one
using namespace std; private attribute ID and two
// base class public methods
class Student {
int ID;
public:
void setID(int n){ID = n;}
int getID() {return ID;}
};
// derived class
class Undergrad: public Student {
string college;
public:
void setCollege(string s) { college = s;}
string getCollege() {return college;}
};
6
Example, continued
int main()
{
Undergrad Yaser;
string c;
int m;
cout << "\nPlease, enter your college: ";
getline(cin,c);
cout << "and your ID: "; Object of the derived class can access methods of
cin >> m; the derived class and methods of the base class
Yaser.setID(m);
Yaser.setCollege(c);
cout << "\nYour data is:";
cout << "\nID: " << Yaser.getID() << ", College: " << Yaser.getCollege();
return 0;
}
7
Protected Members
• Private variables
cannot be accessed
using derived class
methods.
• Therefore, the
protected access
specifier was
introduced.
8
Accessibility of base class members
(Can base class members be accessed by derived class
methods?)
10
Constructors and Destructors in multilevel
inheritance
Vehicle
FourWheeler
11
Car
Constructors and Destructors in Base and Derived
Classes in multilevel inheritance
Vehicle
FourWheeler
12 Car
What if base class constructor takes arguments?
• In the previous examples both base Base class
and derived class have default
constructors.
13
Derived Class
14
Main function
15
Example
Consider the following class hierarchy:
Employee
Manager Developer
where
Each Employee has a name and ID.
Manager is an employee with extra data: salary and
bonus.
Developer is an employee with extra data: number
16
of working hours, salary per hours.
Requirements:
• Implement the following methods:
• For Employee Class:
Set Employee Data: to let the user set employee data.
Get Employee Data: to print employee data.
• For Manager Class:
Set Manager Data: to let the user set manager data.
Get Manager Data: to print manager data.
Calculate total salary = (salary * bonus)/100 + salary.
• For Developer Class:
Set Developer Data: to let the user set developer data.
Get Developer Data: to print developer data.
Calculate total salary = (salary per hours* num of working hours).
17
Answer
18
Answer, continued
19
Answer, continued
Sample run
20