Answer to Question #214412 in C++ for Hemambar

Question #214412

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


1
Expert's answer
2021-07-13T02:27:08-0400
#include <iostream>
using namespace std;


class InputData{
private:
	int data_a;
	int data_b;
public:
	int get_a(){
		cout<<"Enter a: ";
		cin>>data_a;
		return data_a;
	}
	int get_b(){
		cout<<"Enter b: ";
		cin>>data_b;
		return data_b;
	}
};


class Arith_Unit: public InputData{
public:
	int add(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a+data_b;
	}
	int sub(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a-data_b;
	}
	int mul(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a*data_b;
	}
	float div(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a/data_b;
	}
};
class  Logic_Unit: public InputData{
public:
	int and(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a & data_b;
	}
	int or(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a | data_b;
	}
	int xor(){
		int data_a=get_a();
		int data_b=get_b();
		return data_a ^ data_b;
	}
};
class  ALUnit: public Arith_Unit, public Logic_Unit{
public:
	void operations(){
		Arith_Unit AU;
		Logic_Unit LU;
		int choice=0;
		cout<<"0. Add \n";
		cout<<"1. Sub\n";
		cout<<"2. Mul\n";
		cout<<"3. Div\n";
		cout<<"4. And\n";
		cout<<"5. Or\n";
		cout<<"6. Xor\n";
		cout<<"Your choice: ";
		cin>>choice;
		if(choice==0){
			cout<<"Add: "<<AU.add()<<"\n";
		}else if(choice==1){
			cout<<"Sub: "<<AU.sub()<<"\n";
		}else if(choice==2){
			cout<<"Mul: "<<AU.mul()<<"\n";
		}else if(choice==3){
			cout<<"Div: "<<AU.div()<<"\n";
		}else if(choice==4){
			cout<<"And: "<<LU.and()<<"\n";
		}else if(choice==5){
			cout<<"Or: "<<LU.or()<<"\n";
		}else if(choice==6){
			cout<<"Xor: "<<LU.xor()<<"\n";
		}else{
			cout<<"\nWrong menu item.\n\n";
		}
	}
};
int main(){


	 ALUnit AL;
	 AL.operations();
	
	


	cout<<"\n";
	system("pause");
	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