Difference between struct and class

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

Advertisements

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

Advertisements

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.

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 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:

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.

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 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 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.

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.
>

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
>

( 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).

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

"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.)

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
>
>
>
>
>
>

"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++.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!