Inheritance (Object-Oriented Programming)
inheritance is the another properties of OOPS .The process of creating a new class from existing class .new class inherit some of the properties and behaviour of the existing class.
where A is base class and B is the derived class
Inheritance is an OOP concept where one class (called the child / subclass / derived class) acquires the properties and behaviors (variables and methods) of another class (called the parent / superclass / base class).
It promotes code reusability, extensibility, and maintainability.
Example:
If Animal is a parent class and Dog is a child class, then Dog can use the features of Animal and also have its own additional features
Types of Inheritance.
1.Single inheritance: It is the types of inheritance in which it has one base class and one derived class.
base → derive
Advantages:
-
Simple and easy to understand
-
Improves code reuse
#include<iostream>
using namespace std;
class base
{
private:
int a,b;
public:
void input()
{
cout<<" enter the value of a and b"<<endl;
cin>>a>>b;
}
void show()
{
cout<<"a="<<a<<"b="<<b<<endl;
}
};
class derive:public base
{
private:
int m,n;0
public:
void input1()
{
cout<<" enter the value ofm and n"<<endl;
cin>>m>>n;
}
void show1()
{
cout<<"m="<<m<<"n="<<n<<endl;
}
};
int main()
{
derive obj;
obj.input();
obj.show();
return 0;
}