Is the concept of class optional in C++?

J

jacob navia

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.

:)

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.

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 :)

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.
 
A

Abhishek Padmanabh

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.
 
A

Abhishek Padmanabh

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.
 
J

Juha Nieminen

jacob said:
1) Some people say that templates and classes are "optional" in C++.

Naturally you can make programs in C++ which do not use classes nor
templates in any way. On the other hand, if you do that then what you
have is basically a slightly extended C. If you want to take advantage
of the full potential of C++ (to, for example, write *safer* code), the
best way is to use classes and, in some cases, templates.

I'd say that using classes and templates in C++ is optional, but
avoiding them is not very smart.
 
A

Abhishek Padmanabh

Is that a joke?

Probably, I chose a wrong word. But why? It allows generic programming
quite effectively via templates and iterators, example from the
standard library itself could be the algorithms from <algorithm>.
 
B

Bo Persson

Juha said:
Naturally you can make programs in C++ which do not use classes nor
templates in any way. On the other hand, if you do that then what
you have is basically a slightly extended C. If you want to take
advantage of the full potential of C++ (to, for example, write
*safer* code), the best way is to use classes and, in some cases,
templates.

I'd say that using classes and templates in C++ is optional, but
avoiding them is not very smart.

Well, it's like saying that functions and variables are optional in C.
Just write main() as one big asm block!


Bo Persson
 
J

jacob navia

Bo said:
Well, it's like saying that functions and variables are optional in C.
Just write main() as one big asm block!

Yes. I think making classes optional in C++ is
taking everything away!

Division is optional then! You never divide.
Period!

:)

How much can you take away from C++ ?
 
L

lbonafide

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:

Yes, kind of like brakes are optional while driving a car. You don't
need to use them to stop, but it's better if you do.
 
I

Ian Collins

jacob said:
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.
You have become completely fixated with constructors and destructors.
Why are you so obsessed with them? As an implementor of a C compiler
system do you feel threatened?

Constructors and destructors are a tool, you can use them in your code
or you can ignore them in favour of initialisation and destruction
methods. Obviously you will then have to to handle your own memory
management, but there's nothing in the language to prevent you making a
rod for your own back.

I'm still looking forward to seeing your pure C implementation of RAII,
a point you continue to evade.
This is not optional at all.

Another C++ concept are templates. They are a central part of C++ and
its standard library.

These are not "optional" features.
Some here would argue that templates should not be written in
application code but only in libraries. Most of the C++ code I see
uses, but does not introduce, templates.
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.
OO is one of the paradigms supported by C++, but it isn't the only one.
One of the beauties of C++ is you can replace most inheritance with
composition by using - templates!
 
J

James Kanze

We were discussing this point in comp.lang.c
1) Some people say that templates and classes are "optional" in C++.

Define "optional". The following doesn't use either, and is
certainly a legal C++ program:

#include <cstdio>

int
main()
{
std::printf( "Hello, world!\n" ) ;
return 0 ;
}

In practice, I don't think you can write much C++ without
classes, since C++ doesn't have structures. (The keyword struct
defines a class.) But since a class with only public data
members is indistinguishable from a C struct, who cares.
Templates, of course, are completely optional, and I've written
a lot of C++ code which doesn't use them. (Don't forget that
they are a relatively late addition to the language.)

Practically, of course, the question is: why would you want to?
I spend something like ten years programming in C, defining a
struct and a set of functions to manipulate it, and crossing my
fingers that no one accessed it other than through those
functions. At the very least, you want the access control of
classes.

Beyond that, there are certain (very common) cases where
inheritance, templates or operator overloading make life
significantly simpler, and the resulting code significantly more
robust. Why not use them then?

[...]
But I think that is wrong. One of the central concepts in C++
is the concept of class and inheritance,

Historically, and even today, the essential thing that C++ adds
to C is access control. All the rest are nice added features,
which help solve certain problems; access control is
fundamental.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top