0

Am learning CPP and in the below code snippet, I am expecting 40 as the answer, but getting 30. Can anyone please help to identify what am I doing wrong.

The program may not make sense, but its just for learning.

class Shape {

protected:
    int shape = 10;
public:

    int get() {
        return shape;
    }
    virtual void process() {
        shape = 30;
    }
};

class Rectangle : public Shape {

public:
    void process() {
        shape = 40;
    }

};


class Object : Shape {

    Shape m_sh;
public:
    Object(Shape &sh) {
        m_sh = sh;
        m_sh.process();
    }

    int getShape() {
        return m_sh.get();
    }

};

int main(int argc, char *argv[]) {

    Rectangle rect;
    Object obj1 = Object(rect);

    std::cout << obj1.getShape() << std::endl; // am expecting 40, but getting 30.

}
infoclogged
  • 3,641
  • 5
  • 32
  • 53

0 Answers0