Difference between struct and class

Discussion in 'C++' started by Ook, Oct 3, 2005.

  1. Ook

    Ook Guest

    We have had a discussion on the differences between a class and a structure,
    and no one is in agreement. As I understand it, a structure defaults to
    public, a class defaults to private. There are issues about constructors
    that I'm not clear on. I do know that I can take a simple project with a
    class that has constuctors, destructors, accessors, and modifiers, change
    the classes to structs, and it compiles and runs fine. Can some kind soul
    outline the differences, or point me to a source that does so? TIA :)
     
    Ook, Oct 3, 2005
    #1
    1. Advertising

  2. Ook

    mlimber Guest

    Ook wrote:
    > We have had a discussion on the differences between a class and a structure,
    > and no one is in agreement. As I understand it, a structure defaults to
    > public, a class defaults to private. There are issues about constructors
    > that I'm not clear on. I do know that I can take a simple project with a
    > class that has constuctors, destructors, accessors, and modifiers, change
    > the classes to structs, and it compiles and runs fine. Can some kind soul
    > outline the differences, or point me to a source that does so? TIA :)


    See this FAQ:

    http://www.parashift.com/c -faq-lite/classes-and-objects.html#faq-7.8

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #2
    1. Advertising

  3. Ook

    Axter Guest

    Ook wrote:
    > We have had a discussion on the differences between a class and a structure,
    > and no one is in agreement. As I understand it, a structure defaults to
    > public, a class defaults to private. There are issues about constructors
    > that I'm not clear on. I do know that I can take a simple project with a
    > class that has constuctors, destructors, accessors, and modifiers, change
    > the classes to structs, and it compiles and runs fine. Can some kind soul
    > outline the differences, or point me to a source that does so? TIA :)


    IAW C++ standard the only difference is the default public VS default
    private.
    That is it. Nothing else.

    However, some programmers user a general rule in which they use struct
    for POD types and class for complex types.

    But that has nothing to do with the official C++ standard.
     
    Axter, Oct 3, 2005
    #3
  4. Ook

    Ook Guest

    Ook, Oct 3, 2005
    #4
  5. Ook

    mlimber Guest

    Ook wrote:
    > > See this FAQ:
    > >
    > > http://www.parashift.com/c -faq-lite/classes-and-objects.html#faq-7.8
    > >
    > > Cheers! --M
    > >

    >
    > So there are no difrerences with respect to default constructors, etc.?


    Correct. Even initialization lists can work with classes, though
    constructors are generally preferred:

    class A { public: int i, j; };
    A a1 = { 1, 42 }; // Ok
    A a2; // Ok

    struct B { int i, j; };
    B b2 = { 1, 42 }; // Ok
    B b2; // Ok

    Cheers! --M
     
    mlimber, Oct 3, 2005
    #5
  6. Ook

    Gina Guest

    I am new to c++ as well
    .... I do not know whether I got that right at all
    but as far as I understand it is the difference between both:

    1. a struct does not define behaviour ... it only defines the type
    2. a class can have functions and assigns particular behaviour for an
    object)

    a class A has a function A::dosomething
    so we can do
    A.dosomething();

    could that be what you were looking for ?

    cheers,
    Gina

    "Axter" <> schrieb im Newsbeitrag
    news:...
    > Ook wrote:
    > > We have had a discussion on the differences between a class and a

    structure,
    > > and no one is in agreement. As I understand it, a structure defaults to
    > > public, a class defaults to private. There are issues about constructors
    > > that I'm not clear on. I do know that I can take a simple project with a
    > > class that has constuctors, destructors, accessors, and modifiers,

    change
    > > the classes to structs, and it compiles and runs fine. Can some kind

    soul
    > > outline the differences, or point me to a source that does so? TIA :)

    >
    > IAW C++ standard the only difference is the default public VS default
    > private.
    > That is it. Nothing else.
    >
    > However, some programmers user a general rule in which they use struct
    > for POD types and class for complex types.
    >
    > But that has nothing to do with the official C++ standard.
    >
     
    Gina, Oct 4, 2005
    #6
  7. Ook

    mlimber Guest

    Gina wrote:
    > I am new to c++ as well
    > ... I do not know whether I got that right at all
    > but as far as I understand it is the difference between both:
    >
    > 1. a struct does not define behaviour ... it only defines the type
    > 2. a class can have functions and assigns particular behaviour for an
    > object)
    >
    > a class A has a function A::dosomething
    > so we can do
    > A.dosomething();
    >
    > could that be what you were looking for ?
    >
    > cheers,
    > Gina


    Welcome, Gina. Your statement on the topic is incorrect. Check out the
    FAQ:

    http://www.parashift.com/c -faq-lite/classes-and-objects.html#faq-7.8

    This code will work fine:

    #include <iostream>
    using namespace std;

    struct S
    {
    public:
    void Foo()
    {
    i_ = 0xc0ffee;
    cout << "S::Foo()" << endl;
    }
    private:
    int i_;
    };

    class C
    {
    public:
    Bar()
    {
    i_ = 42;
    cout << "C::Bar()" << endl;
    }
    private:
    int i_;
    };

    Cheers! --M
     
    mlimber, Oct 4, 2005
    #7
  8. Gina wrote:
    >
    > I am new to c++ as well
    > ... I do not know whether I got that right at all
    > but as far as I understand it is the difference between both:
    >
    > 1. a struct does not define behaviour ... it only defines the type
    > 2. a class can have functions and assigns particular behaviour for an
    > object)


    Maybe conceptually you are right and indeed this is how most programmers
    treat that topic in practice.
    But as C++ - the language - goes, it is incorrect. A 'struct' and a 'class'
    differ only in 2 topics:
    * members are per default public in a struct and private in a class
    * the inheritance is per default public in a struct and private in a class

    Other then that, there is no difference. At least none enforced by C++ - the language.
    Any other differences in usage, as described by you above, may or may not be enforced
    by your local design rules.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 4, 2005
    #8
  9. Ook

    Gina Guest

    Hi & Thanks ....
    for your answers .. and for the welcome ;-)

    where I got what I stated here is from 'Visual C++ in 21 days' .pdf

    herein is says:
    .... public ... private etc .... all exactly what has been said
    and then ( free translation) :

    '... difference between classes and structures is that structures don't
    contain functionality they only contain data members
    ....
    the main difference shows up in using them....structures are only a
    container for data elements...'
    .... bit confusing ...
    I have not read the link provided in the above post re topic.... which I
    will !!
    but
    is it wrong then ... what's written in that book ??

    Gina ?:-|




    "Karl Heinz Buchegger" <> schrieb im Newsbeitrag
    news:...
    > Gina wrote:
    > >
    > > I am new to c++ as well
    > > ... I do not know whether I got that right at all
    > > but as far as I understand it is the difference between both:
    > >
    > > 1. a struct does not define behaviour ... it only defines the type
    > > 2. a class can have functions and assigns particular behaviour for an
    > > object)

    >
    > Maybe conceptually you are right and indeed this is how most programmers
    > treat that topic in practice.
    > But as C++ - the language - goes, it is incorrect. A 'struct' and a

    'class'
    > differ only in 2 topics:
    > * members are per default public in a struct and private in a class
    > * the inheritance is per default public in a struct and private in a class
    >
    > Other then that, there is no difference. At least none enforced by C++ -

    the language.
    > Any other differences in usage, as described by you above, may or may not

    be enforced
    > by your local design rules.
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Gina, Oct 4, 2005
    #9
  10. Ook

    mlimber Guest

    Gina wrote:
    > Hi & Thanks ....
    > for your answers .. and for the welcome ;-)
    >
    > where I got what I stated here is from 'Visual C++ in 21 days' .pdf
    >
    > herein is says:
    > ... public ... private etc .... all exactly what has been said
    > and then ( free translation) :
    >
    > '... difference between classes and structures is that structures don't
    > contain functionality they only contain data members
    > ...
    > the main difference shows up in using them....structures are only a
    > container for data elements...'
    > ... bit confusing ...
    > I have not read the link provided in the above post re topic.... which I
    > will !!
    > but
    > is it wrong then ... what's written in that book ??
    >
    > Gina ?:-|


    The book is wrong in the sense that Karl detailed: the book is likely
    describing common practice rather than what the language allows.

    Cheers! --M

    PS, It's considered bad manners to top-post in newsgroups. Put your
    replies inline or at the bottom.
     
    mlimber, Oct 4, 2005
    #10
  11. Gina wrote:
    >
    > Hi & Thanks ....
    > for your answers .. and for the welcome ;-)
    >
    > where I got what I stated here is from 'Visual C++ in 21 days' .pdf
    >
    > herein is says:
    > ... public ... private etc .... all exactly what has been said
    > and then ( free translation) :
    >
    > '... difference between classes and structures is that structures don't
    > contain functionality they only contain data members
    > ...
    > the main difference shows up in using them....structures are only a
    > container for data elements...'
    > ... bit confusing ...
    > I have not read the link provided in the above post re topic.... which I
    > will !!
    > but
    > is it wrong then ... what's written in that book ??
    >
    > Gina ?:-|


    All books with something like ' ... in x days' need to take some shortcuts
    and don't tell you everything. Well, in fact usually they tell you very little.
    It is nearly impossible for a C++ book to cover every aspect of C++. Thus those
    books are often incorrect (especially with the details) in wide areas.
    Even if I am generous and interpret '21 days' as '21 lessons', this is still nowhere
    near the truth. One cannot learn C++ in 21 lessons. It takes months to have some solid
    understanding of the basics. Mastering C++ with the finer details takes years. So better
    make a big bow around all books stating they can teach you x in y days.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 4, 2005
    #11
  12. Ook

    Gina Guest

    M.,
    in the meantime I have read the article provided by your link ....

    .... and just wonder, how would a structure definition 'look' .... when
    containing functionality ..... ???

    books with incorrect statements !!! <deary me>
    Gina

    "mlimber" <> schrieb im Newsbeitrag
    news:...
    > Gina wrote:
    > > Hi & Thanks ....
    > > for your answers .. and for the welcome ;-)
    > >
    > > where I got what I stated here is from 'Visual C++ in 21 days' .pdf
    > >
    > > herein is says:
    > > ... public ... private etc .... all exactly what has been said
    > > and then ( free translation) :
    > >
    > > '... difference between classes and structures is that structures don't
    > > contain functionality they only contain data members
    > > ...
    > > the main difference shows up in using them....structures are only a
    > > container for data elements...'
    > > ... bit confusing ...
    > > I have not read the link provided in the above post re topic.... which I
    > > will !!
    > > but
    > > is it wrong then ... what's written in that book ??
    > >
    > > Gina ?:-|

    >
    > The book is wrong in the sense that Karl detailed: the book is likely
    > describing common practice rather than what the language allows.
    >
    > Cheers! --M
    >
    > PS, It's considered bad manners to top-post in newsgroups. Put your
    > replies inline or at the bottom.
    >
     
    Gina, Oct 4, 2005
    #12
  13. Ook

    Gina Guest

    Hallo Karl-Heinz.

    well .... I have to start somehwere .... and with some practical stuff ....
    the book was for free ....

    > All books with something like ' ... in x days' need to take some

    shortcuts
    > and don't tell you everything. Well, in fact usually they tell you very

    little.
    > It is nearly impossible for a C++ book to cover every aspect of C++. Thus

    those
    > books are often incorrect (especially with the details) in wide areas.


    > Even if I am generous and interpret '21 days' as '21 lessons', this is

    still nowhere
    > near the truth. One cannot learn C++ in 21 lessons. It takes months to

    have some solid
    > understanding of the basics.


    especially with c++ !!! your are totally right!!

    Cheers,
    Gina

    Mastering C++ with the finer details takes years. So better
    > make a big bow around all books stating they can teach you x in y days.
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Gina, Oct 4, 2005
    #13
  14. Ook

    mlimber Guest

    Gina wrote:
    > M.,
    > in the meantime I have read the article provided by your link ....
    >
    > ... and just wonder, how would a structure definition 'look' .... when
    > containing functionality ..... ???
    >
    > books with incorrect statements !!! <deary me>
    > Gina


    First, I will reiterate: don't top-post. Put your replies *BELOW* (or
    inline with) the one you are quoting.

    To answer your question, I'll refer you to my previous post in this
    thread where I gave an example:

    http://groups.google.com/group/comp...25ed13dd021/3c64a9e4ffdabe3a#3c64a9e4ffdabe3a

    Cheers! --M
     
    mlimber, Oct 4, 2005
    #14
  15. Gina wrote:
    >
    > M.,
    > in the meantime I have read the article provided by your link ....
    >
    > ... and just wonder, how would a structure definition 'look' .... when
    > containing functionality ..... ???
    >


    just like a class.
    A few seconds ago I wrote in one of my projects:

    struct CStationDesc
    {
    CStationDesc( const CString& Key = "", short Id = 0, long Limit = 0, long Allocation = 0 )
    : m_Key( Key ),
    m_StationId( Id ),
    m_Limit( Limit ),
    m_Allocation( Allocation )
    {}

    CString m_Key;
    short m_StationId;
    long m_Limit;
    long m_Allocation;
    };

    ( Don't worry about CString. It is just another data type).
    It is a structure I use for collecting data during a lengthy calculation process, storing
    multiple such data sets in a vector, sorting them and presenting the results to the user.
    I introduced that structure only to have a place where I can organize things. I could have
    made it a class as well, with all members public or a class with all members private and
    setter and getter functions. But I wanted to emphasize that this 'collection' is only needed
    to have a nice place for data organization and no real intelligence is needed for them, that
    is why I used a struct.
    But in order to simplify life, I have given it a constructor (you can add any other
    member function as well).

    The constructor allows me to write

    std::vector< CStationDesc > Results;

    ....

    Results.push_back( CStationDesc( "Kalle", 123, Limit, Allocation ) );

    instead of

    CStationDesc Desc;

    Desc.m_Key = "Kalle";
    Desc.m_StationId = 123;
    Desc.m_Limit = Limit;
    Desc.m_Allocation = Allocation;

    Results.push_back( Desc );

    See. Much shorter.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 4, 2005
    #15
  16. Ook

    mlimber Guest

  17. Ook

    Ook Guest

    "Gina" <> wrote in message
    news:dhu3b6$2qu$03$-online.com...
    > M.,
    > in the meantime I have read the article provided by your link ....
    >
    > ... and just wonder, how would a structure definition 'look' .... when
    > containing functionality ..... ???
    >
    > books with incorrect statements !!! <deary me>
    > Gina
    >


    I was taught in a c++ class at the local community college, and I have read
    in articles posted here and there on the Internet, that a struct can't have
    a parameterless constructor. However, the following code compiles and runs
    just fine. If I understand what I'm doing, then I've been taught wrong :(

    struct Zoot
    {
    public:
    Zoot();
    int size();
    private:
    int _size;
    int* _data;
    };

    int Zoot::size(){return _size;}

    // This should be my parameterless constructor
    Zoot::Zoot()
    {
    _size = 5;
    _data = new int[ _size ];
    _data[0] = 123;
    }

    int main()
    {
    Zoot z;
    cout << z.size() << endl;
    return 0;
    }
     
    Ook, Oct 4, 2005
    #17
  18. Ook

    Howard Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > Gina wrote:
    >>
    >> I am new to c++ as well
    >> ... I do not know whether I got that right at all
    >> but as far as I understand it is the difference between both:
    >>
    >> 1. a struct does not define behaviour ... it only defines the type
    >> 2. a class can have functions and assigns particular behaviour for an
    >> object)

    >
    > Maybe conceptually you are right and indeed this is how most programmers
    > treat that topic in practice.
    > But as C++ - the language - goes, it is incorrect. A 'struct' and a
    > 'class'
    > differ only in 2 topics:
    > * members are per default public in a struct and private in a class
    > * the inheritance is per default public in a struct and private in a class
    >
    > Other then that, there is no difference. At least none enforced by C++ -
    > the language.
    > Any other differences in usage, as described by you above, may or may not
    > be enforced
    > by your local design rules.
    >


    Perhaps that bok is just old?

    When I first started using C++ (about 1992, I think), the [Borland] compiler
    I was using did indeed have those differences between class and struct which
    Gina quotes from that book. A struct was strictly a C struct (POD), while a
    class was a whole new entity, with member functions. The fact that some
    early C++ compilers were written that way led some authors to present those
    as specifications of the C++ language (as opposed to simply descriptions of
    how some vendors implemented it). And since then, the common practice of
    using struct for POD objects and class for more complex entities has helped
    perpetuate what is now really a misconception about the language
    specifications.

    (I wouldn't be surprised if some older professors still teach that.
    Certainly, I've seen that misconception here more than once.)

    -Howard
     
    Howard, Oct 4, 2005
    #18
  19. Ook

    Gina Guest

    Howard,

    maybe ... it is old .... I was scrolling through it but couldn't find any
    date

    it is about vc++ and it is an online book *.pdf....
    it states that the struct comes from old c ...

    but thanks to everybody for help and clarification !!

    Gina

    "Howard" <> schrieb im Newsbeitrag
    news:fDx0f.369029$...
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > > Gina wrote:
    > >>
    > >> I am new to c++ as well
    > >> ... I do not know whether I got that right at all
    > >> but as far as I understand it is the difference between both:
    > >>
    > >> 1. a struct does not define behaviour ... it only defines the type
    > >> 2. a class can have functions and assigns particular behaviour for

    an
    > >> object)

    > >
    > > Maybe conceptually you are right and indeed this is how most programmers
    > > treat that topic in practice.
    > > But as C++ - the language - goes, it is incorrect. A 'struct' and a
    > > 'class'
    > > differ only in 2 topics:
    > > * members are per default public in a struct and private in a class
    > > * the inheritance is per default public in a struct and private in a

    class
    > >
    > > Other then that, there is no difference. At least none enforced by C++ -
    > > the language.
    > > Any other differences in usage, as described by you above, may or may

    not
    > > be enforced
    > > by your local design rules.
    > >

    >
    > Perhaps that bok is just old?
    >
    > When I first started using C++ (about 1992, I think), the [Borland]

    compiler
    > I was using did indeed have those differences between class and struct

    which
    > Gina quotes from that book. A struct was strictly a C struct (POD), while

    a
    > class was a whole new entity, with member functions. The fact that some
    > early C++ compilers were written that way led some authors to present

    those
    > as specifications of the C++ language (as opposed to simply descriptions

    of
    > how some vendors implemented it). And since then, the common practice of
    > using struct for POD objects and class for more complex entities has

    helped
    > perpetuate what is now really a misconception about the language
    > specifications.
    >
    > (I wouldn't be surprised if some older professors still teach that.
    > Certainly, I've seen that misconception here more than once.)
    >
    > -Howard
    >
    >
    >
    >
    >
    >
     
    Gina, Oct 4, 2005
    #19
  20. Ook

    Howard Guest

    "Ook" <nousenetspam at dead ice dot us> wrote in message
    news:...
    >


    > I was taught in a c++ class at the local community college, and I have
    > read in articles posted here and there on the Internet, that a struct
    > can't have a parameterless constructor. However, the following code
    > compiles and runs just fine. If I understand what I'm doing, then I've
    > been taught wrong :(
    >


    That's a C# specification (in .NET), if I recall correctly. It most
    certainly is wrong in regards to standard C++.

    -Howard
     
    Howard, Oct 4, 2005
    #20
    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,621
  2. Robben
    Replies:
    3
    Views:
    549
    tmartsum
    Sep 7, 2005
  3. XiongBin
    Replies:
    6
    Views:
    343
    Default User
    Oct 26, 2005
  4. Replies:
    4
    Views:
    401
    James Kanze
    Oct 11, 2007
  5. Pep
    Replies:
    4
    Views:
    870
    James Kanze
    Sep 10, 2009
Loading...

Share This Page