Look at this example
class base {
public:
int m1;
base() {
m1 = 5;
}
};
class der: public base {
public:
int m1;
der() {
m1 = 6;
}
};
int main() {
der d;
cout << d.m1;
return 0;
}
Here size of object d is 8 byte, which is allocated for 2 m1(one for base class and other for derive class). What is the mechanism to resolve d.m1?
The
m1inderhides the name ofbase‘sm1, so any access tom1via aderobject gets you that instance’sder::m1. You can accessbase::m1inderlike this:And you can access the base object thus: