Urgent

Sep 11, 2012 at 4:34pm
Whats the difference between structures and classes (other than the privacy factor for the member variables of classes) when we can do same things in both ?
Sep 11, 2012 at 4:54pm
There is no difference, other than default privacy. In C days, structs just held variables and no methods. I don't know of anyone who uses structs as classes, even now. They just still get used as C structs. I believe they made it in C++ for capability with C.
Sep 11, 2012 at 4:59pm
closed account (1yR4jE8b)
I, actually, mostly use structs instead of classes most of the time in C++ to save a bit of typing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct Foo
{
    Foo();
    Foo(const Foo& other);
    ~Foo()
    
    const Foo& operator=(const Foo& other);
    
    /* other methods */
    
private:
    int x;
    float y;
    double z;
};

/* instead of */

class Bar
{
public:
    Bar();
    Bar(const Bar& other);
    ~Bar()
    
    const Bar& operator=(const Bar& other);
    
    /* other methods */
    
private:
    char x;
    bool y;
    float z;
};


A habit I picked up from my recent Ruby excursions I guess. Not a huge deal really, just a personal preference I have.
Last edited on Sep 11, 2012 at 4:59pm
Sep 11, 2012 at 4:59pm
I wanna know their difference in C++ specifically...
If we can declare functions in both; whats the point of classes then ?
Sep 11, 2012 at 5:14pm
I wanna know their difference in C++ specifically

I already told you.

If we can declare functions in both; whats the point of classes then ?


Because in OOP you use classes to define object templates. Not structs. It's what people are used to seeing, and what they want to see. C++ was designed to be fully compatible with C, so the struct had to say. POD structs in C++ are supposed to be compatible with C structs, but you can use structs in C++ just like classes if you so desire.

@darkestfright,

You actually don't save any typing. In your class, you can define your private variables above your public modifier, and then you don't have to type private. Doing that, you actually save two key strokes if you use classes :D
Sep 11, 2012 at 5:23pm
In C++ structs and classes are exactly the same thing (aside from the default storage modifier, as already mentioned before). struct is there to support legacy C code (and most people will use struct to indicate they want POD), class is there because 'class' is the word thats most commonly used to describe the concept.
Sep 11, 2012 at 5:42pm
closed account (1yR4jE8b)
You actually don't save any typing. In your class, you can define your private variables above your public modifier, and then you don't have to type private. Doing that, you actually save two key strokes if you use classes :D


But I don't put the private members at the top, I put the public stuff at the top because that's what I want people to see first. I jam all the private stuff at the bottom because people shouldn't be looking at it.
Sep 11, 2012 at 5:59pm
But I don't put the private members at the top, I put the public stuff at the top because that's what I want people to see first. I jam all the private stuff at the bottom because people shouldn't be looking at it.


But if your class definition is reasonably sized, top or bottom shouldn't really matter anyway. I don't know, I tend to stick private up top and public on bottom.
Sep 11, 2012 at 6:23pm
People probably shouldn't be looking at the header to figure out how to use a class anyway. They should be looking at the documentation.
Sep 11, 2012 at 10:53pm
closed account (1yR4jE8b)
Why not both? Especially if you use something like Doxygen.
Topic archived. No new replies allowed.