473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Declaring structs vs. classes

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

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
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
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
"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
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
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
Victor, thank you for pointing me at the FAQ. Very, very helpful.

Regards,
Markus.

May 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Der Andere | last post by:
Two short questions: 1. Is there any implementation of sets in PHP4? 2. Can I define structs (e.g., as in C++) or do I have to use classes instead?? Cheers, Matthias --
18
by: Rick | last post by:
Hi, We all know that Java has classes but how about basic storage objects like structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types etc. How about Java? Greetings, Rick
16
by: Michi Henning | last post by:
Below is a bit of code that creates a delegate. The delegate invokes a member function of a struct. The code compiles, but has surprising behavior: using System; namespace ConsoleApplication1...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
19
by: desktop | last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup 's book page 225 he writes: "The declaration of Date in the previous subsection (declared as a struct) provides a set of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.