COM265 - Part 05 - Inheritance

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

COM265

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

class Person class Adult: public Person


{ {
string name, surname; int ID_card;

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

Derived class (child or Sub Class) Adult


Derived class

Base class should be placed above the derived.


The arrow must point to the base class
i.e., not from the base to the derived
please, remember (seems counter-intiutive)

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?)

Mode of Member in the base class


inheritance private: protected: public:

:private inaccessible private private

:protected inaccessible protected protected


9 :public inaccessible protected public
Constructors and Destructors in Base and
Derived Classes
• Derived classes can have their
own constructors and destructors
• When an object of a derived class
is created, the base class
constructor is executed first,
followed by the derived class
constructor
• When an object of a derived class
is destroyed, its destructor is
called first, then that of the base
class

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.

• When the base class’s


constructor takes
arguments, the derived class
constructor pass arguments
to the base class constructor

13
Derived Class

constructor Passing Arguments


to Base Class Constructor

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

You might also like