0% found this document useful (0 votes)
23 views61 pages

Inheritance

The document discusses concepts related to inheritance in Object-Oriented Programming, including generalization, subtyping, and specialization. It explains how inheritance allows for the creation of new classes from existing ones, detailing access modes (public, protected, private) and the role of constructors and destructors in class relationships. Additionally, it covers various types of inheritance such as single, multilevel, multiple, and hybrid inheritance with examples in C++.

Uploaded by

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

Inheritance

The document discusses concepts related to inheritance in Object-Oriented Programming, including generalization, subtyping, and specialization. It explains how inheritance allows for the creation of new classes from existing ones, detailing access modes (public, protected, private) and the role of constructors and destructors in class relationships. Additionally, it covers various types of inheritance such as single, multilevel, multiple, and hybrid inheritance with examples in C++.

Uploaded by

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

Object Oriented Programming

Lecture 24
Dr. Ashfaq Hussain Farooqi
Concepts Related with Inheritance
Concepts Related with Inheritance

• Generalization

• Subtyping (Extension)
• Specialization (Restriction or Overriding )
Generalization

• In OO models, some classes may have common characteristics

• We extract these features into a new class and inherit original classes
from this new class

• This concept is known as Generalization

• When we implement Generalization in a programming language, it is


often called Inheritance instead
Generalization - Example

Student Teacher Doctor


• Name • Name • Name
• Age • Age • Age
• Gender • Gender • Gender
• Program • Designation • Specialty
• RollNumber • University • Hospital
• Semester
Eat() Eat()
Eat()
Walk() Walk()
Walk()
Teach() CheckUp()
AttendClass()
TakeExam() Prescribe()
Study()
Generalization - Example

Student Teacher Doctor


• Name • Name • Name
• Age • Age • Age
• Gender • Gender • Gender
• Program • Designation • Specialty
• RollNumber • University • Hospital
• Semester
Eat() Eat()
Eat()
Walk() Walk()
Walk()
Teach() CheckUp()
AttendClass()
TakeExam() Prescribe()
Study()
Generalization - Example
Person
• Name
• Age
• Gender
Eat()
Walk()

Student Doctor
• Program Teacher • Specialty
• RollNumber • Hospital
• Designation
• Semester
• University CheckUp()
AttendClass() Prescribe()
Teach()
Study()
TakeExam()
Sub-typing & Specialization

• We want to add a new class to an existing model

• Find an existing class that already implements some of the desired


state and behavior

• Inherit the new class from this class and add unique behavior to the
new class
Sub-typing (Extension)

• Sub-typing means that derived class is behaviorally compatible with


the base class

• Behaviorally compatible means that base class can be replaced by the


derived class

• Derived class add unique behavior to the new class


Sub-typing - Example
Person
• Name
• Age
• Gender
Eat()
Walk()

Student Doctor
• Program Teacher • Designation
• RollNumber • Salary
• Designation
• Semester
• Salary CheckUp()
AttendClass() Prescribe()
Teach()
Study()
TakeExam()
Specialization (Restriction or Overriding)

• Specialization means that derived class is behaviorally incompatible


with the base class

• Behaviorally incompatible means that base class can’t always be


replaced by the derived class

• Reasons for Specialization


• Provide behavior specific to a derived class
• Modify the default behavior
• Restrict the default behavior
• Improve performance
Specialization - Example
Person
• …
• Age: [0 … 100]
• …
SetAge(a)
Age = a

Adult
• …
• Age: [18 … 100]
• … If age < 18 then
error
SetAge(a) else
… age = a
Inheritance

• Inheritance is one of the key features of Object-oriented


programming in C++.
• It allows us to create a new class (derived class/child class) from an
existing class (base class/parent class).
• The derived class inherits the features from the base class and can
have additional features of its own.
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)

What is inherited from the base class?


Every member of a base class except:
• constructors and its destructor
• friends
UML Notation

Parent Class Base Class

Child Class Derived Class


Inheritance (example)

Vehicle Polygon
class class

Car Truck Rectangle Triangle


class class class class
Inheritance (Syntax and Example)

Syntax
class Child : public Parent {
class ChildClass: public ParentClass public:
{ int multiply()
... {
}; return x * y;
}
#include <iostream> };
using namespace std;

class Parent int main()


{ {
public: Child var1;
var1.set_values(4, 5);
int x, y; cout << var1.multiply();
void set_values(int a, int b) return 0;
{ }
x = a; y = b;
}
};
Access in Inheritance

• The various ways we can derive classes are known as access modes.
These access modes have the following effect:

• public: If a derived class is declared in public mode, then the members of


the base class are inherited by the derived class just as they are.
• private: In this case, all the members of the base class
become private members in the derived class.
• protected: The public members of the base class
become protected members in the derived class.

• The private members of the base class are always private in the derived
class.
Access in Inheritance
Public Inheritance

class Child: public Parent {…};

Member access in
Base Class Derived Class
Public Public
Protected Protected
Private Hidden
Protected Inheritance

class Child: protected Parent {…};

Member access in
Base Class Derived Class
Public Protected
Protected Protected
Private Hidden
Private Inheritance

class Child: private Parent {…};

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

• Every object of derived class has an anonymous


object of base class
Allocation in Memory
• The object of derived class is represented in memory
as follows

base member1
base member2 Data members of base
... class
derived member1
derived member2 Data members of
... derived class
Constructors

• The anonymous object of base class must be initialized using constructor


of base class

• When a derived class object is created the constructor of base class is


executed before the constructor of derived class
Constructors

base member1 Base class constructor


base member2 initializes the anonymous
... object

derived member1 Derived class constructor


derived member2 initializes the derived class
... object
Example

class Parent int main()


{ {
public: Child cobj;
Parent() return 0;
{ }
cout << "Parent Constructor...";
}
};

class Child : public Parent


{
Output:
public: Parent Constructor...
Child()
{ Child Constructor...
cout << "Child Constructor...";
}
};
Constructor

•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: Child cobj;
Parent(int i) return 0;
{ }
cout << "Parent Constructor...";
}
};

class Child : public Parent


{
Output:
public: Error
Child()
{
cout << "Child Constructor...";
}
};
Base Class Initializer

• C++ has provided a mechanism to explicitly call a constructor of


base class from derived class

• The syntax is similar to member initializer and is referred as


base-class initialization
Example

class Parent
int main()
{
public:
{
Parent(int i)
Child cobj(1);
{
return 0;
cout << "Parent Constructor...";
}
}
};

class Child : public Parent


{
public:
Child(int i): Parent(i)
{
cout << "Child Constructor...";
}
};
Base Class Initializer

• User can provide base class initializer (through


constructor) and member initializer simultaneously
Example

class Parent
{
public:
Parent()
{
cout << "Parent Constructor...";
}
};

class Child : public Parent


{
int member;
public:
Child(): member(0), Parent()
{
cout << "Child Constructor...";
}
};
Base Class Initializer

• The base class initializer can be written after member


initializer for derived class
• The base class constructor is executed before the
initialization of data members of derived class.
Initializing Members

• Derived class can only initialize members of base class


using overloaded constructors
Destructors

• Destructors are called in reverse order of constructor


called
• Derived class destructor is called before the base class
destructor is called
Example

class Parent
{
public:
Parent() { cout << "Parent Constructor"; }
~Parent() { cout << "Parent Destructor"; }
};

class Child : public Parent


{
public:
Child() { cout << "Child Constructor"; }
~Child() { cout << "Child Destructor"; }
};

int main()
{
Child var;
return 0;
}
Single Inheritance

“A derived class with only one base class is


called single inheritance”

Class A

Class B
Inheritance (example)

#include <iostream> class Child : public Parent {


using namespace std;
};
class Parent{
public:
void myFunction()
{
cout << “Parent class“ << endl; int main() {
} Child myObj;
}; myObj.myFunction();
return 0;
}
Inheritance (example)

// derived classes class Rectangle : public Polygon {


#include <iostream> public:
using namespace std; int area()
{ int main() {
return width * height; Rectangle rect;
class Polygon { Triangle trgl;
protected: }
}; rect.set_values(4, 5);
int width, height; trgl.set_values(4, 5);
public: cout << rect.area();
void set_values(int a, int b) cout << trgl.area();
{ class Triangle : public Polygon {
return 0;
width = a; height = b; public:
}
} int area()
}; {
return width * height / 2;
}
};
Other types of Inheritance

Multiple Multi-level Hybrid


Multilevel Inheritance

“A derived class with one base class and that base


class is a derived class of another is called
multilevel inheritance.”

Class A

Class B

Class C
Multi-level Inheritance
Car

Toyota
Corolla

Altis Grande 1.8 Altis 1.6


Multilevel Inheritance

class Grand_Parent {
class Child : public Parent {
public: public:
void MyGrandParent() void Me()
{ {
cout << "Grand Parent class" << endl; cout << "Child class" << endl;
} }
}; };

class Parent : public Grand_Parent { int main() {


Child myObj;
public: myObj.MyGrandParent();
void MyParent() myObj.MyParent();
{ myObj.Me();
cout << "Parent class" << endl;
} return 0;
}; }
Multiple Inheritance

“A derived class with multiple base class is called


multiple inheritance.”

Class A Class B

Class C
Multiple Inheritance

We may want to reuse characteristics of more than one parent class

Electric Car Petrol Car

Hybrid Car
Multiple Inheritance (example)

class MyClass { // Derived class


public: class MyChildClass : public MyClass, public MyOtherClass
void myFunction() { {
cout << “class 1“ << endl; };
}
};

// Another base class int main() {


class MyOtherClass { MyChildClass myObj;
public: myObj.myFunction();
void myOtherFunction() { myObj.myOtherFunction();
cout << “class 2" << endl; return 0;
} }
};
Hybrid Inheritance

“Inheritance in which the derivation of a class involves several


different types of inheritance is called hybrid inheritance”

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

Which Drive operation Hybrid Car inherits?


Thanks a lot

You might also like