polymorphism in C++

Posted by user550413 on Stack Overflow See other posts from Stack Overflow or by user550413
Published on 2011-01-10T20:13:08Z Indexed on 2011/01/10 20:53 UTC
Read the original article Hit count: 117

Filed under:
|

I am trying to implement the next 2 functions

Number& DoubleClass::operator+( Number& x);
Number& IntClass::operator+(Number& x);

I am not sure how to do it..(their unidirectionality is explained below):

   class IntClass;
   class DoubleClass;

class Number {
        //return a Number object that's the results of x+this, when x is either
        //IntClass or DoubleClass
        virtual Number& operator+(Number& x) = 0;
};


class IntClass : public Number {
    private:
        int my_number;
        //return a Number object that's the result of x+this.
        //The actual class of the returned object depends on x.
        //If x is IntClass, then the result if IntClass.
        //If x is DoubleClass, then the results is DoubleClass.
    public:
        Number& operator+(Number& x);
};


class DoubleClass : public Number {
    private:
        double my_number;
    public:

        //return a DoubleClass object that's the result of x+this.
        //This should work if x is either IntClass or DoubleClass
        Number& operator+( Number& x);
};

© Stack Overflow or respective owner

Related posts about c++

Related posts about polymorphism