Answer to Question #213176 in C++ for Hemambar

Question #213176

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-20T17:05:48-0400
#include<iostream>
#include<sstream> 
#include<string>
using namespace std;
class InputData{
	private:
	int	data_a, data_b;
	public:
		void set_A(int a){
			data_a = a; 
		}
		void set_B(int b ){
			data_b = b;
		}
	int	get_a(){
		return data_a;
	} 
	int get_b(){
		return data_b;
	}
};
class Arith_Unit: public InputData{
	public:
	




		void display(){
			cout<<"The value of data_a is\t"<<get_a()<<endl;
			cout<<"The value of data_b is\t"<<get_b()<<endl;
		}
	  
		
		int	add(){
			return get_a() + get_b();
		}
		int sub(){
			return get_a() + get_b();
		}
		int  mul(){
			return get_a() * get_b();
		}
		double div(){
			return get_a()/get_b();
		} 
};
 class Logic_Unit: public InputData{
 	public:
 		////Getting the data_a in binary form 
 		string display_A(){
 			string bin = "";
 			
int a[10], n, i;    
n = get_a();    
for(i=0; n>0; i++)    
{    
a[i]=n%2;    
n= n/2;  
}   


   
for(i=i-1 ;i>=0 ;i--)    
{    


string h = to_string(a[i]);
bin = bin + h;


}    
return bin;
		
}
 	//Getting the data_b in binary form 
 	string display_B(){
 			string bin = "";
 			
int a[10], n, i;    
n = get_b();    
for(i=0; n>0; i++)    
{    
a[i]=n%2;    
n= n/2;  
}   


   
for(i=i-1 ;i>=0 ;i--)    
{    


string h = to_string(a[i]);
bin = bin + h;


}    
return bin;
		
}
//and, or, xor are key words
void and1(){
	string str = display_A();
	string str1 = display_B(); 
    char arr[str.length() + 1]; 
    char arr1[str1.length() + 1]; 
    
    for (int x = 0; x < sizeof(arr); x++) { 
        arr[x] = str[x]; 
        
		arr1[x] = str1[x]; 
        
    } 
   
    for (int x = 0; x < sizeof(arr) - 1; x++) { 
    
     if((arr[x]== '1') && (arr1[x]== '1')){
     	cout<<arr[x];
	 }
	 else{
	 	cout<<0;
	 }
         
        
    } 
} 
void or1(){
		string str = display_A();
	string str1 = display_B(); 
    char arr[str.length() + 1]; 
    char arr1[str1.length() + 1]; 
    
    for (int x = 0; x < sizeof(arr) - 1; x++) { 
        arr[x] = str[x]; 
        
		arr1[x] = str1[x]; 
        
    } 
   
    for (int x = 0; x < sizeof(arr) - 1; x++) { 
    
     if((arr[x]== '1') || (arr1[x]== '1')){
     	cout<<arr[x];
	 }
	 else{
	 	cout<<0;
	 }
         
        
    } 
	
} 
void xor1(){
	string str = display_A();
	string str1 = display_B(); 
    char arr[str.length() + 1]; 
    char arr1[str1.length() + 1]; 
    
    for (int x = 0; x < sizeof(arr); x++) { 
        arr[x] = str[x]; 
        
		arr1[x] = str1[x]; 
        
    } 
   
    for (int x = 0; x < sizeof(arr) - 1; x++) { 
    
     if((arr[x]== '1') && (arr1[x]== '0')){
     	cout<<arr[x];
	 }
	 else if((arr[x]== '0') || (arr1[x]== '1')){
	 		cout<<arr[x];
	 }
	 else{
	 	cout<<0;
	 }
         
        
    } 
	
}


void disp(){
	cout<<"\n data_a in the binary form\t"<<display_A();
	cout<<"\n data_b in the binary form\t"<<display_B();
}
	
 };
int main(){
	InputData data;
	data.set_A(20);
	data.set_B(30);
	Arith_Unit unit;
	unit.set_A(10);
	unit.set_B(9);
	unit.display();
	cout<<endl;
	
	Logic_Unit logic;
	logic.set_A(10);
	logic.set_B(9);
	logic.disp();
	int code;
	cout<<"\nChoose code between 0 to 6 to perform\n";
	cin>>code;
	if(code==0){
		cout<<"The addition of the two numbers is\t"<<unit.add();
	}
	else if(code==1){
			cout<<"\nThe subtraction of the two numbers is\t"<<unit.sub();
	}
	else if(code==2){
		cout<<"\nThe multiplication of the two numbers is\t"<<unit.mul();
	}
	else if(code==3){
		cout<<"\nThe division of the two numbers is\t"<<unit.div();
	}
	else if(code == 4){
		cout<<"\nPerforming and() operation\n";
	logic.and1();
	}
	else if(code==5){
		cout<<"\nPerforming or() operation\n";
	logic.or1();
	}
	else if(code == 6){
	cout<<"\nPerforming xor1() operation\n";
	logic.xor1();
	}
	
	
	
	




}

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