K
Kapil Khosla
Hi,
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.
I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.
Base *ba = new Base;
Derived *de = new Derived;
Now when I do something like
Base *one = (Base*)de;
What is really happening here ? "de" was an object of class Derived
and had data specific to that class. Now by typecasting I have access
to all the functions and data of the Base class but where is the data
of my Derived class object , I no longer have access to it, if I do
one-> ....(derived class data)
Thanks,
Kapil
#include "stdafx.h"
// Using pragmas to see the difference in programs in binary level
using namespace std;
class Base
{
public:
int a,b;
void print();
};
class Derived : public Base
{
public:
int c,d;
void print();
};
void Base:rint()
{
cout << "In Base" << endl;
}
void Derived:rint()
{
cout <<"In Derived" << endl;
}
int main()
{
Base ba;
Derived de;
Base *a = new Base; // a is a pointer to the Base object
Derived *b = new Derived; // b is the pointer to the Derived object
Base *c = (Base*)b;
c->print();
Derived *d = (Derived*)b;
d->print();
}
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.
I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.
Base *ba = new Base;
Derived *de = new Derived;
Now when I do something like
Base *one = (Base*)de;
What is really happening here ? "de" was an object of class Derived
and had data specific to that class. Now by typecasting I have access
to all the functions and data of the Base class but where is the data
of my Derived class object , I no longer have access to it, if I do
one-> ....(derived class data)
Thanks,
Kapil
#include "stdafx.h"
// Using pragmas to see the difference in programs in binary level
using namespace std;
class Base
{
public:
int a,b;
void print();
};
class Derived : public Base
{
public:
int c,d;
void print();
};
void Base:rint()
{
cout << "In Base" << endl;
}
void Derived:rint()
{
cout <<"In Derived" << endl;
}
int main()
{
Base ba;
Derived de;
Base *a = new Base; // a is a pointer to the Base object
Derived *b = new Derived; // b is the pointer to the Derived object
Base *c = (Base*)b;
c->print();
Derived *d = (Derived*)b;
d->print();
}