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

OOP Lect 14 Inheritance Till Mid

Lecture 14 covers the concept of inheritance in Object Oriented Programming, highlighting its importance for software reusability. It explains the different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid, along with access control mechanisms for class members. The lecture also provides examples and definitions related to class hierarchies and inheritance relationships.

Uploaded by

johnnylorenzo001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OOP Lect 14 Inheritance Till Mid

Lecture 14 covers the concept of inheritance in Object Oriented Programming, highlighting its importance for software reusability. It explains the different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid, along with access control mechanisms for class members. The lecture also provides examples and definitions related to class hierarchies and inheritance relationships.

Uploaded by

johnnylorenzo001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Lecture 14: Inheritance CS112: Object Oriented Programming

Object Oriented Programming

Software Engineering Department


Spring 2025

Muhammad Huzaifa Shah

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance – I

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Introduction to Inheritance
• Probably most powerful feature of OOP

• New classes are created from existing classes

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Introduction to Inheritance
• New classes absorb all features of existing classes
including their data and functions.

• Also enhance them by adding their own new features


in form of new data members and new member
functions

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Introduction
• Existing classes are called base classes

• New classes are called derived classes

• Inheritance provides us a mechanism of software


reusability which is one of the most important
principles of software engineering

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Inheriting Data and Functions


• All data members and member functions of base class are
inherited to derived class
class BaseClass{
// members....
// member function
};

class DerivedClass : public BaseClass{


// members....
// member function
};
• Constructors, destructors and = operator are not inherited
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Animals: Class’s hierarchy

Classification Animal

Mammal Reptiles

People Dog .....

man woman Inheritance

John Mary

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance Examples
Base class Derived classes

Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Another example: University’s community


member’s hierarchy
CommunityMember

Employee Student Alumnus Single inheritance

Faculty Staff Single inheritance

Administrator Teacher Single inheritance

AdministratorTeacher Multiple inheritance

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Some definitions in class hierarchy


1. Direct base class
Inherited explicitly (one level up hierarchy)
2. Indirect base class
Inherited two or more levels up hierarchy
3. Single inheritance
Inherits from one base class
4. Multiple inheritance
Inheritance from multiple classes

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Types of Inheritance: Levels


1. Single inheritance
1 Base and 1 Drive Class
2. Multiple inheritance
A derived class inherits from multiple base classes.
3. Multilevel inheritance
A chain of inheritance where one class is derived from another,
which is derived from another.
4. Hierarchical inheritance
One base class with multiple derived classes.
5. Hybrid inheritance
Combination of two or more types of inheritance.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Types of Inheritance: Levels


Base Derived
Type Example
Classes Classes
Single
1 1 Vehicle → Car
Inheritance

Multiple Student + Athlete →


2+ 1
Inheritance SportsStudent

Multilevel
1 1 (chain) Animal → Mammal → Dog
Inheritance

Hierarchical
1 2+ Animal → (Dog, Cat)
Inheritance

Hybrid Person → (Father, Mother) →


Combo Combo
Inheritance Child
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Types of Inheritance: Levels


1. Single inheritance

2. Multiple inheritance

3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Which Type does it belong to?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

1.Single inheritance
1. Single inheritance is defined as the inheritance in which a
derived class is inherited from the only one base class.

Where 'A' is the base


class, and 'B' is the
derived class.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

1.Single inheritance
1. Single inheritance is defined as the inheritance in which a
derived class is inherited from the only one base class.

2. Inheriting Methods:

Where 'A' is the base


class, and 'B' is the
derived class.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Access Control Over the


Members
• Two levels of access control
base class/ superclass/ over class members
parent class – class definition
– inheritance type
members goes to
derive from

