Please explain the working of this code:-
#include<iostream>
using namespace std;
class Base
{
public:
int x;
Base():x(10){}
~Base(){}
};
class Derived:public Base
{
public:
int y;
Derived():y(20){}
~Derived(){}
};
int main()
{
Base bobj,*bptr;
Derived dobj,*dptr;
bptr=(Base *)&dobj;
cout<<bptr->x<<endl;
//ERROR:cout<<bptr->y<<endl; no access to derived members //
dptr=(Derived*)&bobj;
cout<<dptr->x<<endl;
cout<<dptr->y<<endl;
}
Output:-10
10
0
[Finished in 2.7s]