Answer to Question #213002 in C++ for Hemambar

Question #213002
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 performarithmetic 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 performarithmetic and logical operations according to the given codes. Choose code 0 to code 6 to performthe said seven operations. Write sample program to test the ALU class
1
Expert's answer
2021-07-05T04:26:04-0400
#include <iostream>
using namespace std;
template<typename T>
class InputData{
private:
 T data_a;
 T data_b;
 void get_a(T a){data_a=a;}
 void get_b(T b){data_b=b;}
};
template<typaname T>
class Arith_Unit: public InputData<T>{
public:
 T add(){return data_a+data_b;}
 T sub(){return data_a-data_b;}
 T mul(){return data_a*data_b;}
 T div(){return data_a/data_b;}
};
template<typename T>
class Logic_Unit: InputData<T>{
public:
 T and(){return data_a&data_b;}
 T or(){return data_a|data_b;}
 T xor(){return data_a^data_b;}
};
template<typename T>
class ALUnit: public Arith_Unit<T>, Logic_Unit<T>{
private:
 Arith_Unit arith;
 Logic_Unit logic;
public:
 ALUnit(T a, T b){
  arith = new Arith_Unit();
  arith.get_a(a);
  arith.get_b(b);
  logic = new Logic_Unit();
  logic.get_a(a);
  logic.get_b(b);
 }
 T op(int choice) {
  switch(choice){
   case 0:
    return arith.add();
   case 1:
    return arith.sub();
   case 2:
    return arith.mul();
   case 3:
    return arith.div();
   case 4:
    return logic.and();
   case 5: 
    return logic.or();
   case 6:
    return logic.xor();
  }
 }
};
int main(){
 
 return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS