Inheritance Presentation
Inheritance Presentation
INHERITANCE
• What is inheritance?
• Introduction to Inheritance
• Inheritance Examples
• Why Inheritance
• Inheritance – Terminology & Notation
• Modes of Inheritance
SLIDE 01
• Types of Inheritance
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
What Is Inheritance?
SLIDE 02
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Example: Insects
• Insect is generic
SLIDE 03
• Bee and grasshopper are specific
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
The “IS - A" Relationship
SLIDE 04
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Introduction - Inheritance
SLIDE 05
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Introduction - Inheritance
SLIDE 06
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Inheritance Examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
SLIDE 07
Account CheckingAccount
SavingsAccount
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Why Inheritance?
SLIDE 08
easier to understand
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Why Inheritance?
class Rectangle{
private:
int numVertices;
float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle Triangle float area();
};
SLIDE 09
}; float area();
};
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Inheritance – Terminology and Notation
SLIDE 10
};
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Inheriting Data and Functions
SLIDE 11
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
What Does a Child Class Have?
• In the Student and underGrad example shown earlier:
• An object of the derived class has:
• All members defined in child class
• All members of the parent class except constructors, destructors and
operator=
• An object of the derived class can use:
• All public members defined in child class
SLIDE 12
• All public members defined in parent class
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Modes of Inheritance
• Public Mode: If we derive a subclass from a public base class. Then the public
member of the base class will become public in the derived class and protected
members of the base class will become protected in the derived class.
• Protected Mode: If we derive a subclass from a Protected base class. Then both
public members and protected members of the base class will become
protected in the derived class.
• Private Mode: If we derive a subclass from a Private base class. Then both
public members and protected members of the base class will become Private
SLIDE 13
in the derived class.
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Types of Inheritance
SLIDE 14
created from another derived class.
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Example:
class Person { } void display_s()
int id; }; {
char name[100]; class Student : private Person { display_p();
public: char course[50]; cout <<"Course: "<< course << "\nFee: " <<
void set_p() int fee; fee << endl;
{ public: }
cin >> name; cout << "Enter the Course Name:"; Student s;
SLIDE 15
cout << endl <<"Id: "<< id << "\nName: " << } }
name <<endl;
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024