Structs vs Classes - What's the purpose? |
The history of how the two languages -- C & C++ -- evolved, one from the other shows "the purpose."
C is a general purpose, imperative procedural programming language. Program data is manipulated by lots and lots of functions that exist separate from the data. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support. Despite the low-level capabilities C was designed to encourage cross-platform programming. Same code, different hardware and operating systems.
C++ was created to be a programming ecosystem that encouraged using object-oriented design. An organic way of organizing programs. Data and procedures (functions) could be combined into a single object for storage and manipulation.
Another early design idea was "type-safety". Having the compiler flag as errors trying to assign one type (a string, for instance) to a variable of another, differing type. Say, an int.
C++ was originally tagged as "C with classes." Using C's struct as the basis for for what became classes in C++.
there are rumors that a few early c++ compilers translated to C first and compiled that |
Yup, "Cfront." Saint Bjarne's original C++ compiler that converted C++ code into C and then compiled that to machine code.
More than a few people complain/wonder about why this feature is in the language, etc. C++ they say is like a Frankenstein's Monster, cobbled and stitched together from bits and pieces swiped from elsewhere.
At first C++ was the creation of a single person, Bjarne Stroustrup. Now it is result of a committee. A giraffe is a horse designed by a committee.
C++ is revised and updated with a view for solving real world problems. Does it do it as well as it could. Debatable.
Long story short, in C++ structs and classes are red-headed step children because of the history of how C++ evolved and mutated because and from C.