473,413 Members | 1,696 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,413 software developers and data experts.

why class keyword in C++



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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.

Thanks in advance.

Feb 17 '06 #1
12 1960
Viru Rathore wrote:
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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.


Because it's cooler that way.

V
--
Please remove capital As from my address when replying by mail
Feb 17 '06 #2
Viru Rathore wrote:

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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.

Thanks in advance.

The point of OOD/OOP is information and process hiding by creating black
boxes with only the interfaces publicly available, (the need to know
basis for what is available). Hence for OOD/OOP development of an object
most of the data and methods would normally be private. But for backward
compatibility with old C code struct had to be supported with data and
methods defaulting to being public. Personally I always include explicit
declaration of scope, (private,protected or public), within an object.

JB
Feb 17 '06 #3
n2xssvv g02gfr12930 wrote:
Viru Rathore wrote:

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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.

Thanks in advance.

The point of OOD/OOP is information and process hiding by creating black
boxes with only the interfaces publicly available, (the need to know
basis for what is available). Hence for OOD/OOP development of an object
most of the data and methods would normally be private.


However, I find it much more logical to put the public members first, since
they are the part that is interesting for the largest group of people. So
my classes always start like:

class Foo
{
public:

It's similar with inheritance. The default is private, even though one
usually inherits publically way more often.

Feb 17 '06 #4
> However, I find it much more logical to put the public members first, since
they are the part that is interesting for the largest group of people. So
my classes always start like:

class Foo
{
public:

It's similar with inheritance. The default is private, even though one
usually inherits publically way more often.

This is right but i want to know that we can do the same thing in
struct.
If we want private then we can define as private or required public we
define as public.

So why make a keyword different from struct to class.

OOD/OOP requird class keyword so make compitible to that OOAD grammer
to make this keyword.

Feb 17 '06 #5
Viru Rathore wrote:
However, I find it much more logical to put the public members first,
since they are the part that is interesting for the largest group of
people. So my classes always start like:

class Foo
{
public:

It's similar with inheritance. The default is private, even though one
usually inherits publically way more often.

This is right but i want to know that we can do the same thing in
struct.
If we want private then we can define as private or required public we
define as public.


Yes. The two differences between the keywords struct and class are:

1. The default access / default inheritance
2. "class" can be used for a template parameter, "struct" can't.
So why make a keyword different from struct to class.
I guess it's just because the name "class" is so widespread for .... well, a
class.
OOD/OOP requird class keyword so make compitible to that OOAD grammer
to make this keyword.


Something like that, yes.

Feb 17 '06 #6
"Rolf Magnus" writes:
I guess it's just because the name "class" is so widespread for .... well,
a
class.


"class" was the word used in Simula 67, and perhaps before that. I checked
to see if Smalltalk preceded Simula 67 and chanced upon an interesting
article by Allan Kay. Which I will have to read some day when I have the
time. It's some of the early history of OOP.

http://gagne.homedns.org/~tgagne/con...storyST.html#4
Feb 17 '06 #7
Viru Rathore ha scritto:

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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.

Thanks in advance. I am not sure, but from what I understood (from strousdrup book) the
need of another keyword is
to avoid to modify C struct keyword and to mantain complete
consistence/compatibility with C.
ye
Pier Paolo

Feb 17 '06 #8

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:67********************@comcast.com...
Viru Rathore wrote:
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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.


Because it's cooler that way.


Classier, anyway.

-Howard

Feb 17 '06 #9

"Viru Rathore" <ra**************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...


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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.


Backwards compatibility, both with C and with older C++ code, I assume.

In my first C++ compiler (Turbo C++ 2, I think), the keyword struct meant a
purely C-style struct, what we call a POD ("plain old data") structure
today. No member functions, no inheritance, simply public data. The class
keyword in that software indicated the new C++-style definition, with member
functions and inheritance allowed.

I (and many other programmers, I'm sure) still tend to program in that
fashion, using struct only for pure POD data holders, and class for
everything else.

-Howard
Feb 17 '06 #10

"Howard" <al*****@hotmail.com> wrote in message
news:qi*********************@bgtnsc05-news.ops.worldnet.att.net...

"Viru Rathore" <ra**************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...


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 accessbility feature user can got by using Accessor public,
private or protected.
So why class keyword is implemented in C++.


Backwards compatibility, both with C and with older C++ code, I assume.

In my first C++ compiler (Turbo C++ 2, I think), the keyword struct meant
a purely C-style struct, what we call a POD ("plain old data") structure
today. No member functions, no inheritance, simply public data. The
class keyword in that software indicated the new C++-style definition,
with member functions and inheritance allowed.

I (and many other programmers, I'm sure) still tend to program in that
fashion, using struct only for pure POD data holders, and class for
everything else.


Then we get this:

class Foo: public Bar
{
public:
void method();
private:
int x;
};

From the top: Public inheritance is more popular than private inheritance
(despite the latter is slightly more robust). But classes default to private
so we have to write 'public Bar' on most base classes.

Then, we should put the public methods at the top of the class, to document
"use me like this". And we put the privates down at the bottom, to document
"please get bored before you read this far".

So, by giving 'class' default 'private' access, as a prod to remind us to
use good design style, we have to write 'public' twice as often as we should
have.

struct Foo: Bar
{
void method();
private:
int x;
};

That's cleaner, except we should use 'struct' to mean "plain ole data
bucket", and not let our colleagues think that's not a class.

#define CLASS_ struct

CLASS_ Foo: Bar
{
void method();
private:
int x;
};

And that's just evil (and hard to grep for).
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 17 '06 #11
Phlip wrote:
I (and many other programmers, I'm sure) still tend to program in that
fashion, using struct only for pure POD data holders, and class for
everything else.
Then we get this:

class Foo: public Bar
{
public:
void method();
private:
int x;
};

From the top: Public inheritance is more popular than private inheritance
(despite the latter is slightly more robust). But classes default to
private so we have to write 'public Bar' on most base classes.

Then, we should put the public methods at the top of the class, to
document "use me like this". And we put the privates down at the bottom,
to document "please get bored before you read this far".

So, by giving 'class' default 'private' access, as a prod to remind us to
use good design style, we have to write 'public' twice as often as we
should have.


2 is not just twice as much as 0. :)
struct Foo: Bar
{
void method();
private:
int x;
};

That's cleaner, except we should use 'struct' to mean "plain ole data
bucket", and not let our colleagues think that's not a class.

#define CLASS_ struct

CLASS_ Foo: Bar
{
void method();
private:
int x;
};

And that's just evil (and hard to grep for).


I agree with you on all points (except for the "twice as often" part).

Feb 17 '06 #12
pbruno wrote:
Viru Rathore ha scritto:
So why class keyword is implemented in C++.

I am not sure, but from what I understood (from strousdrup book) the
need of another keyword is to avoid to modify C struct keyword and to
mantain complete consistence/compatibility with C.

Except that it isn't implemented that way. I'd be happier if what you
say is true, and struct was purely a backward compatible C construct,
strictly POD and all that. However, that's not the way it is.

Brian
Feb 17 '06 #13

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

Similar topics

24
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
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
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
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
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
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
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*
4
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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.