why class keyword in C++

V

Viru Rathore

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.
 
V

Victor Bazarov

Viru said:
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
 
N

n2xssvv g02gfr12930

Viru said:
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
 
R

Rolf Magnus

n2xssvv said:
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.
 
V

Viru Rathore

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.
 
R

Rolf Magnus

Viru said:
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.
 
P

pbruno

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
 
H

Howard

Viru Rathore said:
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
 
P

Phlip

Howard said:
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).
 
R

Rolf Magnus

Phlip said:
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).
 
D

Default User

pbruno said:
Viru Rathore ha scritto:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top