1

This is a simplified version of my problem, I want to modify the variable(2d vector in my original code) in base class using function from 2 child classes, while keeping the modified variable and displaying it.

Basically I want to modify the variable declared in base class, by calling functions with the same name from different child classes, and the modified variable will be shared with all child classes. Sorry for my bad understanding of polymorphism, still trying to digest through.

PS:I removed the constructor and virtual destructor from below because stackoverflow won't let me through otherwise.

#include <iostream>

using namespace std;

class Shape
{
    protected:
    int test[3];

    public:
    virtual void chgValue() {}
    void setInitialValue();
    void showValue();
};

void Shape::setInitialValue() //sets test to {1,2,3}
{
    test[0]=1;
    test[1]=2;
    test[2]=3;
}

void Shape::showValue() //display elements of test
{
    for(int i=0;i<3;i++)
        cout<<test[i]<<" ";
}

class Square : public Shape //child class 1
{
    public:
    void chgValue()
    {
        test[1]=5;
    }
};

class Triangle : public Shape //child class 2
{
    public:
    void chgValue()
    {
        test[2]=7;
    }
};

int main()
{
    Shape a;
    Square b;
    Triangle c;

    Shape *Shape1=&b;
    Shape *Shape2=&c;

    a.setInitialValue(); //sets test to {1,2,3}
    Shape1->chgValue(); //change test[1] to 5
    Shape2->chgValue(); //change test[2] to 7
    a.showValue();  //shows 1 2 3 instead of 1 5 7

    return 0;
}

Expected output is 1 5 7, but the actual output is 1 2 3.

yun1001
  • 13
  • 4
  • `a`, `b`, and `c` are all separate instances – paddy Jan 15 '19 at 02:26
  • Try to call `Shape1->showValue()`, `Shape2->showValue()`. – 273K Jan 15 '19 at 02:26
  • Maybe you want `static int test[3];`. It shares `test` between all instances. – 273K Jan 15 '19 at 02:28
  • Shape1->showValue(), Shape2->showValue() will show random numbers except for the modified one, static gives undefined reference to 'Shape::test' – yun1001 Jan 15 '19 at 02:46
  • I've provided you with the hints, not solutions, since your aim is unclear. If you don't understand them and can't make the code well I recommend you to start from https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 273K Jan 15 '19 at 03:05
  • @yun1001 Please check my updated answer! – duong_dajgja Jan 15 '19 at 03:36

1 Answers1

2

I would guess you have mis-understood concepts of OOP here:

Shape a;
Square b;
Triangle c;

This means you have defined three different objects and they occupy three separate memory regions in RAM.

a.setInitialValue();

This simply sets elements of int test[3]; of the a object.

Shape *Shape1=&b;
Shape *Shape2=&c;

Shape1->chgValue();
Shape2->chgValue();

This should change elements of int test[3]; of the b and c objects respectively; and this does NOT affect to the a object.

After all, elements of int test[3]; of:

  • the a: 1 2 3
  • the b: x 5 x
  • the c: x x 7

Note: x here means undetermined (it could be some garbage left in the RAM).


Updated to address OP's comments

If you really want to "modify the variable declared in base class, by calling functions with the same name from different child classes, and the modified variable will be shared with all child classes" then you could declare int test[3]; in Shape static as follows:

class Shape
{
  protected:
    static int test[3];

  public:
    // other code goes here
    // ...
};

int Shape::test[3];

// other code goes here
// ...
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65