MUHAMMAD ASIF
SALEEM Ph.D IT
UTHM Malaysia
AGENDA
• Introduction
• Inheritance
• Significance inheritance
• Types of inheritance
• Code Discussion (Examples)
INHERITANCE
The capability of a class to derive
properties and characteristics from
another class is called Inheritance
SIGNIFICANCE OF INHERITANCE
Code Reusability Extensibility and Scalability
Promotes Hierarchical Classification
Reduces Development Time Maintainability
TYPES OF INHERITANCE
Multilevel
Inheritance
Multiple Inheritance
Hierarchical
Inheritance
Single Inheritance Inheritance Hybrid
Inheritance
SINGLE INHERITANCE
A Parent Class
Inherits
B Child Class
MULTIPLE INHERITANCE
Parent Class 1 Parent Class 2
Inherits Inherits
Child Class
MULTILEVEL INHERITANCE
Parent Class
Inherits
Child Class 1
Inherits
Child Class 2
HIERARCHICAL INHERITANCE
Parent Class
Inherits Inherits Inherits
Child Class 1 Child Class 2 Child Class 2
HYBRID INHERITANCE
Parent Class Inherits
Inherits
Inherits
Child Class 1 Child Class 2
Inherits
Inherits
Child Class 2
PRACTICAL DEMONSTRATIONS
MULTILEVEL INHERITANCE
// Base class (parent)
class MyClass {
public:
void myFunction() {
Parent Class
cout << "Some content in parent
class." ;
}
Inherits
};
// Derived class (child)
class MyChild: public MyClass {
Child Class 1
};
// Derived class (grandchild)
Inherits
class MyGrandChild: public MyChild {
};
int main() {
Child Class 2
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
MULTIPLE
// Base class
INHERITANCE
class MyClass {
public:
void myFunction() {
cout << "Some content in parent
class." ;
}
};
// Another base class
class MyOtherClass {
Parent Class 1 Parent Class 2
public:
void myOtherFunction() { Inherits
cout << "Some content in another Inherits
class." ;
};
}
Child Class
// Derived class
class MyChildClass: public MyClass, public M
yOtherClass {
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
SINGLE INHERITANCE
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl; A
}
}; Inherits
// Derived class (inherits from Animal)
class Dog : public Animal {
public:
B
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Own method
return 0;
}
THANK YOU