Create a class called InputData. It has two private data members data_a (int) and data_b (int). Write a function input() to get input for the attributes from the user and a function display() to display the values of the attributes. The values of these two data members can be returned by using two public functions get_a() and get_b().
*Derive a class called Arith_Unit from InputData. It contains the functions add(), sub(),mul(), div() to perform arithmetic operations on data_a and data_b.
#include<iostream>
using namespace std;
class InputData{
private:
int data_a;
int data_b;
public:
void input(){
cout<<"Enter the first number"<<endl;
cin>>data_a;
cout<<"Enter the first number"<<endl;
cin>>data_b;
}
int get_a(){
return data_a;
}
int get_b(){
return data_b;
}
void display(){
cout<<"The value of first number is "<<get_a()<<endl;
cout<<"The value of second number is "<<get_b()<<endl;
}
};
class Arith_unit:public InputData{
public:
double add(){
return get_a() + get_b();
}
double sub(){
return get_a() - get_b();
}
double mul(){
return get_a() * get_b();
}
double div(){
return get_a() / get_b();
}
};
int main(){
Arith_unit t;
t.input();
cout<<"The sum is\t"<<t.add()<<endl;
cout<<"The difference is\t"<<t.sub()<<endl;
cout<<"The multiplication is\t"<<t.mul()<<endl;
cout<<"The division is\t"<<t.div()<<endl;
}
Comments
Leave a comment