Everything you can do in C++, I can do in C, or Assembly or Python or Rust, etc etc. Like whats the point??? 🤷 |
Well, this risks hijacking the thread, but....
Sure, entire operating systems are written in C, and those are among the most ambitious targets made.
However, that it can be done isn't the point.
Consider in C the simple integer and float (and double). If you write in assembler you know full well that if you use floating point instructions on an integer, you get gibberish. Likewise, using integer instructions on floats or doubles produces largely unusable results. The programmer makes the selection, because in assembler the only real difference between integers, floats and doubles is the format of the bits.
In C, however, the selection is automatic by type. The type used makes the appropriate selection of the processor's instructions, and I've not seen it make a mistake on this selection on 40+ years of software development. I submit that this behavior, the selection of an instruction or process by type is the behavior of an object.
You know the cliche, "If you have a hammer, all your problems look like nails." What if the problem is a screw? Would the hammer likely produce acceptable results? Probably not. Yet, who among us hasn't flipped a screwdriver the "wrong way around" to pound in a small nail?
It is common to all human thought that a strong associate exists between things and processes. We don't drive a bicycle the way we drive a car. Even the word "drive" seems out of place in that phrase. For every thing we do we have strong associations between knowledge and objects, even theoretical ones.
You don't use trigonometry to solve chemical equations. There no reason to apply quaternion algebra for simple addition.
In C, the family of fread/fwrite/fclose (and related functions) either require or return a FILE *. Other than fopen itself, there's hardly any use for these functions without a FILE *. It is a structure filled out by open, used by all the others, and basically deconstructed by fclose. These are very old, yet they outline what is a loosely object like collection.
Therein is the point.
Where human thought commonly requires the association of objects and behaviors, it should not be puzzling that language would reflect that, as in the case of inappropriate phrasing with "drive" with "bicycle". There is leverage to be gained.
Write a GUI Windows application of some ambition in C, the way I did it back in the 80's, learning from Petzold's text. Then, grab a copy of either Qt or WxWindows and write the same application.
It will take you only that experiment to clearly demonstrate why OOP matters.
If that experiment doesn't make it clear, it never will be.
Of course, Java and C# fans would argue they have better support for GUI applications than either Qt or WxWindows (or MFC, or ATL or any number of frameworks of the past).
Then, try to find a product of ambitious design, like Photoshop, 3DS Max, Revit, AutoCAD, Inventory, Mudbox, Rhino, Cinema4D, UE4, Unity, or any number of applications from spreadsheets to SQL engines, written in C# or Java. The list will be comparatively small.
Then, find them in C.
The list will be older products, likely not for more than one operating system, not easily ported between operating systems, and quite probably just operating systems.
Python finds a place among casual programmers from other professions. They don't have time to master the complexity (and nastiness) of C++ or C. They may just as much prefer Java or C# (I've seen all 3), but they share something in common. They are not, typically, among the primary products in demand.
That has its place, so I'm not saying such software or those languages are inferior. They are, however, inferior for a certain type of application requirements. C++ isn't a language for everyone. Certainly C even less so.
Yet the notion of OOP, which isn't all that well defined or clear, brings within it indispensable leverage. The originator of the part of the term "Object" wasn't even clear on what it has come to mean, and it means to many different things that aren't even about objects themselves.
That goes back to the notion of C's integers selecting behavior based on type. That isn't a class, and there's nothing of a template. There's no language feature involved, because it is otherwise entirely opaque to the programmer. There's also no leverage available to apply it elsewhere by choice. C has no such feature. C++ does. That's why one can fashion a library of vectors, matrices, quaternions and such to operate linear algebra in a fashion similar to their simpler counterparts. Certainly 3D graphics applications have been written in C, but like writing a Windows GUI in C, it takes a lot more work.
a = b * c;
Seems like an obvious statement. Without types declared, however, it is a fragment easily assumed to represent either integers or floats. Yet, this could be a rotation of vector b by quaternion c, producing a view in vector a according to a camera's position.
To do that in C would require something like,
rotate( a, b, c );
...and so what? It would work, certainly, where the function does the same thing to the same data.
Well, not so fast to assume so. In Eigen, the statement a = b * c may not do what it looks like it's doing at first. Where b and c may be defined as n dimensional matrices and vectors, the resulting transformation might be called with a function in C, as was the rotate function, but that's not what Eigen does. The operator * fires an instruction generator, but does not perform the multiplication of the vector by the matrix. Instead, it encodes what that instruction is, along with the parameters, for transport through the =, such that then, in the assignment operator a runtime optimization can be chosen for whatever chain of operations have been concatenated for the most efficient processing. It may be expressed, in a different line, as a = (b * c) * d, where now the various * operators chain instructions together, as required, to feed those instructions to the assignment operator for threaded scheduling of the solution.
Not something you can create in C# or Java, certainly not Python or Perl, C or Fortran. D can do it.
...but who knows D?
R might. I haven't checked.
This is not to imply the objects are the answer to programming problems, and certainly a student of the language should realize that classes aren't always the right thing to build.
The real point is that as the language evolved over 30 years there accumulated a number of leveraged features which incrementally follow the way humans think. It is far from complete, and may have more to go than how far it has come.