What is Inheritance in C++ :
In C++,
-Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.
Type of Inheritance in C++:
-Single Inheritance
-Multiple Inheritance
-Multilevel Inheritance
-Hierarchical inheritance
-Hybrid inheritance
Multilevel Inheritance:
-When child class work as parent for another third class then it is called as Multi-level Inheritance.
Syntax-
class one
{
};
class two:public one
{
};
class three:public two
{
};
program:
#include < iostream>
using namespace std;
class Animal{
public:
void drink() {
cout<<"Drinking.."<< endl;
}
};
class Dog: public Animal
{
public:
void eat(){
cout<<"Eating.."<< endl;
}
};
class BabyDog: public Dog
{
public:
void donothing() {
cout<<"Nothing";
}
};
int main(void) {
BabyDog d1;
d1.drink();
d1.eat();
d1.donothing();
return 0;
}
Drinking
Eating
Nothing