Declaring structs vs. classes

M

Markus Svilans

Hi,

What is the difference between having a struct with constructors and
methods versus a class?

In many C++ examples online I have seen code similar to this:

struct Animal
{
Animal()
{
id = 0;
name[0] = 0;
}
int id;
char name[30];
};

Intuitively, I would understand structs with methods to be more data
oriented (i.e. no virtual methods), where the methods provide
initialization and maybe get/set methods. Classes would be more object
oriented, with more elaborate code, besides simple initialization and
get/set methods.

Can any one elaborate more on this? I'd like to understand better when
it's more correct to use a struct with methods, and when it's more
correct to use classes. Until now I have been using classes for any
kind of data structure that requires methods and automatic
initialization.

Thanks,
Markus.
 
N

Noah Roberts

Markus said:
Hi,

What is the difference between having a struct with constructors and
methods versus a class?

Default access permissions. Structs are public, classes are private.

struct X { int x; };
class Y { int y; };


X x;
Y y;

x.x = 5; // legal
y.y = 5; // illegal - no access to private member..

That is it...there is no other difference.
 
M

mlimber

Noah said:
Default access permissions. Structs are public, classes are private.

struct X { int x; };
class Y { int y; };


X x;
Y y;

x.x = 5; // legal
y.y = 5; // illegal - no access to private member..

That is it...there is no other difference.

Incorrect. Classes also default to private inheritance, while structs
default to public. The FAQ omits this part also, but it includes an
extra bit about idiomatic usage:

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

Cheers! --M
 
O

osmium

Markus Svilans said:
What is the difference between having a struct with constructors and
methods versus a class?

In many C++ examples online I have seen code similar to this:

struct Animal
{
Animal()
{
id = 0;
name[0] = 0;
}
int id;
char name[30];
};

Intuitively, I would understand structs with methods to be more data
oriented (i.e. no virtual methods), where the methods provide
initialization and maybe get/set methods. Classes would be more object
oriented, with more elaborate code, besides simple initialization and
get/set methods.

Can any one elaborate more on this? I'd like to understand better when
it's more correct to use a struct with methods, and when it's more
correct to use classes. Until now I have been using classes for any
kind of data structure that requires methods and automatic
initialization.

I see no point to the sample you posted. The fact that he uses a C style
string makes me wonder about how serious the programmer was.

From what you say, I think your understanding of the situation is pretty
much correct.
 
M

mlimber

mlimber said:
Classes also default to private inheritance, while structs
default to public. The FAQ omits this part also,

.... uh, nevermind about that omission. It's there, I just missed it. --M
 
M

Markus Svilans

osmium said:
I see no point to the sample you posted. The fact that he uses a C style
string makes me wonder about how serious the programmer was.

I apologize for appearing non-serious through my use of inappropriate
example code.

Regards,
Markus.
 
M

Markus Svilans

Victor, thank you for pointing me at the FAQ. Very, very helpful.

Regards,
Markus.
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top