I have been wondering which is better to use? A struct or a class? I have been thinking it would be a class since it's members are private by default. But I have no clue...
I have been wondering which is better to use? A struct or a class? I have been thinking it would be a class since it's members are private by default. But I have no clue...
I don't think there is such as better here, but using a class is more common. From that point of view it might by handier to use a class, especially when using a library which uses classes it leads to more consistency and easier design. And it might be easier to read, a lot of people associate a structure with the struct as used in C while they associate class with the C++ class.
I don't think there is a better here as well. I agree with Shiro. But structs in C++ are most commonly used with data members while classes more commonly include the member functions.
First of all, Do you know the difference between them?
If Yes... Then use what you want
If No... Then this thread should be about the difference between them.
none...
I thought the only difference was that a structs members are public by default while a classes are private by default. And that a class was made from structures and have expanded functionality. Is this what the difference is?
A class isn't made from a struct; a class is a struct with data members set to private by default. That's it. However, it is good coding practice to label the access of classes anyway. Personally, I use structs exclusively in the C form, so private and protected data members are ruled out.
I have seen structs used #define'd as interface in COM coding in C++, but this is an exception to the rule.
Use whichever you want... it's not a large dilemma =P
Personally I prefer class for things with functions, constructors, etc. and I use struct for things that are just data. Although you can put just data in a class and all the virtual functions and assignment operators you want in a struct =)
I say use class, unless you only have public data members, use a struct.
I see so much c++ code using c-ish structs, that I freak out everytime I see the word struct in a c++ program- but that's just me
Anyways, doesn't matter.
There's really no big difference between a class and a struct, apart from struct having public members as default.
Structs can have all features classes can, like constructors and inheritance. But most of the time structs are used as simple data-containers.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
When you apply inheritance to structs they are inherited publicly by default
Have a wonderful day.... and keep smiling... you look terrific that way
signing off...
shiv... as i know him
I use structs simply to store like data like time (mins, hours, secs) or address (house number, line1, line2, post/zip code) but I would use a class when I wanted to also manipulate that data, have the data members private and the access functions public like
Code:private: int hours; int mins; int secs; public: int gethours(){return hours;};
Compiler == Visual C++ 6.0"Come Out Fighting."