Answer to Question #193473 in Programming & Computer Science for Praveen Kumar Gopa

Question #193473

Design a class template with overloaded operator / to perform a = b / c.


1
Expert's answer
2021-05-17T10:03:07-0400
#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;
}

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