Answer to Question #213305 in C++ for Hemambar

Question #213305

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-29T23:31:58-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class inputData{
	
	int data_a,data_b;
	public:
		
		void input()
		{
			cout<<"Enter value of a : ";
			cin>>data_a;
			
			cout<<"Enter value of b : ";
			cin>>data_b;		
		}
		
		void display()
		{
			cout<<"Value of a : ";
			cout<<data_a;
			
			cout<<"Value of b : ";
			cout<<data_b;
		}
		
		int get_a()
		{
			return data_a;
		}
		
		int get_b()
		{
			return data_b;
		}
};


class Arith_Unit:public inputData{
	public:
		int add()
			{
				int n = get_a();
				int m = get_b();
				return n + m;
			}
			
			int sub()
			{
				int n = get_a();
				int m = get_b();
				return n - m;
			}
			
			int mul()
			{
				int n = get_a();
				int m = get_b();
				return n * m;
			}
			
			int div()
			{
				int n = get_a();
				int m = get_b();
				return n / m;
			}
};


class Logic_Unit: public inputData{
	public:
		int And()
		{
			int n = get_a();
			int m = get_b();
			return n&m;
		}
		
		int Or()
		{
			int n = get_a();
			int m = get_b();
			return n|m;
		}
		
		int Xor()
		{
			int n = get_a();
			int m = get_b();
			return n^m;
		}
		
		
};


int main()
{
	Arith_Unit ob1;
	ob1.input();
	cout<<"1. Add"<<endl<<"2. Subtract"<<endl<<"3. Multiply"<<endl<<"4. Divide"<<endl<<endl;
	cout<<"Enter which operation to perform : ";
	int option;
	cin>>option;
	switch(option)
	{
		case 1:
			cout<<ob1.get_a()<<" + "<<ob1.get_b()<<" = "<<ob1.add()<<endl;
			break;
			
		case 2:
			cout<<ob1.get_a()<<" - "<<ob1.get_b()<<" = "<<ob1.sub()<<endl;
			break;
		
		case 3:
			cout<<ob1.get_a()<<" * "<<ob1.get_b()<<" = "<<ob1.mul()<<endl;
			break;
			
		case 4:
			cout<<ob1.get_a()<<" / "<<ob1.get_b()<<" = "<<ob1.div()<<endl;
			break;
	}
	
	cout<<endl;
	Logic_Unit ob2;
	ob2.input();
	
	cout<<"1. And"<<endl<<"2. Or"<<endl<<"3. Xor"<<endl<<endl;
	cout<<"Enter which operation to perform : ";
	int option1;
	cin>>option1;
	switch(option1)
	{
		case 1:
			cout<<ob1.get_a()<<" & "<<ob1.get_b()<<" = "<<ob2.And()<<endl;
			break;
			
		case 2:
			cout<<ob1.get_a()<<" | "<<ob1.get_b()<<" = "<<ob2.Or()<<endl;
			break;
		
		case 3:
			cout<<ob1.get_a()<<" ^ "<<ob1.get_b()<<" = "<<ob2.Xor()<<endl;
			break;
			
	}
	
	
}





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