There are 5 types of Inheritance in C++:
A class derives from only one base class in single inheritance. This means that there is only one subclass descended from a single superclass.
Syntax:
class base {
//Body of the base class
};
class derived : access_specifier base {
//Body of the derived class
};
#include <iostream>
using namespace std;
class Animal {
public:
void fun1() {
cout<<"Animal Class"<<endl;
}
};
class Dog : public Animal {
public:
void fun2() {
cout<<"I am a dog"<<endl;
}
};
int main() {
Dog obj;
obj.fun1();
obj.fun2();
return 0;
}
Animal Class
I am a dog
Multiple inheritance is a type of inheritance in which a class is derived from various classes. Class C, as illustrated in the diagram above, is a subclass of classes A and B.
Syntax:
class base1 {
//body
};
class base2 {
//body
};
.
.
.
class derived : access_specifier base1, access_specifier base2, ... {
//body of the derived class
};
#include <iostream>
using namespace std;
class A {
protected:
int a;
public:
void seta(int x) {
a = x;
}
};
class B {
protected:
int b;
public:
void setb(int y) {
b = y;
}
};
class C : public A, public B {
public:
int add() {
cout<<"Addition of two numbers = "<<a+b;
}
};
int main() {
C obj;
obj.seta(4);
obj.setb(9);
obj.add();
return 0;
}
Addition of two numbers = 13
In hierarchical inheritance, more than one class inherits from a single base class as shown in the representation above. This gives it a structure of hierarchy.
#include <iostream>
using namespace std;
class Values {
protected:
double a, b;
public:
void initialize(double x, double y) {
a = x;
b = y;
}
};
class A : public Values {
public:
void add() {
cout<<"addition = "<<a+b<<endl;
}
};
class B : public Values {
public:
void subtract() {
cout<<"subtraction = "<<a-b<<endl;
}
};
int main() {
A obj1;
B obj2;
obj1.initialize(4.5,8.7);
obj1.add();
obj2.initialize(3.6,11);
obj2.subtract();
return 0;
}
addition = 13.2
subtraction = -7.4
A class is derived from another derived class in multilevel inheritance. If our implementation is correct, this inheritance can have as many levels as we want.
#include <iostream>
using namespace std;
class Animal {
public:
void fun1() {
cout<<"Animal"<<endl;
}
};
class PetAnimal : public Animal {
public:
void fun2() {
cout<<"Pet animal"<<endl;
}
};
class Dog : public PetAnimal {
public:
void fun3() {
fun1();
fun2();
cout<<"Dog"<<endl;
}
};
int main() {
Dog obj;
obj.fun3();
return 0;
}
Animal
Pet animal
Dog
Hybrid Inheritance is a combination of Hierarchical and Multilevel Inheritance.
Note: also read about Inheritance in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…