Structs vs Classes

Sep 1, 2010 at 7:24pm
hey, all i was wondering is there any differences or advantages, of using one or the other?
Sep 1, 2010 at 7:32pm
strictly speaking, they're exactly the same except classes default to private, whereas structs default to public.

Aesthetically speaking, structs are generally used for just groupings of information (data structures), whereas classes are used to represent objects.
Sep 1, 2010 at 8:07pm
Structs are remainings from C
Classes is the C++ replacement!
Sep 1, 2010 at 9:20pm
Well, from the editorial point of view the difference in their very names.
Functionally, a struct keyword leaves one option only for the constructing a compound
of their chain:

struct a{int y}; struct b{int y}; //e.t.c

struct c
{
a A;
b B;
//e.t.c.
g G;
};

void{c C;}

Of course each of them can be included in one another making eventually one to edit something like this:

x=A.B.C.D.E.F.G.y // to read y field of G struct
instead of
x=C.G.y or C->G.y

As for classes there is almost no limit to structures except for the cross reference
including compounds that may contain a lot of the unused functionality...
Sep 1, 2010 at 9:50pm
Structs are remainings from C
Classes is the C++ replacement!


This is not true.

Functionally, a struct keyword leaves one option only for the constructing a compound
of their chain


This is true of classes as well.


I'll say again.... functionally speaking... the only difference is that structs default to public, and classes default to private. Apart from that they are identical:

1
2
3
4
5
6
7
8
9
struct Foo
{
  int this_will_be_public;
};

class Bar
{
  int this_will_be_private;
};


In practice, the two are used in different styles. Generally structs have public members and are just clumps of data.

Whereas classes tend to be encapsulated objects.

This is not a "rule", though. It's just a style thing for clarity.
Sep 1, 2010 at 11:05pm
the only difference is that structs default to public, and classes default to private. Apart from that they are identical:

1
2
3
4
5
6
7

class Bst
{
   public:
    int this_will_be_private;
};


would that still default private? or does it override it?
Last edited on Sep 1, 2010 at 11:06pm
Sep 1, 2010 at 11:09pm
Default means what happens without the keywords 'public' or 'private' or 'protected'. If you add the keyword, its no longer the default.
Sep 2, 2010 at 12:09am
oh ok makes sense thanks guys i appreciate the help
Sep 2, 2010 at 6:09am

Structs are remainings from C
Classes is the C++ replacement!


Hmmm... above statements is not really true. From what I know C++ was created and built upon C. Struct already exist in C so when C++ was created and to make sure legacy C code still run perfect using a C++ compiler, the struct is retained. Classes was introduced in C++ with protection specifiers so I believe to make C struct co-exist peacefully with C++, they default struct protection specifiers to public. This mean legacy C code need not be changed when compiled using a C++ compiler.

I think the idea that time is a C++ compiler should be able compile legacy C code without developers needing to edit the C code. It must be a seam-less process. So this arise the scenario where we see struct and also class supported in C++.

Personally for me due to my C leanings, I tend to use struct and class interchangeably. Ideal should be all new C++ code should stick to class but then hehehe.... C++ cannot force me since it must support C constructs isn't it ? :)
Sep 2, 2010 at 2:14pm
Structs and classes differ in a few ways. Semantically, there are two differences: the default access specifer for members and the default access specifier for inheritance.

Beyond semantics, the two are commonly used to convey the implementor's intent. For example, struct is often used to represent "C-style" structs that are a simple grouping of data. They typically do not enforce invariants nor do they add behavior. I often see more simple user-defined types, for tag dispatching, empty base classes, etc. implemented as structs, as well.

A class is more than just a grouping of data. Classes model an abstraction and add encapsulation. They often enforce invariants and provide behavior.
Topic archived. No new replies allowed.