Exforsys
+ Reply to Thread
Results 1 to 4 of 4

Help needed in some basic C++ questions

This is a discussion on Help needed in some basic C++ questions within the C and C++ forums, part of the Programming Talk category; Hi, can anyone tell me the answers for the following: 1. Diff. between Class and structures w.r.t memory I know ...

  1. #1
    NareshShroff is offline Junior Member Array
    Join Date
    Apr 2004
    Answers
    18

    Help needed in some basic C++ questions

    Hi,
    can anyone tell me the answers for the following:

    1. Diff. between Class and structures w.r.t memory
    I know there is one diff. with access specifiers. But memory wise wat is the difference?
    I thought all data members will be stored adjacently in memory in both classes and structs. I know there is diff. b/n union and struct in memory wise.

    2. Can u call the following function declaration as overloaded
    int fun(char, char) , void fun(int, char)

    does it matter with the return types?

    Thank You,
    NareshShroff


  2. #2
    akpraveen Guest

    Re:Help needed in some basic C++ questions

    2. Can u call the following function declaration as overloaded
    int fun(char, char) , void fun(int, char)
    I am not sure about the first question, but the 2nd question\'s answer is simple!

    The function declaration is different (In Java, you would say that the method signature is different)..

    An overload is pretty much using the same function declaration but with different function definition ( meaning, logic is different!)... I am rusty on C++ but with respect to Object Oriented Programming, this is a perfect example of polymorphism (meaning multiple forms/ways to do a thing)! Your return types do not matter when you are doing overloading!

    The first question kindles me a little. The difference in memory usage of Classes and Structures is that structures are less memory intensive and heap is allocated during runtime. With classes, memory is allocated during compile time itself.

    Good luck!


  3. #3
    priyaraji is offline Member Array
    Join Date
    Apr 2006
    Answers
    52
    .You can use structure the same way as you use class except for difference with access specifiers.But mostly structures are used to group data and classes are used mostly to group data and functions.
    2.Yes the functions given
    int fun(char, char) , void fun(int, char) are overloaded.
    For instance you may have:

    Example of Function Overloading

    int area(int s) // For Square
    {
    return(s*s);
    }
    float area(float x,float y) // For rectangle
    {
    return(x*y);
    }

    double area(double a,double b) // For Triangle
    {
    return(0.5*a*b);
    }


    This will have declaration as
    float area(float,float);
    doble area(double,int);

    So as per the above example I have given,your example of function declaration is also a function overloaded

    Sripriya


  4. #4
    svs_aditya's Avatar
    svs_aditya is offline Aditya Array
    Join Date
    Jul 2006
    Location
    India
    Answers
    10
    Hi NareshShroff,
    Good question..
    yes,That is indeed the only difference.
    class has 'private',
    and struct has 'public'. That's why you'll typicalyl see
    derived classes declared as:

    class B : public A

    So, I suggest using
    the different keywords 'class' and 'struct' as
    indicators of your intentions: I.e. use 'class'
    when you're creating abstract types with special
    functionality (e.g. polymorphism), use 'struct'
    if simply grouping related simple data items (e.g.
    a pair of integer objects). But ultimately, the
    choice is yours .
    Objects created from a 'struct' definition are no
    less than those created from a 'class' definition.

    regards,
    Aditya


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...