473,503 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why did c++ add the class keyword?

The class keyword created a lot of confusion that resulted in a popular
style that says struct is appropriate for POD (plain old data) and
class is appropriate for fancier things (member functions, data hiding,
inheritance). And indeed the FAQ even advocates this usage model. I
myself have broken from this tradition because I find the public
defaulting of struct more appropriate than class and thus prefer it
100% of the time. The benefit is I no longer need to unhide the
constructor and I don't lose any explicit control over privacy. So what
was the reason for introducing the class keyword?

Sep 24 '06 #1
9 3766
<44****@email.comwrote:
>The class keyword created a lot of confusion that resulted in a popular
style that says struct is appropriate for POD (plain old data) and
class is appropriate for fancier things (member functions, data hiding,
inheritance). And indeed the FAQ even advocates this usage model. I
myself have broken from this tradition because I find the public
defaulting of struct more appropriate than class and thus prefer it
100% of the time.
(OT)

I also always use struct, since normally the first element of
a struct or class is public, and therefore it saves a keyword.
I find the theory that if there's so much as a member function it
should be a class unconvincing. Everyone knows a struct
and a class are the same thing.

But please note I am not a software engineer in the first place. :-)

Steve
Sep 24 '06 #2
44****@email.com wrote:
The class keyword created a lot of confusion that resulted in a popular
style that says struct is appropriate for POD (plain old data) and
class is appropriate for fancier things (member functions, data hiding,
inheritance). And indeed the FAQ even advocates this usage model. I
myself have broken from this tradition because I find the public
defaulting of struct more appropriate than class and thus prefer it
100% of the time. The benefit is I no longer need to unhide the
constructor and I don't lose any explicit control over privacy. So what
was the reason for introducing the class keyword?
Most of the time, I use the class keyword and
annotate the sections. I have sections for public,
protected and private in my stencil. The class
keyword helps me to distinguish object-oriented
projects from ad-hoc or C style projects.

My philosophy is that everything should be private
unless proven otherwise. Information is distributed
on a need to know basis (high encapsulation). This
really helps in debugging, especially on large
projects.

For demos and experiments, I use the struct.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Sep 24 '06 #3
44****@email.com wrote:
The class keyword created a lot of confusion that resulted in a
popular style that says struct is appropriate for POD (plain old
data) and class is appropriate for fancier things (member functions,
data hiding, inheritance). And indeed the FAQ even advocates this
usage model. I myself have broken from this tradition because I find
the public defaulting of struct more appropriate than class and thus
prefer it 100% of the time. The benefit is I no longer need to
unhide
the constructor and I don't lose any explicit control over privacy.
So what was the reason for introducing the class keyword?
I believe the current state is just an accident in the evolution of
the language.