class Point{
protected: int x, y;
public: void set(int a, int b);
};
derived class/ subclass/
child class class Circle : public Point{
……
};
17
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Access a Method
class Point{
protected: class Circle : public Point{
int x, y; private: double r;
public: public:
void set(int a, int b) void set (int a, int b, double c) {
{x=a; y=b;} Point :: set(a, b); //same name function call
void foo (); r = c;
void print(); }
}; void print(); };

Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle
18
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Modes of Inheritance
Visibility modes in accessing data members and methods
can be classified into three categories:
1. Public: When the member is declared as public, it is
accessible to all the functions of the program.
2. Private: When the member is declared as private, it is
accessible within the class only.
3. Protected: When the member is declared as protected, it is
accessible within its own class as well as the class
immediately derived from it.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Public Inheritance
1. Public Inheritance (class Derived : public Base)
1. Statement 1: “If a member is public in the base class, it remains
public in the derived class.”

2. Statement 2: “If a member is protected in the base class, it remains


protected in the derived class.”

3. Statement 3: “If a member is private in the base class, it is not


inherited in the derived class.”

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Public Inheritance

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Protected Inheritance
2. Protected Inheritance (class Derived : protected Base)
1. Statement 1: “If a member is public in the base class, it becomes
protected in the derived class.”

2. Statement 2: “If a member is protected in the base class, it remains


protected in the derived class.”

3. Statement 3: “If a member is private in the base class, it is not


inherited in the derived class.”

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Protected Inheritance

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Private Inheritance
1. Private Inheritance (class Derived : private Base)
1. Statement 1: “If a member is public in the base class, it becomes
private in the derived class.”

2. Statement 2: “If a member is protected in the base class, it also


becomes private in the derived class.”

3. Statement 3: “If a member is private in the base class, it is not inherited


in the derived class.”

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Private Inheritance

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming
Access a Method
#include <iostream> class DerivedPublic : public Base {
using namespace std; public: void display() {
class Base { cout << "Public "
public: int publicVar = 1; <<publicVar<<endl;
protected: int protectedVar cout << “Prot" << protectedVar
= 2; << endl;
private: int privateVar = 3; // privateVar is not accessible } };
class DerivedProtected : protected Base {
};
public: void display() { cout << "Protected Inheritance: " <<
publicVar << ", " << protectedVar << endl; // privateVar is not
accessible } };
class DerivedPrivate : private Base { public: void display() { cout
<< "Private Inheritance: " << publicVar << ", " << protectedVar
<< endl; // privateVar is not accessible } };
int main() {
DerivedPublic d1; d1.display(); // Accessible: publicVar, protectedVar
DerivedProtected d2; d2.display(); // Accessible: publicVar, protectedVar
(but now protected in DerivedProtected)
DerivedPrivate d3; d3.display(); // Accessible: publicVar, protectedVar (but
now private in DerivedPrivate) 26
return 0; }Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Public Inheritance
#include <iostream> int main() {
using namespace std; Derived d;
d.display();
class Base {
public: cout << "From object: " << d.publicVar << endl; //
int publicVar = 10; Accessible
protected: // cout << d.protectedVar << endl; // Not
accessible (protected)
int protectedVar = 20;
// cout << d.privateVar << endl; // Not
private: accessible (private)
int privateVar = 30;
}; return 0;
}
class Derived : public Base {
public:
void display() {
cout << "Public Var: " << publicVar << endl; // Accessible
cout << "Protected Var: " << protectedVar << endl; // Accessible
// cout << "Private Var: " << privateVar << endl; // Not accessible
}
};
27
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Protected Inheritance
#include <iostream> int main() {
using namespace std; Derived d;
d.display();
class Base {
public: // cout << d.publicVar << endl; // Not accessible
int publicVar = 10; (now protected)
protected: // cout << d.protectedVar << endl; // Not accessible
(protected)
int protectedVar = 20;
// cout << d.privateVar << endl; // Not accessible
private: (private)
int privateVar = 30;
}; return 0;
}
class Derived : protected Base {
public:
void display() {
cout << "Public Var: " << publicVar << endl; // Accessible (now
protected)
cout << "Protected Var: " << protectedVar << endl; // Accessible
// cout << "Private Var: " << privateVar << endl; // Not accessible
}
}; 28
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Private Inheritance
#include <iostream> int main() {
using namespace std; Derived d;
d.display();
class Base {
public: // cout << d.publicVar << endl; // Not accessible
int publicVar = 10; (now private)
protected: // cout << d.protectedVar << endl; // Not accessible
(now private)
int protectedVar = 20;
// cout << d.privateVar << endl; // Not accessible
private: (private)
int privateVar = 30;
}; return 0;
}
class Derived : private Base {
public:
void display() {
cout << "Public Var: " << publicVar << endl; // Accessible (now
private)
cout << "Protected Var: " << protectedVar << endl; // Accessible
(now private)
// cout << "Private Var: " << privateVar << endl; // Not accessible
} 29
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
};
Lecture 14: Inheritance CS112: Object Oriented Programming

