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
#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;
}
Comments
Leave a comment