typecasting in c++

Discussion in 'C Programming' started by Archanasravan, Jan 30, 2013.

  1. Archanasravan

    Archanasravan

    Joined:
    Jan 30, 2013
    Messages:
    1
    Likes Received:
    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?
     
    Archanasravan, Jan 30, 2013
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. =?Utf-8?B?Smlt?=

    ArrayList typecasting from binary SQL data

    =?Utf-8?B?Smlt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    689
    Bruce Barker
    Apr 11, 2005
  2. Kapil Khosla

    Understanding Typecasting in C++

    Kapil Khosla, Jul 19, 2003, in forum: C++
    Replies:
    3
    Views:
    7,635
    John Harrison
    Jul 20, 2003
  3. Nicolay Korslund
    Replies:
    7
    Views:
    686
    Nicolay Korslund
    Sep 30, 2003
  4. venkatesh
    Replies:
    1
    Views:
    9,186
    lallous
    Dec 6, 2003
  5. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    7,351
    Robert Street
    Feb 21, 2004
  6. cman
    Replies:
    3
    Views:
    1,831
  7. LinuxGuy
    Replies:
    6
    Views:
    1,312
    Mike Wahler
    May 10, 2005
  8. Arun Prasath
    Replies:
    2
    Views:
    484
    Peter Shaggy Haywood
    Nov 26, 2003
Loading...