Create a class called InputData. It has two private data members data_a and data_b. Set the
values of these two data members 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. Derive a class Logic_Unit from InputData. It
contains the functions and(), or() and xor() to perform logical operations on data_a and data_b.
Finally derive a class called ALUnit from Arith_Unit and Logic_Unit classes. It has to perform
arithmetic and logical operations according to the given codes. Choose code 0 to code 6 to perform
the said seven operations. Write sample program to test the ALU class.
#include <iostream>
using namespace std;
//A class called InputData
class InputData{
private:
// two private data members data_a and data_b.
int data_a;
int data_b;
public:
//Set the values of these two data members by using two public functions get_a() and get_b().
int get_a(){
cout<<"Enter data_a: ";
cin>>data_a;
return data_a;
}
int get_b(){
cout<<"Enter data_b: ";
cin>>data_b;
return data_b;
}
};
//Derive a class called Arith_Unit from InputData.
class Arith_Unit: public InputData{
public:
//It contains the functions add(),sub(), mul(),div() to perform arithmetic operations on data_a and data_b.
int add(){
return (get_a()+get_b());
}
int sub(){
return (get_a()-get_b());
}
int mul(){
return (get_a()*get_b());
}
float div(){
return (get_a()/get_b());
}
};
// Derive a class Logic_Unit from InputData. It contains the functions and(),
//or() and xor() to perform logical operations on data_a and data_b.
class Logic_Unit: public InputData{
public:
int and(){
return (get_a() && get_b());
}
int or(){
return (get_a() || get_b());
}
int xor(){
return (get_a()^get_b());
}
};
//Finally derive a class called ALUnit from Arith_Unit and Logic_Unit classes. It has to perform
//arithmetic and logical operations according to the given codes. Choose code 0 to code 6 to perform
//the said seven operations.
class ALUnit: public Arith_Unit, public Logic_Unit{
public:
void operations(){
Arith_Unit arith_Unit;
Logic_Unit logic_Unit;
int operation=0;
cout<<"0. 'add' operation\n";
cout<<"1. 'sub' operation\n";
cout<<"2. 'mul' operation\n";
cout<<"3. 'div' operation\n";
cout<<"4. 'and' operation\n";
cout<<"5. 'or' operation\n";
cout<<"6. 'xor' operation\n";
cout<<"Select operation: ";
cin>>operation;
if(operation==0){
cout<<"Add function result: "<<arith_Unit.add()<<"\n";
}
if(operation==1){
cout<<"Sub function result: "<<arith_Unit.sub()<<"\n";
}
if(operation==2){
cout<<"Mul function result: "<<arith_Unit.mul()<<"\n";
}
if(operation==3){
cout<<"Div function result: "<<arith_Unit.div()<<"\n";
}
if(operation==4){
cout<<"and function result: "<<logic_Unit.and()<<"\n";
}
if(operation==5){
cout<<"or function result: "<<logic_Unit.or()<<"\n";
}
if(operation==5){
cout<<"xor function result: "<<logic_Unit.xor()<<"\n";
}
}
};
int main()
{
//Write sample program to test the ALU class.
ALUnit ALUnit;
ALUnit.operations();
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment