Answer to Question #215266 in C++ for Dharshini Priya

Question #215266
Create a class called InputData. It has two private data members data_a (int) and data_b (int). Write a function input() to get input for the attributes from the user and a function display() to display the values of the attributes. The values of these two data members can be returned 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 bitwise operations on data_a and data_b.

Write a main() to test functionalities of the Arith_unit and Logic_unit classes.
1
Expert's answer
2021-07-09T00:28:22-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