Modes of Inheritance
Derived class visibility
Base class
visibility
Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected


Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance Concept
class Rectangle{
private:
Polygon int numVertices;
float *xCoord, *yCoord;
public:
Rectangle void set(float *x, float *y, int
Triangle nV);
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 void set(float *x, float *y, int
nV);
nV);
float area();
}; Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
};
Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance Concept
class Polygon{
Polygon protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Rectangle{
protected:
class Rectangle : public int numVertices;
Polygon{ float *xCoord, float *yCoord;
public: public:
float area(); void set(float *x, float *y, int nV);
}; float area();
}; Sciences and Technology, Topi
Ghulam Ishaq Khan Institute of Engineering
Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance Concept
class Polygon{
Polygon protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
};
Rectangle
Triangle
class Triangle{
protected:
int numVertices;
class Triangle : public
float *xCoord, float *yCoord;
Polygon{
public:
public:
void set(float *x, float *y, int
float area(); nV);
}; float area();
Ghulam Ishaq Khan Institute of Engineering
}; Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Inheritance Concept
x
class Point{
Point y protected:
int x, y;
public:
Circle 3D-Point void set (int a, int b);
x x };
y y
r z

class Circle : public Point{ class 3D-Point: public


private: Point{
double r; private:
}; int z;
};
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Define a Class Hierarchy


• Syntax:
class DerivedClassName : access-level
BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• public
• Any class can serve as a base class
– Thus a derived class can also be a base class

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Class Derivation
Point class Point{
protected:
int x, y;
3D-Point public:
void set (int a, int b);
};
Sphere
class 3D-Point : public class Sphere : public 3D-
Point{ Point{
private: private:
double z; double r;
…… ……
}; };
oint is the base class of 3D-Point, while 3D-Point is the base class of Sphere
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

What to inherit?

• In principle, every member (but not private) of a base


class is inherited by a derived class
– just with different access permission

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Even more …

• A derived class can override methods defined in its


parent class. With overriding,
– the method in the subclass has the identical signature to the
method in the base class.
– a subclass implements its own version of a base class
method.
class A {
protected: class B : public A {
public:
int x, y;
public: void print ()
void print () {cout<<“From B”<<endl;}
};
{cout<<“From
A”<<endl;}
};
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

Access a Method
class Point{
protected: class Circle : public Point{
private: double r;
int x, y;
public: public:
void set(int a, int b) void set (int a, int b, double c) {
Point :: set(a, b); //same name
{x=a; y=b;} function call
void foo (); r = c;
void print(); }
}; void print(); };

Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

Output?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

point and circle classes


class Point
{
protected:
int x,y;
public:
Point(int ,int);
void display(void);
};
Point::Point(int a,int b)
{
x=a;
y=b;
}
void Point::display(void)
{
cout<<"point = [" <<x<<","<<y<<"]";
}
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 14: Inheritance CS112: Object Oriented Programming

class Circle : public Point


{
double radius;
public:
Circle(int ,int ,double );
void display(void);
};
Circle::Circle(int a,int b,double c):Point(a,b) {
radius = c;
}
void Circle::display(void) {
Point::display();
cout<<" and radius = "<<radius;
}

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 14: Inheritance CS112: Object Oriented Programming

int main(void)
{
Circle c(3,4,2.5);
c.display();
return 0;
}
Output:
point=[3,4] and radius = 2.5

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like