Class and Struct

  • Thread starter vishnupriya.sureshbabu
  • Start date
V

vishnupriya.sureshbabu

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

ram.groups

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

Jim Langston

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

Salt_Peter

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

tarun.mohapatra

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

tarun.mohapatra

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

osmium

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".
 
O

osmium

osmium said:
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.
 
S

Salt_Peter

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

Tomás

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
 
T

tragomaskhalos

Bo said:
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.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top