The Gateway to Computer Science Excellence
0 votes
36 views
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]
in Object Oriented Programming by Junior (853 points) | 36 views

Please log in or register to answer this question.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,291 answers
198,209 comments
104,891 users