Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Difference between struct and class

Reply
Thread Tools

Difference between struct and class

 
 
Ook
Guest
Posts: n/a
 
      10-03-2005
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


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-03-2005
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-lit...s.html#faq-7.8

Cheers! --M

 
Reply With Quote
 
 
 
 
Axter
Guest
Posts: n/a
 
      10-03-2005
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.

 
Reply With Quote
 
Ook
Guest
Posts: n/a
 
      10-03-2005

> See this FAQ:
>
> http://www.parashift.com/c++-faq-lit...s.html#faq-7.8
>
> Cheers! --M
>


So there are no difrerences with respect to default constructors, etc.? I
vaguely remember something related to constructors, I'll see if I can find
my lecture notes so I know what question to ask


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-03-2005
Ook wrote:
> > See this FAQ:
> >
> > http://www.parashift.com/c++-faq-lit...s.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

 
Reply With Quote
 
Gina
Guest
Posts: n/a
 
      10-04-2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-04-2005
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-lit...s.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

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-04-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Gina
Guest
Posts: n/a
 
      10-04-2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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
> (E-Mail Removed)



 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-04-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between struct and class and pointer to member data Pep C++ 4 09-10-2009 09:35 AM
any link and binary level difference between c++ struct and class? kjin101@gmail.com C++ 4 10-11-2007 09:48 AM
what is the difference between "struct" and "class"? XiongBin C++ 6 10-26-2005 05:13 PM
Difference between struct and class in c++ and c Robben C++ 3 09-07-2005 01:01 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments