#include <iostream>
usingnamespace std;
class ABC
{
public:
int a = 10;
};
class XYZ : public ABC
{
public:
int a = 20;
};
int main()
{
XYZ obj;
cout << "XYZ.obj = " <<obj.a<<endl; //value printed is 20, means overriding of variable is there, Is there any significance/usecase of such variable overriding?
return 0;
}
It's not really "overriding" the variable. The object contains both variables. The variable defined in XYZ overshadows the variable with the same name in ABC. You can still access both variables if you explicitly specify which class you want to read the variable from.