what's the difference between type struct and type class exactly?

Discussion in 'C++' started by =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 22, 2006.

  1. Hi,

    Consider this code:

    --- beginning of code ---
    #include <iostream>
    using namespace std;

    class Child{
    public:
    Child(const int& n); // constructor
    void Show();
    private:
    int ChildNmb; // the childs number in the sequence of siblings
    const int& NmbOfSibls; // the number of siblings (including itself)
    };

    // constructor:
    Child::Child(const int& n)
    :ChildNmb(n), NmbOfSibls(n){ } // initialisation - const int& NmbOfSibls
    = n,
    // NmbOfSibls becomes an alias for the actual argument variable.
    void Child::Show() {
    cout << "I am child number: " << ChildNmb << ". I have " <<
    NmbOfSibls-1 << " brothers and sisters." << endl;
    }

    //************************************

    int main(){
    int n=0; // Number of children
    ++n; Child c1(n);
    ++n; Child c2(n);
    ++n; Child c3(n);
    c1.Show(); c2.Show(); c3.Show();

    ++n; Child c4(n);
    c1.Show(); c2.Show(); c3.Show(); c4.Show();
    }

    --- end of code ---

    I'm a bit confused about this code... For instance:

    Using "class Child{" gives *EXACTLY* the same result as using "struct
    Child{" - both compiles and runs as well... Why? And why is there both
    type struct and type class, when it seems like there's no difference?

    Another thing is that I'm a bit confused about the constructor:
    "Child::Child(const int& n)" - because I can easily understand if n in
    this case becomes an alias for another variable. But if one pass a
    number such as Child c5(5); to the program, then 5 is not a variable...
    So 5 is stored in memory somewhere and therefore it doesn't need an
    alias... Or am I missing something?


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 22, 2006
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Jordan Guest

    Martin,

    1. Structs and Classes - Structs and classes appear to be the same, but
    there are a few distinctions. First, all members of a struct are
    public by default. Stucts are commonly used to represent records which
    could be composed of several data types internally. Structs support
    member functions but not much else beyond that.

    Classes, however, support a wider range of functionality. By default,
    all members are private in a class. Classes also support inheritance
    and polymorphism where this is not supported by structs.

    2. Variables - Even though '5' is technically a constant, its value
    will be stored in a temporary memory location during the Child's
    constructor call (and thus behave like a variable). All constants that
    participate in operations will be stored in variable memory space. For
    example, when program execution reaches a method call, several things
    are placed on top of a "stack frame" in memory. The stack frame
    contains data needed by the method. There will be a segment for both
    parameters and local variables. That means that all values passed into
    the method call will be placed in a memory location in the stack frame
    under the parameters segment. This would be true if you passed in a
    variable directly to the method because C++ passes by value.
    Jordan, Feb 22, 2006
    #2
    1. Advertising

  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    John Carson Guest

    "Jordan" <> wrote in message
    news:
    > Martin,
    >
    > 1. Structs and Classes - Structs and classes appear to be the same,
    > but there are a few distinctions. First, all members of a struct are
    > public by default. Stucts are commonly used to represent records
    > which could be composed of several data types internally. Structs
    > support member functions but not much else beyond that.
    >
    > Classes, however, support a wider range of functionality. By default,
    > all members are private in a class. Classes also support inheritance
    > and polymorphism where this is not supported by structs.


    This is wrong. Inheritance and polymorphism work with structs.

    The only differences between a struct and a class are that

    1. a struct's members are public by default and a class's members are
    private by default.

    2. inheritance is public by default for a struct and private by default for
    a class. More precisely, given,

    struct Derived : Base
    {
    // stuff
    };

    the inheritance type is public (regardless of whether Base is a class or
    struct). And given:

    class Derived : Base
    {
    // stuff
    };

    the inheritance type is private (regardless of whether Base is a class or
    struct).

    To remember this easily, just think of the base class as a special type of
    member.

    Another way of looking at it is the following. Given

    struct Derived : Base
    {
    // stuff
    };

    the following is exactly equivalent:

    class Derived : public Base
    {
    public:
    // stuff
    };

    Likewise, given

    class Derived : Base
    {
    // stuff
    };

    the following is exactly equivalent

    struct Derived : private Base
    {
    private:
    // stuff
    };

    --
    John Carson
    John Carson, Feb 22, 2006
    #3
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Peter_Julian Guest

    "Jordan" <> wrote in message
    news:...
    | Martin,
    |
    | 1. Structs and Classes - Structs and classes appear to be the same,
    but
    | there are a few distinctions. First, all members of a struct are
    | public by default. Stucts are commonly used to represent records
    which
    | could be composed of several data types internally. Structs support
    | member functions but not much else beyond that.
    |
    | Classes, however, support a wider range of functionality. By default,
    | all members are private in a class. Classes also support inheritance
    | and polymorphism where this is not supported by structs.

    Except for the default access mode, there is absolutely no difference
    between a struct and a class. While styles usually prefer keyword struct
    when dealing with a POD-type, a struct supports inheritence, templates,
    init lists, etc...

    |
    | 2. Variables - Even though '5' is technically a constant, its value
    | will be stored in a temporary memory location during the Child's
    | constructor call (and thus behave like a variable). All constants
    that
    | participate in operations will be stored in variable memory space.
    For
    | example, when program execution reaches a method call, several things
    | are placed on top of a "stack frame" in memory. The stack frame
    | contains data needed by the method. There will be a segment for both
    | parameters and local variables. That means that all values passed
    into
    | the method call will be placed in a memory location in the stack frame
    | under the parameters segment. This would be true if you passed in a
    | variable directly to the method because C++ passes by value.
    |
    Peter_Julian, Feb 22, 2006
    #4
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    Jordan Guest

    I stand corrected. Thank you for correcting me.
    Jordan, Feb 22, 2006
    #5
  6. John Carson wrote:
    > "Jordan" <> wrote in message
    > news:

    -snip-

    > the following is exactly equivalent
    >
    > struct Derived : private Base
    > {
    > private:
    > // stuff
    > };


    Etc... Okay, I learned that a struct's members are public by default and
    a class's members are private by default. The other things you talked
    about such as inheritance and polymorphism I didn't read about yet, but
    I'm almost there in my book and then I'll hopefully understand it better
    when I read that part :)

    Thanks for the answers. I think I'll just wait a bit and read more in my
    book and then read this thread again and ask questions at that time, if
    there is something I don't understand at that moment...


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 22, 2006
    #6
    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. jakk
    Replies:
    4
    Views:
    12,018
  2. Replies:
    5
    Views:
    485
    Old Wolf
    Dec 30, 2004
  3. Robben
    Replies:
    3
    Views:
    521
    tmartsum
    Sep 7, 2005
  4. Replies:
    4
    Views:
    373
    James Kanze
    Oct 11, 2007
  5. Pep
    Replies:
    4
    Views:
    809
    James Kanze
    Sep 10, 2009
Loading...

Share This Page