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
1.Single Inheritance:
-When a parent have only one child then it is called single Inheritance.
Syntax-
class parent
{
};
class child: public parent
{
};
program:
#include< iostream>
using namespace std;
class Home{
public:
int price=10000;
};
class Room: public Home {
public:
int count=5;
};
int main(void) {
Room obj;
cout<<"Price is: "<< obj.price<< endl;
cout<<"Number of Room: "<< obj.count<< endl;
return 0;
}
Price is: 10000
Number of Room: 5