473,795 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes vs Structs?

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 functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ’s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?
Jun 7 '07 #1
19 2559
desktop wrote:
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 functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ’s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?
no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Read
http://www.parashift.com/c++-faq-lit...s.html#faq-7.8

Regards,

Zeppe
Jun 7 '07 #2
On Jun 7, 1:55 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
desktop wrote:
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 functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date 's representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?

no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8

Regards,

Zeppe
There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.

Read this : http://carcino.gen.nz/tech/cpp/struct_vs_class.php

Regards,
ar

"There are things known and things unknown, in between are The Doors."

Jun 7 '07 #3
On 7 Jun, 11:05, Anonymous <ashwani.ra...@ gmail.comwrote:
There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.
What limits do you think struct has? What do you think struct can't
handle that class can?
Read this :http://carcino.gen.nz/tech/cpp/struct_vs_class.php
That link describes the near-equivalence of struct and class (although
where it talks of members it should talk of members and base classes -
see FAQ 7.8) and doesn't mention any "limit" or anything "struct can't
handle". It appears to contradict what you say above.

Gavin Deane

Jun 7 '07 #4
Anonymous wrote:
On Jun 7, 1:55 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
>no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8

Regards,

Zeppe

There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.
I have to admit I've not understood your sentence. If I want to have my
code in c++ (yes, we are in c.l.c++) but I want to use struct that will
depend upon the the limits of struct (well, if I use struct it's because
I need something that depends upon the limits of struct...)... yeah, so
what? And for the last sentence, a struct can handle all the things a
class can, as I said. The only difference is the default visibility of
the members.
Read this : http://carcino.gen.nz/tech/cpp/struct_vs_class.php
That says in a longish way the same that the article that I cited,
agrees with me and contradict what you says. And, on top of that, the OP
didn't have any doubt about the fact that a C++ struct behaves the same
as a c++ class, so I can't really understand the point of the post.

Regards,

Zeppe

Regards,
ar

"There are things known and things unknown, in between are The Doors."
Jun 7 '07 #5
On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:
>no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword.
In what language?

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 7 '07 #6
Anonymous wrote:
On Jun 7, 1:55 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
>desktop wrote:
>>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 functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date 's representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?
no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8

Regards,

Zeppe

There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.

Read this : http://carcino.gen.nz/tech/cpp/struct_vs_class.php

Regards,
ar

"There are things known and things unknown, in between are The Doors."
Ok so it seems that there is no difference between structs and classes
in C++ besides the visibility. But in this article:

http://blog.devstone.com/aaron/archi...06/27/205.aspx

the author mentions that structs are passed as value while classes are
passed as reference. So if I know that my struct/class will contain a
lot of data I would go for a class since all the content will not be
copied each time I pass it to a function.

He also describes that you cannot have a constructor without parameters
in a struct, but that seems to work fine, this compiles:

struct bigblop {
bigblop(){
a = 345;
}
int a;
int b;

int geta() {
return a;
}

void seta(int i) {
a= i;
}

};
Jun 7 '07 #7
Gennaro Prota wrote:
On Thu, 07 Jun 2007 09:55:14 +0100, Zeppe wrote:
>no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword.

In what language?
C++, what else? Just saying that defining classes with the struct
keyword is considered deprecated, and that usually the struct keyword is
used mostly for simple data structures. It's not formally deprecated,
but it's a bad habit, such as using macros and stuff like that.

Regards,

Zeppe

Jun 7 '07 #8
On 7 Juni, 13:17, desktop <f...@sss.comwr ote:
Anonymous wrote:
On Jun 7, 1:55 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
desktop wrote:
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 functions for manipulating a Date. However,it
does not specify that those functions should be the only ones to depend
directly on Date 's representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a classto
make data private?
no, it's not an error. Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword. Readhttp://www.parashift.c om/c++-faq-lite/classes-and-objects.html#fa q-7.8
Regards,
Zeppe
There are certain things you got to decide by yourself while coding.
Now if you want to have you code in C++ but you want to use struct
that will depend upon the limits of struct. If there is something
struct can't handle then you got to use class.
Read this :http://carcino.gen.nz/tech/cpp/struct_vs_class.php
Regards,
ar
"There are things known and things unknown, in between are The Doors."

Ok so it seems that there is no difference between structs and classes
in C++ besides the visibility. But in this article:

http://blog.devstone.com/aaron/archi...06/27/205.aspx

the author mentions that structs are passed as value while classes are
passed as reference. So if I know that my struct/class will contain a
lot of data I would go for a class since all the content will not be
copied each time I pass it to a function.
The author is talking about Managed C++ (a predecessor to C++/CLR), in
which there was some rule like that. In C++/CLR you have to declare
the class *or* struct to be either of value- of ref-type. This however
is totally off-topic in here and (as others have pointed out) the only
difference between struc and class in C++ is the default visibility of
members.

--
Erik Wikström

Jun 7 '07 #9
On Jun 7, 10:55 am, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
desktop wrote:
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 functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ?s representation and the only ones to directly access
objects of class Date . This restriction
can be expressed by using a class instead of a struct"
But I don't see why you need a class for this, on page 234 he even shows
that you can have private and public fields and functions in a struct
also. Is it an error in the book that he writes that you need a class to
make data private?
no, it's not an error.
It depends on how you interpret it. I'm pretty sure that in
this case, Stroustrup is comparing C-style structs to classes;
that by using a class, with everything that C++ supports in
classes, you can express this restriction, which you cannot in a
C style struct. It has nothing to do with the keyword used to
define the type.
Stroustrup doesn't say that you _need_ to create
a class. He says that you can solve that problem creating a class.
Actually, you can solve it also declaring the members private in a
struct. It's deprecated, anyway, such a use of the struct keyword.
Not at all. There's not been the slightest suggestion to
deprecate the keyword struct, at least to my knowledge. The
fact remains that both keywords are legal, and that in C++,
regardless of which keyword you use, you are defining a class.
As a matter of style, only, many people do make a distinction,
but the exact distinction tends to vary from one person to the
next.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #10

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

Similar topics

3
1598
by: Brad Tilley | last post by:
I don't understand classes very well... maybe some day. Is it just me or are they supposed to be difficult to understand? They make my head hurt. Anyway, because I don't understand classes well, I avoid using them. However, many modules in the standard library are implemented as classes: sgmllib, HTMLParser, etc. Is it possible to use these modules without getting into OO programming and inheritance and all the other lofty, theoretical...
14
3803
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
4
4277
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to use structs instead of classes in the array insofar as structs vs. classes appears to be controversial in C# -- with some recommending avoiding structs altogether). It needs to be an array something like this: struct NoteValue {
4
1975
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of their head, do they? Many thanks! -- Best Regards,
2
1367
by: Kiran A K | last post by:
Hi, can anyone give specific examples where structs are preferred to classes?
5
2920
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 that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
7
2255
by: Markus Svilans | last post by:
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()
5
1627
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In C++ i knw there are a few difference between classes and structs but i need to know if there are value or refrence types.
29
2792
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.