typecasting in c++


Joined
Jan 30, 2013
Messages
1
Reaction score
0
Hello All,

I want to clearly how typecastiong works.Here I have taken two classes.Independent classes.Class FIrst contains datamembers and a result function which adds two datamemebers and I am passing values by using paramaterised constructor.

class Second contains datamembers and paramerized constructor.


#include<iostream>
class FIrst
{
public:
int a,b;
FIrst()
{
}
FIrst(int x,int y)
{
a=x;
b=y;
}


int result()
{
return a+b;
}
};
class SEcond
{
public:
int c,d;
SEcond()
{
}
SEcond(int m,int n)
{
c=m;
d=n;
}

};


int main()
{
FIrst f(2,3),*fp;
fp=new FIrst();

SEcond se(8,7);

fp=(FIrst*)&se;
std::cout<<fp->result();
return 0;
}



Here.FIrst class pointer is pointing to SEcond class object.

fp=(FIrst*)&se;

How come it is showing the result of 8 aand 7(ans:15) even though pointer is pointing to SEcond class object?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top