difference between a structure and a class

Discussion in 'C++' started by Shea Martin, Jun 3, 2005.

  1. Shea Martin

    Shea Martin Guest

    I have been programming in C++ for over 4 years. I *think* I knew that
    a struct could have a constructor but I decided to dig into it a little
    more today, and found that there is very little difference between a
    struct and a class in C++. Both have inheritance, access modifiers,
    etc. The only diff I see is the default access level is public vs.
    private.



    I have always just used structs as a minimal class, as that is the way
    they were used in textbooks.



    Do structs have all these features in pure C? If not then why did c++
    change them, when they were adding the Class type? Are there more
    differences than those I have listed? Performance differences? I have
    always just used structs as a minimal class, as that is the way they
    were used in textbooks.



    Thoughts, Ideas?



    ~S
    Shea Martin, Jun 3, 2005
    #1
    1. Advertising

  2. Shea Martin

    Mike Wahler Guest

    "Shea Martin" <> wrote in message
    news:h1%ne.41394$...
    >I have been programming in C++ for over 4 years. I *think* I knew that
    > a struct could have a constructor but I decided to dig into it a little
    > more today, and found that there is very little difference between a
    > struct and a class in C++. Both have inheritance, access modifiers,
    > etc. The only diff I see is the default access level is public vs.
    > private.


    That is indeed the only difference. Note that also included
    in 'access' is the default inheritance: class has 'private',
    and struct has 'public'. That's why you'll typicall see
    derived classes declared as:

    class B : public A

    > I have always just used structs as a minimal class, as that is the way
    > they were used in textbooks.


    Anything that can be done with a class can also be
    done with a struct.

    >
    >
    >
    > Do structs have all these features in pure C?


    No.


    >If not then why did c++
    > change them,


    Because C++ is not C.

    > when they were adding the Class type?


    'class' is not a type. 'class' (and 'struct') is a keyword
    the programmer can use to create custom (aggregate) types.


    >Are there more
    > differences than those I have listed?


    No.

    >Performance differences?


    That's an implementation detail not addresses by the
    language. See "Quality of Implementation".

    > I have
    > always just used structs as a minimal class, as that is the way they
    > were used in textbooks.


    Objects created from a 'struct' definition are no
    less than those created from a 'class' definition,
    so 'minimal' does not apply.

    > Thoughts, Ideas?


    Since the 'mechanics' are the same, 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 (or often that of your employer/
    client).


    -Mike
    Mike Wahler, Jun 3, 2005
    #2
    1. Advertising

  3. Shea Martin

    John Carson Guest

    "Shea Martin" <> wrote in message
    news:h1%ne.41394$
    > I have been programming in C++ for over 4 years. I *think* I knew
    > that a struct could have a constructor but I decided to dig into it a
    > little more today, and found that there is very little difference
    > between a struct and a class in C++. Both have inheritance, access
    > modifiers, etc. The only diff I see is the default access level is
    > public vs. private.
    >
    > I have always just used structs as a minimal class, as that is the way
    > they were used in textbooks.
    >
    > Do structs have all these features in pure C?


    No. They don't have any of those features. Structs in C are just containers
    for data types.

    > If not then why did c++ change them, when they were adding the Class type?


    I suppose you would have to ask Stroustrup (or read his books), but you can
    use structs exactly as they are used in C or you can use them with the added
    features. Your choice. Isn't that better that only being able to use them as
    in C?

    > Are there more
    > differences than those I have listed? Performance differences? I have
    > always just used structs as a minimal class, as that is the way they
    > were used in textbooks.


    Default inheritance, in addition to access, is public with structs. I think
    that is about all the differences, but maybe there is one more.


    --
    John Carson
    John Carson, Jun 3, 2005
    #3
  4. On Fri, 03 Jun 2005 16:03:44 +0000, Mike Wahler wrote:
    > "Shea Martin" <> wrote in message
    > news:h1%ne.41394$...
    >>I have been programming in C++ for over 4 years. I *think* I knew that
    >> a struct could have a constructor but I decided to dig into it a little
    >> more today, and found that there is very little difference between a
    >> struct and a class in C++. Both have inheritance, access modifiers,
    >> etc. The only diff I see is the default access level is public vs.
    >> private.

    >
    > That is indeed the only difference. Note that also included
    > in 'access' is the default inheritance: class has 'private',
    > and struct has 'public'. That's why you'll typicall see
    > derived classes declared as:
    >
    > class B : public A

    [...]
    > Since the 'mechanics' are the same, I suggest using
    > the different keywords 'class' and 'struct' as
    > indicators of your intentions:

    [...]

    Or better yet, to avoid putting tons of `public' in your code, you can
    just use `struct' instead when you declare classes. Many (most?)
    people seem to put public data members/functions at the top of their
    class (and private at the bottom); the default access specifiers for
    `class' are inconvenient for this style of layout (both for reading and
    for writing).

    class B : public A, public N, public Q<B> {
    public:
    void f();
    };

    vs.

    struct B : A, N, Q<B> {
    void f();
    };

    --
    Jordan DeLong
    Jordan DeLong, Jun 3, 2005
    #4
  5. Shea Martin

    Mike Wahler Guest

    "John Carson" <> wrote in message
    news:d7pv7h$rde$...

    > Default inheritance, in addition to access, is public with structs. I
    > think that is about all the differences, but maybe there is one more.


    Yup -- different spelling. :)

    -Mike
    Mike Wahler, Jun 3, 2005
    #5
  6. Shea Martin

    Peter Julian Guest

    "Shea Martin" <> wrote in message
    news:h1%ne.41394$...
    > I have been programming in C++ for over 4 years. I *think* I knew that
    > a struct could have a constructor but I decided to dig into it a little
    > more today, and found that there is very little difference between a
    > struct and a class in C++. Both have inheritance, access modifiers,
    > etc. The only diff I see is the default access level is public vs.
    > private.
    >


    A struct and a class is exactly the same thing (called an abstract type) so
    long as we are confining the definition to the C++ language. The only
    difference is the default access given to their members, ctors, dtor and
    member functions.

    Your statement about constructors is misleading, a class or struct *must*
    have a constructor and a destructor, there is no choice offered here. The
    question is: will the compiler generate defaults for you or did you choose
    to provide the d~tor and ctors yourself? If you supply a single constructor
    with arguement(s), for example, the compiler ceases to supply any default,
    non-copy constructor. (see code below)

    The compiler provides a default shallow copy-ctor and an assignment operator
    as well. The compiler will always supply these until you overide the
    compiler's responsability to do so. Again: same for struct and class.

    >
    > I have always just used structs as a minimal class, as that is the way
    > they were used in textbooks.
    >
    >
    >
    > Do structs have all these features in pure C? If not then why did c++
    > change them, when they were adding the Class type? Are there more
    > differences than those I have listed? Performance differences? I have
    > always just used structs as a minimal class, as that is the way they
    > were used in textbooks.


    No difference at all between the two. One uses the keyword struct and the
    other class. Even inheritence applies to both with respect for their access
    defaults.

    Note how the following Abstract class is not sufficient to create an array
    of Abstract objects. An array requires a default constructor (thats the
    law). The default constructor can't be supplied here because the compiler
    acknowledges that the creator of the class has taken-over the responsability
    to supply all non-copy constructors.

    The following won't compile unless you uncomment the default ctor (or
    comment-out all ctors for the compiler to generate them for you).

    #include <iostream>

    class Abstract
    {
    int a; // private
    public:
    // ctors
    // Abstract() { std::cout << "default ctor invoked\n"; } // uncomment
    this ctor
    Abstract(int n) : a(n) { std::cout << "ctor invoked\n"; }
    // d~tor
    ~Abstract() { std::cout << "\nd~tor invoked"; }
    // copy-ctor
    Abstract(const Abstract& r_copy)
    {
    std::cout << "copy-ctor invoked\n";
    a = r_copy.getA();
    }
    // member function (accessor)
    int getA() const { return a; }
    };


    int main()
    {
    Abstract array[5]; // an array of 5 default Abstract objects
    Abstract abstract(99); // a single Abstract object with member a = 99
    Abstract copy_of_abstract(abstract); // copy
    array[0] = copy_of_abstract; // assignment

    std::cout << "array[0].getA() = " << array[0].getA();

    return 0;
    }
    Peter Julian, Jun 3, 2005
    #6
  7. Classes and structures also differ in terms of the memory
    layout of member variables. In a simple structure,
    the memory layout is in the order of the fields. In a class
    the memory layout is undefined and compiler dependent.

    --
    EventStudio 2.5 - http://www.EventHelix.com/EventStudio
    Auto Layout and Generate Sequence Diagrams in PDF and MS Word
    EventHelix.com, Jun 4, 2005
    #7
  8. Shea Martin

    John Carson Guest

    "EventHelix.com" <> wrote in message
    news:
    > Classes and structures also differ in terms of the memory
    > layout of member variables. In a simple structure,
    > the memory layout is in the order of the fields. In a class
    > the memory layout is undefined and compiler dependent.


    In circumstances in which a struct is guaranteed to have members laid out in
    the order of declaration, an otherwise identical class will likewise have
    members laid out in the order of declaration provided the class begins with
    the keyword public:, as in

    class X
    {
    public:
    // stuff
    };

    In other words, the only difference is one of default access specifiers.
    Adding public: changes the class's access and makes it equivalent to a
    struct for layout purposes. If a struct has virtual members etc., then its
    member layout is no longer guaranteed, just as with a class that has virtual
    members etc.

    --
    John Carson
    John Carson, Jun 4, 2005
    #8
  9. Shea Martin

    Alan Johnson Guest

    Shea Martin wrote:

    > Thoughts, Ideas?


    Just a bit of trivia: A union can also have member functions (including
    constructors and destructors) in C++. It cannot, however, participate in
    inheritance or have virtual functions.

    -Alan
    Alan Johnson, Jun 4, 2005
    #9
  10. Shea Martin

    John Carson Guest

    "Alan Johnson" <> wrote in message
    news:d7s5r3$1uj$
    > Shea Martin wrote:
    >
    >> Thoughts, Ideas?

    >
    > Just a bit of trivia: A union can also have member functions
    > (including constructors and destructors) in C++.



    True. However:

    Section 9.5/1
    "An object of a class with a non-trivial constructor (12.1), a non-trivial
    copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
    copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
    can an array of such objects."

    --
    John Carson
    John Carson, Jun 4, 2005
    #10
  11. Shea Martin

    Ron Natalie Guest

    EventHelix.com wrote:
    > Classes and structures also differ in terms of the memory
    > layout of member variables. In a simple structure,
    > the memory layout is in the order of the fields. In a class
    > the memory layout is undefined and compiler dependent.
    >

    Not true. The ONLY difference is the default access. The
    memory layout is the same. Between access specifiers, they
    are laid out in increasing memory address (but their may be
    interspersed padding).
    Ron Natalie, Jun 5, 2005
    #11
  12. Shea Martin

    DeltaOne Guest

    Hi ,
    There is only the access qualifiers that differ in both. In C++ default
    is private as Class is designed for data hiding and struct default is
    public... Tehre is no difference. It may happen if u are constructing a
    compiler for C++ that for both delarations u can replace with a single
    name say CLST


    like for
    class example{
    ....
    }

    struct exmple1{
    .....
    }

    both wil be translated to

    CLST example{
    ....
    }
    and
    CLST exmple1{
    ....
    }

    but u need to take care for the default access qualifiers.. Thats all
    struct was insrduced for the only fact that it was at that time people
    or C programmers adore struct more than class.. Introduction of struct
    has more to do with mentality of people rather than technical.

    Bye
    DeltaOne, Jun 5, 2005
    #12
    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,130
  2. Replies:
    9
    Views:
    25,290
    Lal Bahadur Singh
    Nov 11, 2011
  3. Replies:
    4
    Views:
    810
    Rolf Magnus
    May 17, 2006
  4. manish sahu
    Replies:
    5
    Views:
    5,180
    James Kanze
    Feb 14, 2008
  5. suaib

    difference between structure and string?

    suaib, Mar 31, 2010, in forum: C Programming
    Replies:
    4
    Views:
    678
    Nick Keighley
    Apr 6, 2010
Loading...

Share This Page