OOP Lect 14 Inheritance Till Mid
OOP Lect 14 Inheritance Till Mid
Inheritance – I
Introduction to Inheritance
• Probably most powerful feature of OOP
Introduction to Inheritance
• New classes absorb all features of existing classes
including their data and functions.
Introduction
• Existing classes are called base classes
Classification Animal
Mammal Reptiles
John Mary
Inheritance Examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
Multilevel
1 1 (chain) Animal → Mammal → Dog
Inheritance
Hierarchical
1 2+ Animal → (Dog, Cat)
Inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
1.Single inheritance
1. Single inheritance is defined as the inheritance in which a
derived class is inherited from the only one base class.
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:
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.
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.”
Public Inheritance
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.”
Protected Inheritance
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.”
Private Inheritance
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
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 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?
Even more …
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
Output?
int main(void)
{
Circle c(3,4,2.5);
c.display();
return 0;
}
Output:
point=[3,4] and radius = 2.5