0% found this document useful (0 votes)
50 views

Inheritance Presentation

Inheritance Presentation

Uploaded by

Atif Saeed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Inheritance Presentation

Inheritance Presentation

Uploaded by

Atif Saeed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OBJECT ORIENTED PROGRAMMING

INHERITANCE

MUHAMMAD ATIF SAEED


ASSOCIATE LECTURER
PRESENTATION OUTLINE

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

• One of the most powerful features of OOP


• Provides a way to create a new class from an existing class
• The new class inherits all the capabilities of the existing class and
can also add capabilities of its own.
• The base class is unchanged by this process.
• The new class is a specialized version of the existing class

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

• Inheritance establishes an “IS - A" relationship between classes.


• A poodle is a dog
• A car is a vehicle
• A flower is a plant
• A football player is an athlete

SLIDE 04
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Introduction - Inheritance

• Existing classes are called base classes


• New classes are called derived classes

SLIDE 05
OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Introduction - Inheritance

• Existing classes are called base classes


• New classes are called derived classes
• Objects of derived classes are more specialized as compared to
objects of their base classes

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?

• Inheritance provides us a mechanism of software reusability


which is one of the most important principles of software
engineering
• Show similarities
• Easy modification by performing modification in one place
• Avoid redundancy, leading to smaller and more efficient model,

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();
};

class Polygon{ class Triangle{


private: private:
int numVertices; int numVertices;
float *xCoord, *yCoord; float *xCoord, *yCoord;
public: public:
void set(float *x, float *y, int nV); void set(float *x, float *y, int nV);

SLIDE 09
}; float area();
};

OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Inheritance – Terminology and Notation

• Base class (or parent or superclass) – inherited from


• Derived class (or child or subclass) – inherits from the base class
• Notation:
class Student // base class
{
};
class UnderGrad : public Student
{ // derived class

SLIDE 10
};

OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024
Inheriting Data and Functions

• All data members and member functions of base class are


inherited to derived class, except
• Constructors, destructors and = operator are not inherited

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

• Single Inheritance: In single inheritance, a class is allowed to inherit


from only one class. i.e. one subclass is inherited by one base class
only.
• Multiple Inheritance: Multiple Inheritance is a feature of C++ where
a class can inherit from more than one class. i.e one subclass is
inherited from more than one base class.
• Multilevel Inheritance: In this type of inheritance, a derived class is

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: }

cout << "Enter the Id:"; void set_s() };

cin >> id; { int main()

cout << "Enter the Name:"; set_p(); {

cin >> name; cout << "Enter the Course Name:"; Student s;

} cin >> course; s.set_s();

void display_p() cout << "Enter the Course Fee:"; s.display_s();

{ cin >> fee; return 0;

SLIDE 15
cout << endl <<"Id: "<< id << "\nName: " << } }
name <<endl;

OBJECT ORIENTED PROGRAMMING | MUHAMMAD ATIF SAEED (Associate Lecturer) | August 5, 2024

You might also like