Types of Inheritance
Types of Inheritance
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
Multiple Inheritance
Hierarchical
Inheritance
A Parent Class
Inherits
B Child Class
MULTIPLE INHERITANCE
Inherits Inherits
Child Class
MULTILEVEL INHERITANCE
Parent Class
Inherits
Child Class 1
Inherits
Child Class 2
HIERARCHICAL INHERITANCE
Parent Class
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
};
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;