Answer to Question #225516 in C++ for zavier

Question #225516

suppose a company “abc” is conducting a test to hire you as oop developer and asked you develop a calculator for the basic arithmetic operators (+, -, *, /, %), where you have to deal at least 11 digit numbers. the numbers can be of with decimal point or without decimal point. the company wants you to develop an efficient code. you can choose any of the following options to develop this whole scenario:

1 function overloading

2 class template


1
Expert's answer
2021-08-12T16:24:19-0400
#include <iostream>
#include <string>
using namespace std;
//Use the template class
template<typename T>
class Number
{
private:
	T date;//data for example int long long double and
public:
	Number()
	{
		this->date = T();
	}
	Number(const T d)
	{
		this->date = d;
	}
	Number(const Number<T> &a)
	{
		this->date = a.getData();
	}
	T getData()const
	{
		return this->date;
	}
	Number<T>& operator=(const Number<T>a)
	{
		this->date = a.getData();
		return *this;
	}
	friend ostream& operator>>(ostream& cout, const Number& a);	
	//overload operator+
	Number<T> operator+(Number<T> const& a)
	{
		return Number<T>(a.getData() +this->getData());
	}
	//overload operator-
	Number<T> operator-(Number<T> const& a)
	{
		return Number<T>(this->getData()-a.getData());
	}
	//overload operator*
	Number<T> operator*(Number<T> const& a)
	{
		return Number<T>(this->getData() * a.getData());
	}
	//overload operator/
	Number<T> operator/(Number<T> const& a)
	{
		return Number<T>(this->getData() / a.getData());
	}
	//overload operator%
	Number<T> operator%(Number<T> const& a)
	{
		return Number<T>(this->getData() % a.getData());
	}
	friend istream& operator>>(istream& cin, Number<T>& a);
};
template<typename T>
ostream& operator<<(ostream& cout, const Number<T>& a)
{
	cout << a.getData();
	return cout;
}
int main()
{
	cout << "Please enter two number: ";
	long long x, y;
	cin >> x >> y;
	Number<long long>a(x);
	Number<long long>b(y);
	Number<long long>pl = a + b;
	Number<long long>mn = a - b;
	Number<long long>pr = a * b;
	Number<long long>dv = a / b;
	Number<long long>md = a % b;


	cout << "a+b=" << pl << endl;
	cout << "a-b=" << mn << endl;
	cout << "a*b=" << pr << endl;
	cout << "a/b=" << dv << endl;
	cout << "a%b=" << md << endl;
	
	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