Inheritance
Inheritance
Lecture 24
Dr. Ashfaq Hussain Farooqi
Concepts Related with Inheritance
Concepts Related with Inheritance
• Generalization
• Subtyping (Extension)
• Specialization (Restriction or Overriding )
Generalization
• We extract these features into a new class and inherit original classes
from this new class
Student Doctor
• Program Teacher • Specialty
• RollNumber • Hospital
• Designation
• Semester
• University CheckUp()
AttendClass() Prescribe()
Teach()
Study()
TakeExam()
Sub-typing & Specialization
• Inherit the new class from this class and add unique behavior to the
new class
Sub-typing (Extension)
Student Doctor
• Program Teacher • Designation
• RollNumber • Salary
• Designation
• Semester
• Salary CheckUp()
AttendClass() Prescribe()
Teach()
Study()
TakeExam()
Specialization (Restriction or Overriding)
Adult
• …
• Age: [18 … 100]
• … If age < 18 then
error
SetAge(a) else
… age = a
Inheritance
Definition:
“Process of extending existing class into new class is known as
inheritance”
• Existing class is known as Base Class (or Parent Class)
• New class is known as Derived Class (or Child Class)
Vehicle Polygon
class class
Syntax
class Child : public Parent {
class ChildClass: public ParentClass public:
{ int multiply()
... {
}; return x * y;
}
#include <iostream> };
using namespace std;
• The various ways we can derive classes are known as access modes.
These access modes have the following effect:
• The private members of the base class are always private in the derived
class.
Access in Inheritance
Public Inheritance
Member access in
Base Class Derived Class
Public Public
Protected Protected
Private Hidden
Protected Inheritance
Member access in
Base Class Derived Class
Public Protected
Protected Protected
Private Hidden
Private Inheritance
Member access in
Base Class Derived Class
Public Private
Protected Private
Private Hidden
Object Oriented Programming
Lecture 25
Dr. Ashfaq Hussain Farooqi
Inheritance (example)
#include <iostream>
using namespace std;
class Polygon {
private:
int width, height;
public:
void set_values(int a, int b)
{
width = a; height = b;
}
int get_width()
{
return width;
}
int get_height()
{
return height;
}
};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : public Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width()
{
return width;
}
int get_height()
{
return height;
}
};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : public Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5);
int get_height() cout << rec1.area();
{ return 0;
return height; }
}
};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : public Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5);
int get_height() cout << rec1.area();
{ cout << rec1.width;
return height; return 0; Error
} }
};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : protected Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5); Error
int get_height() cout << rec1.area();
{ return 0;
return height; }
}
};
Inheritance (example)
#include <iostream>
using namespace std; class Rectangle : private Polygon {
public:
class Polygon { int area()
private: {
int width, height; return get_width() * get_height();
public: }
void set_values(int a, int b) };
{
width = a; height = b;
}
int get_width() int main()
{ {
return width; Rectangle rec1;
} rec1.set_values(4, 5); Error
int get_height() cout << rec1.area();
{ return 0;
return height; }
}
};
Allocation in Memory
base member1
base member2 Data members of base
... class
derived member1
derived member2 Data members of
... derived class
Constructors
•If default constructor of base class does not exist then the
compiler will try to generate a default constructor for base class
and execute it before executing constructor of derived class
•If the user has given only an overloaded constructor for base
class, the compiler will not generate default constructor for base
class
Example
class Parent
int main()
{
public:
{
Parent(int i)
Child cobj(1);
{
return 0;
cout << "Parent Constructor...";
}
}
};
class Parent
{
public:
Parent()
{
cout << "Parent Constructor...";
}
};
class Parent
{
public:
Parent() { cout << "Parent Constructor"; }
~Parent() { cout << "Parent Destructor"; }
};
int main()
{
Child var;
return 0;
}
Single Inheritance
Class A
Class B
Inheritance (example)
Class A
Class B
Class C
Multi-level Inheritance
Car
Toyota
Corolla
class Grand_Parent {
class Child : public Parent {
public: public:
void MyGrandParent() void Me()
{ {
cout << "Grand Parent class" << endl; cout << "Child class" << endl;
} }
}; };
Class A Class B
Class C
Multiple Inheritance
Hybrid Car
Multiple Inheritance (example)
Class A
Class B Class C
Class D
Hybrid Inheritance
Car
Electric Petrol
Car Car
Hybrid
Car
Hybrid Inheritance
Hybrid Inheritance (example)
Problems with Multiple, Multi-level and Hybrid Inheritance
• Increased complexity
• Reduced understanding
• Ambiguity
Diamond Problem
Car
• Color
• Model
• …
Drive()
…
Electric Car Petrol Car
• Color • Color
• Model • Model
• … • …
… …
Hybrid Car
• Color
• Model
• …
…