size of a class containg both data and function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chinmaya
    New Member
    • Aug 2007
    • 9

    size of a class containg both data and function

    Please help me
    what should be the size of a class,if it contains data member of int and float,
    a function,when we crete an object?
    For example
    [CODE=cpp]class X
    {
    int a ;
    float b;
    public:
    void getdata(int x,float y)
    {
    a = x;
    b = y;
    }

    };
    int main()
    {
    X p;
    sizeof(p);
    }[/CODE]
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by chinmaya
    Please help me
    what should be the size of a class,if it contains data member of int and float,
    a function,when we crete an object?
    For example
    class X
    {
    int a ;
    float b;
    public:
    void getdata(int x,float y)
    {
    a = x;
    b = y;
    }

    };
    int main()
    {
    X p;
    sizeof(p);
    }
    What is the value that you are getting with sizeof() function??

    Comment

    • chinmaya
      New Member
      • Aug 2007
      • 9

      #3
      Originally posted by amitpatel66
      What is the value that you are getting with sizeof() function??
      Actually what should be the size means how many bites the class will take when the object is created.
      By the way i have not executed the code that i have given ...........plz can u help

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by chinmaya
        Actually what should be the size means how many bites the class will take when the object is created.
        By the way i have not executed the code that i have given ...........plz can u help
        amitpatel66 is aware of what the question is and what the size of a variable/type means.

        They were suggesting that you can easily answer your own question by simply running the code.

        Once you have run the code and got the result you could try and explain that result to yourself (or us) and if you are confused at that stage we can help you.

        The actual answer to your question, "what should be the size of that class" is that we can't tell without knowing what platform you are using and what structure packing rules it is using.

        Comment

        Working...