Difference between struct and class

O

Ook

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

mlimber

Ook said:
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
 
A

Axter

Ook said:
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.
 
M

mlimber

Ook said:
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
 
G

Gina

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
 
M

mlimber

Gina said:
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
 
K

Karl Heinz Buchegger

Gina said:
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.
 
G

Gina

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

mlimber

Gina said:
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.
 
K

Karl Heinz Buchegger

Gina said:
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.
 
G

Gina

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
 
G

Gina

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
 
M

mlimber

Gina said:
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
 
K

Karl Heinz Buchegger

Gina said:
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.
 
O

Ook

Gina said:
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
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;
}
 
H

Howard

Karl Heinz Buchegger said:
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
 
G

Gina

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 said:
Karl Heinz Buchegger said:
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
 
H

Howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top