Add two numbers using single inheritance
Q. Write a C++ program to add two numbers using single inheritance. Accept these two numbers from the user in base class and display the sum of these two numbers in derived class.
- Inheritance is the most important concept of object oriented programming.
- Deriving a class from another class is known as Inheritance.
Following program is demonstrating single inheritance.
using namespace std;
class AddData //Base Class
int num1, num2;
cout<<"\n Enter First Number : ";
cout<<"\n Enter Second Number : ";
class Addition: public AddData //Class Addition – Derived Class
sum = num1 + num2;
cout<<"\n Addition of Two Numbers : "<<sum;
In the above program, the base class AddData has two protected member variables namely num1 and num2. Class Addition is derived from the base class AddData.
The members of the base class are available in the derived class. This concept of accessing the members of the base class is called as Code Reusability.