Operator Overloading

class DummyMath
{
public:
    DummyMath(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
 
    friend const DummyMath operator* (const DummyMath& lhs, const DummyMath& rhs)
    {
        DummyMath temp;
        temp.a = lhs.a * rhs.a;
        temp.b = lhs.b * rhs.b;
 
        return temp;    
    }   
 
    friend std::ostream& operator<< (std::ostream& s, const DummyMath& dummy)
    {
        s << "[a=" << dummy.a << ", b=" << dummy.b << "]";
        return s;
    }
 
    virtual ~DummyMath() {};
 
private:
    int a;
    int b;
};
 
DummyMath dummyA(3,3);
DummyMath dummyB(4,4);
DummyMath dummyC;
 
dummyC = dummyA * dummyB;
 
std::cout << dummyC << std::endl; // [a=12, b=12]

cpp/operator_overloading.txt · Last modified: 2010/08/10 (external edit)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0