Originally the class concept was added to C (in the language "C with
classes"). At that stage structs and unions were still identical to
their C language counterparts.

Only later did someone ask "Why can't I have member functions in a
struct?". And there were no good reasons...

Bo Persson
Sep 24 '06 #4
44****@email.com wrote:
The class keyword created a lot of confusion that resulted in a popular
style that says struct is appropriate for POD (plain old data) and
class is appropriate for fancier things (member functions, data hiding,
inheritance). And indeed the FAQ even advocates this usage model. I
myself have broken from this tradition because I find the public
defaulting of struct more appropriate than class and thus prefer it
100% of the time. The benefit is I no longer need to unhide the
constructor and I don't lose any explicit control over privacy. So what
was the reason for introducing the class keyword?

When C++ was designed, it was to be mostly compatible with C, while
providing an object orientated language. Proper OO dictates that
objects should have private data by default, and let members be
explicitly public. If they were to make struct public by default, it
would break a lot of C code so therefore the class keyword would be used
for classes, and struct for classes that default to public. Over the
years it became accepted that structs should be used for POD types, and
classes for proper OO types. This system, while a little confusing,
allowed people to code C++ in a proper OO manner, while keeping C++ as
compatible with C as possible.

In all cases, it is probably best to explicitly state if a member is to
be private, protected or public.
Sep 26 '06 #5

Steve Pope wrote:
>
I also always use struct, since normally the first element of
a struct or class is public, and therefore it saves a keyword.
My first element is usually private because I put members first then
functions. Just my style, I guess. I know a lot of people like the
public at the top because then as users they see first what they are
going to need to use the class.

Private methods (other than possibly a disabled copy-constructor and
operator=) generally go at the bottom of my classes, thus I have 2
private sections.
I find the theory that if there's so much as a member function it
should be a class unconvincing. Everyone knows a struct
and a class are the same thing.
I will sometimes use struct for functors that have no member variables
at all. Probably because I've seen others use it in sample code.

Generally I will use the class keyword unless everything in it is
public. And generally member variables will be public only if it's
grouped-together data and then it would generally have no member
functions other than trivial constructors (often one default
constructor and one that has initial values for all the members).

Sep 26 '06 #6
Bo Persson wrote:
44****@email.com wrote:
>The class keyword created a lot of confusion that resulted in a
popular style that says struct is appropriate for POD (plain old
data) and class is appropriate for fancier things (member functions,
data hiding, inheritance). And indeed the FAQ even advocates this
usage model. I myself have broken from this tradition because I find
the public defaulting of struct more appropriate than class and thus
prefer it 100% of the time. The benefit is I no longer need to
unhide
the constructor and I don't lose any explicit control over privacy.
So what was the reason for introducing the class keyword?

I believe the current state is just an accident in the evolution of
the language.

Originally the class concept was added to C (in the language "C with
classes"). At that stage structs and unions were still identical to
their C language counterparts.

Only later did someone ask "Why can't I have member functions in a
struct?". And there were no good reasons...
My beef with class is that it's also in the tag name space. The tag name
space is a language design mistake. I suggested back in the 80's to
Bjarne that the class be in the regular name space, and leave struct in
the tag name space for C compatibility. He replied there was too much
existing C++ code to make such a change.

With the D programming language there was a chance to fix that. Structs
are for POD, classes are for objects, and both are in the regular name
space (there is no tag name space in D). Both class and struct default
to public.

Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Sep 26 '06 #7
Walter Bright wrote:
Bo Persson wrote:
>44****@email.com wrote:
>>The class keyword created a lot of confusion that resulted in a
popular style that says struct is appropriate for POD (plain old
data) and class is appropriate for fancier things (member
functions,
data hiding, inheritance). And indeed the FAQ even advocates this
usage model. I myself have broken from this tradition because I
find
the public defaulting of struct more appropriate than class and
thus
prefer it 100% of the time. The benefit is I no longer need to
unhide
the constructor and I don't lose any explicit control over
privacy.
So what was the reason for introducing the class keyword?

I believe the current state is just an accident in the evolution of
the language.

Originally the class concept was added to C (in the language "C
with
classes"). At that stage structs and unions were still identical to
their C language counterparts.

Only later did someone ask "Why can't I have member functions in a
struct?". And there were no good reasons...

My beef with class is that it's also in the tag name space. The tag
name space is a language design mistake.
I guess so. I don't seem to write code where it matters, so it's not a
big deal for me. Perhaps more of a problem to compiler writers?
I suggested back in the 80's
to Bjarne that the class be in the regular name space, and leave
struct in the tag name space for C compatibility. He replied there
was too much existing C++ code to make such a change.

With the D programming language there was a chance to fix that.
Structs are for POD, classes are for objects, and both are in the
regular name space (there is no tag name space in D).
So it seems that there really _were_ reasons to keep the structs
unchanged. Some of the memcpy-guys are now arguing that they need a
pod_struct or something, to have the compiler assure the POD-ness of
their data.

So, keeping structs as C-style PODs might have been a good idea after
all. But now there is *way* too much C++ code to make such a change.
Both class and struct default to public.
Typing an additional "public:" per class definition is not big deal,
and it also serves as documentation. Making it optional could satisfy
both sides, of course.
Bo Persson
Sep 27 '06 #8

Bo Persson wrote:
So it seems that there really _were_ reasons to keep the structs
unchanged. Some of the memcpy-guys are now arguing that they need a
pod_struct or something, to have the compiler assure the POD-ness of
their data.
Better solution is to have a standard is_pod that can be overloaded for
user-defined types.
So, keeping structs as C-style PODs might have been a good idea after
all. But now there is *way* too much C++ code to make such a change.
Useful for optimisation but something that is_pod can resolve, even if
the data members are private. basic types and pointers (including
pointers to functions) would be pods by default.

Somewhere in between POD and non-POD are movable classes/structs. A
class is movable if you can move it to another memory location without
invalidating any of its members. Obviously any external pointers to it
or its members may become invalidated by such a move.

Note that not everything is copyable or moveable with memcpy/memmove
even in C. Well not as a whole set anyway. (How do you copy or move a
linked-list?).
Typing an additional "public:" per class definition is not big deal,
and it also serves as documentation. Making it optional could satisfy
both sides, of course.
I agree though having inheritance default to public would be useful
because that's the most common type of inheritance. I guess they made
the default private to be "orthogonal".

Sep 27 '06 #9
Bo Persson wrote:
Walter Bright wrote:
>My beef with class is that it's also in the tag name space. The tag
name space is a language design mistake.
I guess so. I don't seem to write code where it matters, so it's not a
big deal for me. Perhaps more of a problem to compiler writers?
It's not too much of a problem in C because the lookups are always
qualified with 'struct' or 'union', but it's a bit of a mess in C++. The
names default to being in the main space, but then are pushed into the
tag space if there are any other declarations with the same name. The
semantic analyzer has to look in the tag space if it's expecting to see
a class name based on the context, after first looking in the regular
space and finding a name that isn't a tag. It's just goofy - and it
didn't have to be that way.
>With the D programming language there was a chance to fix that.
Structs are for POD, classes are for objects, and both are in the
regular name space (there is no tag name space in D).
So it seems that there really _were_ reasons to keep the structs
unchanged. Some of the memcpy-guys are now arguing that they need a
pod_struct or something, to have the compiler assure the POD-ness of
their data.
POD objects and OOP objects are just different and serve different
design goals. I think that any code design that tries to mix them
together (with, say, inheritance) is probably a mistake. People coming
to D programming from C++ at first find the struct(POD)/class(OOP)
dichotomy to need a bit of getting used to, but once they do, the
feedback on the choice is clearly positive.

Structs and classes are like ints and floats. Sure, you can use a float
as a loop counter, but it's just not meant for that.
So, keeping structs as C-style PODs might have been a good idea after
all. But now there is *way* too much C++ code to make such a change.
I agree. C++ is boxed in by legacy code based on decisions made 20 years
ago. This happens, of course, to all successful languages.
>Both class and struct default to public.
Typing an additional "public:" per class definition is not big deal,
and it also serves as documentation. Making it optional could satisfy
both sides, of course.
It's a minor thing, I agree. It's like a story my father told me. We
lived in a small town, and he once asked the mayor what the biggest
problem he faced was. The mayor replied that the town was evenly divided
between dog lovers and dog haters. Neither side could gain ascendancy,
so his work was occupied with constantly trying to compromise and
reconcile the two <g>.

Walter Bright
http://www.digitalmars.com
C, C++, D programming language compilers
Sep 27 '06 #10

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

Similar topics

24
3258
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
14
2325
by: Jason Heyes | last post by:
I want to write a class that supports operations on keywords belonging to the C++ programming language. The following program repeatedly prompts the user for a keyword until 'explicit' is finally...
20
2851
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
5
14406
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
9
2483
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
5
6841
by: Dylan Moreland | last post by:
I'm trying to implement a bunch of class methods in an ORM object in order to provide functionality similar to Rails' ActiveRecord. This means that if I have an SQL table mapped to the class...
4
1650
by: key9 | last post by:
Hi All I 've got no idea how to describe it on subject. See code: //ABC class Keyword{}; // user's command class Handler{}; // command's action // A Command contain A Keyword* + A Handler*
12
1965
by: Viru Rathore | last post by:
If struct keyword is support the all functionallity of class keyword, only differance i have found is accessibility of member is different in struct is public and class has private. This...
4
1313
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to...
3
1976
by: Hongyu | last post by:
Hi, I am a newbie in C++. I saw a code like the below and don't understand it. class A { public: A();
0
7093
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
7467
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...
0
5594
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,...
1
5022
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...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.