I have a class with a function and a subclass.
In the following code, I want to override the int x
in a way that d.getX()
return 40.
using std::cout, std::endl;
class Base {
protected:
int x;
int y;
public:
Base(int y) : y(y) {};
int getX() {
return x;
}
};
class Derived : public Base {
protected:
int x = 40;
public:
using Base::Base;
};
int main() {
Base d(10);
cout << d.getX() << endl; // want to print 40
return 0;
}
Is this possible? Thank you!