typecasting in c++

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

  1. Archanasravan

    Archanasravan

    Joined:
    Jan 30, 2013
    Messages:
    1
    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
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:
    505
    Bruce Barker
    Apr 11, 2005
  2. Kapil Khosla

    Understanding Typecasting in C++

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

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,937
    Robert Street
    Feb 21, 2004
Loading...

Share This Page