Re: Is the concept of class optional in C++?
On Apr 7, 8:29 pm, jacob navia <ja...@nospam.com> wrote:
Hi
We were discussing this point in comp.lang.c
1) Some people say that templates and classes are "optional" in C++.
2) I argued against this. My reasoning is as follows:
C++ is compatible (up to a certain point, but mostly) with C. Then,
actually you can program in C, without using any C++ features OF COURSE.
C++ would be then an "optional" language.
:-)
There could be 2 points of view. One from the code that the programmer
writes and other that he/she uses i.e. the standard library. Classes
are one of the building blocks of OO programming. If you don't use it,
you are free to but that won't mean you are not programming in OO not
that you are not programming in C++. C++ is not strictly OO and allows
generic functional programming very beautifully. If you don't use
templates, again you are free to since you might be having a fantastic
library at hand that could be exposing APIs based on OO and templates.
You are not obliged to do that yourself.
Now, if you don't even use the C++ standard library (excluding the C
standard library) again you are free to (i.e. you are not using
templates or classes in any form, not yourself directly or using a
library i.e. indirectly). Still there's lot to C++ than just that.
Let's see what you are probably left with if you don't want to use
classes and templates either directly or indirectly:
1) C standard lib
2) C style structs
3) Arrays
4) void* and function overloading (for generic programming)
5) Error code return value based exception/error handling
6) Function overloading
7) Libraries written in C
8) <add things that I have missed out>
So, you are basically writing C code which compiles on a C++ compiler
with few C++ features that you can easily not opt to use either. Then
why bother with C++?
But I think that is wrong. One of the central concepts in C++
is the concept of class and inheritance, and all its associated baggage
like constructors/destructors/copy constructors/ what have you.
This is not optional at all.
Optional as in you can compile your code with a C++ compiler but then
you are not using most of the C++ constructs which are its salient
features.
Another C++ concept are templates. They are a central part of C++ and
its standard library.
These are not "optional" features.
Following the above logic you could even say that division is
optional. If you do not use it in your program, you never would
think it was there :-)
Exactly. If that is how one wants to use it, why just call it to be C+
+ code? For formality :-) sake?
I tay by my definition. The concept of class and inheritance is
one of the central concepts of C++ and it is not optional at all.
classes and inheritance are central concepts of writing OO code in C+
+. As for programmer himself he could write functional code but use
libraries that exploit those features, for example: containers,
iterators, algorithms, C++ IO. Is that allowed when people argue that
you don't "have" to use classes and templates i.e. using those
features indirectly? That would be very valid C++, of course.