search
Log In
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
0 votes
61 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 61 views

Please log in or register to answer this question.

Related questions

1 vote
0 answers
1
96 views
Give a simple example on inheriting the operator function and using it in programme
asked Apr 22, 2018 in Object Oriented Programming anonymous 96 views
2 votes
1 answer
2
1.9k views
When the inheritance is private, the private methods in base class are _____ in the derived class (in C++) inaccessible accessible protected public
asked Jul 30, 2016 in Object Oriented Programming jothee 1.9k views
2 votes
3 answers
3
1.2k views
Which of the following, in C++, is inherited in a derived class from base class? constructor destructor data members virtual methods
asked Jul 30, 2016 in Object Oriented Programming jothee 1.2k views
1 vote
1 answer
4
358 views
_____ allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own members. Overloading Inheritance Polymorphism Encapsulation
asked Jul 25, 2016 in Object Oriented Programming jothee 358 views
...