#include<iostream> class Foo { protected: // Make x visible to derived classes int x; public: Foo() { x = 2; } }; class Derived : public Foo { public: Derived() { x = 4; } void print(){ std::cout << x << std::endl; } }; int main() { Derived a; a.print(); } This prints 4.I want to access both the values of x in print.I want to print 2 and 4 both.Do I need to creat object of Foo in Derived class and access it through object.x?But that calls the constructor of Foo more than once.I don't want that to happen.
Derivedclass?std::cout << 2 << 4 << '\n'? Seriously, what do you mean?