291 views
0 votes
0 votes
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]

Please log in or register to answer this question.

Related questions

1 votes
1 votes
0 answers
1
anonymous asked Apr 22, 2018
267 views
Give a simple example on inheriting the operator function and using it in programme
2 votes
2 votes
1 answer
2
go_editor asked Jul 30, 2016
7,466 views
When the inheritance is private, the private methods in base class are _____ in the derived class (in C++)inaccessible accessibleprotectedpublic
2 votes
2 votes
3 answers
3
go_editor asked Jul 30, 2016
6,795 views
Which of the following, in C++, is inherited in a derived class from base class?constructordestructordata membersvirtual methods