By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,572 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,572 IT Pros & Developers. It's quick & easy.

Declaring structs vs. classes

P: n/a
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.

May 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

Markus Svilans wrote:
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.

May 15 '06 #2

P: n/a
Markus Svilans wrote:
What is the difference between having a struct with constructors and
methods versus a class?

[...]


FAQ 7.8

V
--
Please remove capital As from my address when replying by mail
May 15 '06 #3

P: n/a
Noah Roberts wrote:
Markus Svilans wrote:
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.


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-lit...s.html#faq-7.8

Cheers! --M

May 15 '06 #4

P: n/a
"Markus Svilans" writes:
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.
May 15 '06 #5

P: n/a
mlimber wrote:
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

May 15 '06 #6

P: n/a
osmium wrote:

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.

May 16 '06 #7

P: n/a
Victor, thank you for pointing me at the FAQ. Very, very helpful.

Regards,
Markus.

May 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.