How to change data of base class from derived C++

  c++, class

i have two classes one base and one derived from it. Im trying to do some calculations in the derived class but i want it to change directly the data from the base class. Here some code for example.I have them separated in files.

Base.cpp

class Base
{
public:

int base_data;

void fun()
{
    Derived d;
    d.DerivedFun();
};

Derived.cpp

class Derived :public Base
{

void DerivedFun()
{
    base_date=4;
}

};

So what i want is to be able to change the data from the derived class and when i try to print base_data from a Base class object it will be 4 ( from the example above) . If i can give you more info ill be glad to add.

Source: Windows Questions C++

LEAVE A COMMENT