+ 5
C++ structure or class?
Based on the technical specifications of C++ there are no major differences between structures and classes in C++. One clear difference is the fact that structure members are public by default while class members are private. From your experience as a programmer when do you choose a structure or a class and why? Personally, I use a struct when an object has only attributes (e.g. database record) but not any behaviour requiring member functions. For anything else I use classes.
5 odpowiedzi
+ 9
Having come from a time when OOP didn't exist and people were fighting over structured programming, I tend to require two different functions accessing a data structure before making it a class.
I've been told multiple times by purists I don't understand OOP to which I respond: I did OOP code before any language was created to support it. If all access to the structure is limited to one function, that is OOP even without the class overhead forcing it.
+ 8
@rudolph this is C++ code.
struct point {
int x, y;
} dataPoint;
void setPoint(int x, int y) {
dataPoint.x = x;
dataPoint.y = y;
}
While this isn't a place I'd use a structure as it needs lots more methods to be useful, it does give a complete example.
A place I use structures is parsing input buffers where a get token rountine reads the input buffer into the structure on the first call and returns the first token of the input storing state information in the structure. Each additional call parses the next token out of the structure maintaining the state information. The structure allocation initializes the state information to know the buffer requires reading on the first call and no other code beyond the get token routine makes access to the structure.
This is a perfect example of OOP without using language support. Prior to C++, all data was declared in the source file. The header provided the functions to provide a pseudo class interface. The single class source had a #ifdef main that performed all testing of the class. After it tested correctly, it was compiled with a flag to disable the main and the new version of the object was available to the larger system for linking.
+ 6
I was coding structured using goto's because it made it easier to understand the code and prevented bugs, when I started in Fortran. Later, that year I discover Algol and my Fortran code was a perfect match for it's structured statements.
The same thing happened, when C++ showed up. My C code was simple to convert to classes. Making data accessible to anywhere introduces bugs that are difficult to find especially in 200K to 2M of source code lines.
Done right structured programming and OOP makes debugging programs simple because each method has a limited set of possible tests to prove it is error free. Done wrong and the interactions between methods can make proof of your code being error free impossible.
+ 5
@rudolph flash, I believe structure is a feature inherited from C. Classes are an object oriented concept added with C++. I think one of the primary reasons is encapsulation, data hiding and polymorphism.
+ 4
Thank you @John Wells! It is interesting to know how personal experience is also a factor that determines the programmer's style.