470,870 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

Class and Struct

What is the main reason for prefering class over struct? Access
specifires can also be used in struct. Is there any other reason for
using class other than inheritance?

May 25 '06 #1
12 1948
vi********************@gmail.com wrote:
What is the main reason for prefering class over struct? Access
specifires can also be used in struct. Is there any other reason for
using class other than inheritance?


You can use inheritence with struct too.
The difference is the default access specifier which is public for
struct. Normally I use struct where the grouping of data is important
but not hiding. An example from the standard library is std::pair.

May 25 '06 #2
<vi********************@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
What is the main reason for prefering class over struct? Access
specifires can also be used in struct. Is there any other reason for
using class other than inheritance?


Personally, I make pod struct and anything with a method a class.
May 25 '06 #3
vi********************@gmail.com wrote:
What is the main reason for prefering class over struct? Access
specifires can also be used in struct. Is there any other reason for
using class other than inheritance?


None, there is absolutely no difference between a struct and a class.
The fact that their default access specifier is different doesn't change
that fact.
Some will argue that they prefer reserving struct for POD-types. At that
point: its a purely a question of style. And there is nothing wrong with
style so long as its consistant.
May 25 '06 #4
There is basically no difference between struct and class in c++ except
default access specifier. Struct keyword is there in C++ to mentain the
C like terms. We can do the same operations with struct as we are doing
with class also.

May 25 '06 #5
There is basically no difference between struct and class in c++ except
default access specifier. Struct keyword is there in C++ to mentain the
C like terms. We can do the same operations with struct as we are doing
with class also.

May 25 '06 #6
<vi********************@gmail.com> wrote:
What is the main reason for prefering class over struct? Access
specifires can also be used in struct. Is there any other reason for
using class other than inheritance?


It's a nice documentation aid. The convention is that struct contains only
data and class contains date and member functions. You can scan a bunch of
code and, with virtually no thought, categorize it into one of two "bins".
May 25 '06 #7
"osmium" writes:
It's a nice documentation aid. The convention is that struct contains
only data and class contains date and member functions. You can scan a
bunch of code and, with virtually no thought, categorize it into one of
two "bins".


data, not date.
May 25 '06 #8

<de*********@gmail.com> skrev i meddelandet
news:11*********************@y43g2000cwc.googlegro ups.com...
A very related link -
http://blog.devstone.com/Aaron/archi...02/24/205.aspx


But that is for the other C++ language, C++/CLI on .NET.
Bo Persson
May 25 '06 #10
ta*************@gmail.com wrote:
There is basically no difference between struct and class in c++ except
default access specifier. Struct keyword is there in C++ to mentain the
C like terms. We can do the same operations with struct as we are doing
with class also.


I disagree, a struct in C and a struct in C++ have absolutely nothing in
common, including terminology. Its like saying that a class in Java
maintains a terminology with a class in C++, that is not the case. If
you do C, you do C, period.

In fact, a class in Java is much, much closer a construct to a class in
C++ than a struct in C is to a struct in C++. Yet, one does not qualify
the class in Java as maintaining C-like terms.
May 26 '06 #11
Salt_Peter posted:
I disagree, a struct in C and a struct in C++ have absolutely nothing in common, including terminology.

I think struct's original purpose was to store more than one value, e.g.:

struct EmployeeInfo {
unsigned long id;
char name[35];
unsigned hourly_wage;
};
C and C++ definitely have this in common.

However, C++ then brought in the concept of putting functions in with the
members, and called it a class:

class EmployeeInfo {
public:
unsigned long id;
char name[35];
unsigned hourly_wage;

unsigned CalculatePay( unsigned const hours_worked )
{
return hourly_wage * hours_worked;
}
};
This functionality was also extended to "struct"... in fact, they went
the whole hog and said that a "class" and "struct" are the same thing
(except of course for default access).

If you do C, you do C, period.

Not really. I've really only ever been a C++ programmer, but if you told
me I had to write a C program, I bet I could do it.
-Tomás
May 26 '06 #12
Bo Persson wrote:
<de*********@gmail.com> skrev i meddelandet
news:11*********************@y43g2000cwc.googlegro ups.com...
A very related link -
http://blog.devstone.com/Aaron/archi...02/24/205.aspx

But that is for the other C++ language, C++/CLI on .NET.


Nope, the link is NOT C++/CLI. It's C#. In C# struct and class do have
significantly different meanings, but C++/CLI adheres to standard C++
on this issue.

May 26 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steve | last post: by
15 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by Jon Slaughter | last post: by
17 posts views Thread by Jef Driesen | last post: by
12 posts views Thread by Angus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.