Design a class template with overloaded operator / to perform a = b / c.
#include <iostream>
template<class T>
class Divisor
{
public:
T value;
Divisor<T> operator/(const Divisor<T>& other)
{
Divisor<T> result;
result.value = value / other.value;
return result;
}
};
int main()
{
Divisor<int> a1;
a1.value = 10;
Divisor<int> b1;
b1.value = 2;
Divisor<int> c1 = a1 / b1;
std::cout << c1.value << std::endl;
Divisor<float> a2;
a2.value = 10;
Divisor<float> b2;
b2.value = 3;
Divisor<float> c2 = a2 / b2;
std::cout << c2.value << std::endl;
return 0;
}
Comments
Leave a comment