S
somenath
I have come to know from C++ book that "a single object (e.g an object of type derived) might have some more than one address (e.g its address when pointed to by Base * pointer and its address when pointed to by a Derived* pointer). That cannot happen in C"
So to understand this concept I wrote the following program
#include<iostream>
using namespace std;
class Base { };
class Derivedublic Base
{
};
int main (void) {
Derived d;
Derived *d1;
Base *bp = &d;
d1= &d;
cout<<"bp = "<<bp<<endl;
cout<<"&d = "<<&d<<endl;
cout<<"d1 = "<<d1<<endl;
return 0;
}
===
Output
bp = 0x28ac57
&d = 0x28ac57
d1 = 0x28ac57
====
My understanding from the above mentioned text is as follows.
The object "d" is pointed by bp (base class pointer) and derived class pointer d1.
I was expecting value of d1 and bp will be different as they are of type derived and base class.
I think my understanding about the above mentioned the text is wrong.
Please help me to understand the following concept
"a single object (e.g an object of type derived) might have some more than one address (e.g its address when pointed to by Base * pointer and its address when pointed to by a Derived* pointer). That cannot happen in C".
How a single object can have multiple address?
Along with this I am interested to know how the C++ compiler restrict the access of function defined only in derived class from a base class pointer.
For example in the following code
class Base { };
class Derivedublic Base
{
public:
void print()
{
cout<<"Inside derived "<<endl;
}
};
int main (void) {
Derived d;
Base *bp = &d;
bp->print();
return 0;
}
how the compiler makes sure that "bp" can not access "print" ?
Could you please provide me some resources which explain this topics?
So to understand this concept I wrote the following program
#include<iostream>
using namespace std;
class Base { };
class Derivedublic Base
{
};
int main (void) {
Derived d;
Derived *d1;
Base *bp = &d;
d1= &d;
cout<<"bp = "<<bp<<endl;
cout<<"&d = "<<&d<<endl;
cout<<"d1 = "<<d1<<endl;
return 0;
}
===
Output
bp = 0x28ac57
&d = 0x28ac57
d1 = 0x28ac57
====
My understanding from the above mentioned text is as follows.
The object "d" is pointed by bp (base class pointer) and derived class pointer d1.
I was expecting value of d1 and bp will be different as they are of type derived and base class.
I think my understanding about the above mentioned the text is wrong.
Please help me to understand the following concept
"a single object (e.g an object of type derived) might have some more than one address (e.g its address when pointed to by Base * pointer and its address when pointed to by a Derived* pointer). That cannot happen in C".
How a single object can have multiple address?
Along with this I am interested to know how the C++ compiler restrict the access of function defined only in derived class from a base class pointer.
For example in the following code
class Base { };
class Derivedublic Base
{
public:
void print()
{
cout<<"Inside derived "<<endl;
}
};
int main (void) {
Derived d;
Base *bp = &d;
bp->print();
return 0;
}
how the compiler makes sure that "bp" can not access "print" ?
Could you please provide me some resources which explain this topics?