Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typecasting in c++

Reply
Thread Tools

typecasting in c++

 
 
Archanasravan Archanasravan is offline
Junior Member
Join Date: Jan 2013
Posts: 1
 
      01-30-2013
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList typecasting from binary SQL data =?Utf-8?B?Smlt?= ASP .Net 1 04-11-2005 04:49 PM
Advanced pointer typecasting Robert Street C++ 3 02-21-2004 01:33 AM
Typecasting char pointer to structure venkatesh C++ 1 12-06-2003 06:46 AM
Typecasting operator on simple types vs. classes Nicolay Korslund C++ 7 09-30-2003 04:33 PM
Understanding Typecasting in C++ Kapil Khosla C++ 3 07-20-2003 11:40 AM



Advertisments