I have a base class and a derived class. Inside the base class is a value, and a function virtual modify(int val) that subtracts that value from the member. Additionally, the derived class overrides modify(int val), which does some checking before calling the base class' method.
When this is called by using derivedObject.modify(someVal) in main(), everything works fine. However, when I call a function modifyAndPrintValue(Base obj), which calls this modify(), the call pattern correctly hits Derived::modify() => Base::modify(), but the next time I print, the value stored in the Base class is not modified (it is the original value).
class Base
{
public:
int value;
void virtual modify(val) {value -= val;}
int getValue() {return value;}
}
class Derived: public Base
{
public:
void modify(val) {Base::modify(val);}
}
void modifyAndPrint(Base obj)
{
obj.modify(5);
cout << obj.getValue(); // should print 9 -5 = 4
}
int main()
{
Derived derivedObj(); //assume this initializes the object with a value, say 10
derivedObj.modify(1);
cout << derivedObj.getValue(); // returns correct value, 9 (10 - 1)
modifyAndPrint(derivedObj); // does not print correct value, still prints 9
}
What am I doing wrong?