[comp.lang.c++] C++ as a better C

bobk@hplsla.HP.COM ( Bob Kunz) (04/23/88)

I recently attended the AT&T training on C++ and an interesting question
came up in one of the discussions about the use of C++ "simply as a 
better C".  The opinions ranged from only use C++ if you intend to use
all it's features to it's fine to use as a better C because it has type
checking and one can choose to use the other features if one wants to.

What's the opinion of the net?  Have people used C++ as a better C and
been successful?  Or do people only consider C++ when speaking object
oriented in the same breath?  Does the problem need to be solvable by
object oriented techniques before C++ is considered?  Why did you not
use C?  And would an ANSI-C compiler have made a difference?

--

Bob Kunz			hplsla!bobk
Hewlett-Packard			AT&T 206-335-2135 
Lake Stevens Division
8600 Soper Hill Rd.
Everett, WA 98205

djones@megatest.UUCP (Dave Jones) (05/05/88)

in article <6590041@hplsla.HP.COM>, bobk@hplsla.HP.COM (                Bob Kunz) says:
> 
> 
> I recently attended the AT&T training on C++ and an interesting question
> came up in one of the discussions about the use of C++ "simply as a 
> better C".  The opinions ranged from only use C++ if you intend to use
> all it's features to it's fine to use as a better C because it has type
> checking and one can choose to use the other features if one wants to.
> 
> What's the opinion of the net?  Have people used C++ as a better C and
> been successful?  Or do people only consider C++ when speaking object
> oriented in the same breath?  Does the problem need to be solvable by
> object oriented techniques before C++ is considered?  Why did you not
> use C?  And would an ANSI-C compiler have made a difference?
> 



Since I do my C programming in the object-style, for me there
is no conflict.  C++ makes it easier to program the way I would anyhow.
Once you've heard of object-programming and understood it, why would
you want to program any other way? :-)

But here's another "better C" reason:  You can redefine
operators.  So if somebody gets "programmed into a corner" with
direct accesses to a data-structure all over the place (no doubt in
old code written by somebody who panicked an ran long ago) you can fight
your way out.






                  --  Dave J. 

ark@alice.UUCP (05/07/88)

In article <6590041@hplsla.HP.COM>, bobk@hplsla.UUCP writes:
 
> What's the opinion of the net?  Have people used C++ as a better C and
> been successful?  Or do people only consider C++ when speaking object
> oriented in the same breath?  Does the problem need to be solvable by
> object oriented techniques before C++ is considered?  Why did you not
> use C?  And would an ANSI-C compiler have made a difference?

Yes, people have used C++ as a better C and been successful.
Doing so is an excellent way to get your feet wet.

If you have an ANSI C compiler, it will give you many of the
``better C'' things of C++, but not all.  For instance,

	int f();

in ANSI C means

	``f is a function returning int and I haven't said anything
	  about its arguments''

This is essential for C compatibility, but it encourages sloppy
coding and suppresses type checking.  The same declaration in C++
means

	``f is a function returning int with no arguments''

And, of course, once you start using C++ as ``a better C'' you will
find it much easier to explore the other aspects of the language.

bs@alice.UUCP (05/07/88)

I have seen reports on several large internal AT&T project using C++
as ``a better C''. They were happy and even the crudest measurements
showed improvements that could not be attributed to other sources.

I am NOT talking about toy projects or projects staffed exclusively
by MIT PhDs or projects chosen for a specifically perfect fit with
some C++ features.

An interesting aspect is that the benefits appears to increase with time
as peoples expertice grows and as people drifts towards using C++ as
a ``data abstraction language'' where those techniques are appropriate.

The smooth integration of the various features seems to ease the introduction
of new techniques into environments where  one could not easily ``stop
the world'' while changing to a new system and allows programmers and
designers to learn at their own pace.

This approach can work and so can the approach of throwing people into
``object-oriented programming'' head first. I strongly suspect that
these approaches suplements each other and allows people to use a
style of programming/design that suits their temprament, skills, and needs.

I don't see people using C++ as only ``a better C'' for long, though.
With C++ in place the drift towards the more advanced features and
techniques is just about inevitable - the benefits and opportunities
simply becomes too obvious to resist.

jos@cs.vu.nl (Jos Warmer) (05/09/88)

In article <6590041@hplsla.HP.COM> bobk@hplsla.HP.COM (Bob Kunz) writes:
>
>I recently attended the AT&T training on C++ and an interesting question
>came up in one of the discussions about the use of C++ "simply as a 
>better C".  The opinions ranged from only use C++ if you intend to use
>all it's features to it's fine to use as a better C because it has type
>checking and one can choose to use the other features if one wants to.
>
>What's the opinion of the net?  Have people used C++ as a better C and
>been successful?  Or do people only consider C++ when speaking object
>oriented in the same breath?  Does the problem need to be solvable by
>object oriented techniques before C++ is considered?  Why did you not
>use C?  And would an ANSI-C compiler have made a difference?
>
We have been using C++ entirely for several years now.
Recently, we committed ourselves to writing a big program
(50000 lines) in C

We decided to use C++ as a "better C" in this project and it worked out
very well.  We used C++ used in a lint-like manner.
Many hours of debugging were saved by the type-checking and
argument-checking facilities of C++.

I would advise using C++ to anyone. If not for its extra facilities,
then at least for use as a MUCH better lint.

				  Jos Warmer
				  jos@cs.vu.nl

rlb@polari.UUCP (05/11/88)

In article <706@vlot.cs.vu.nl>, jos@cs.vu.nl (Jos Warmer) writes:
> In article <6590041@hplsla.HP.COM> bobk@hplsla.HP.COM (Bob Kunz) writes:
> >
> 
> I would advise using C++ to anyone. If not for its extra facilities,
> then at least for use as a MUCH better lint.
> 
> 				  Jos Warmer
> 				  jos@cs.vu.nl

I can't glean too much from the C++ book about what kinds of semantic checks
the compiler performs beyond type checking for expressions and function args
(and checking to see that all possible values are accounted for if you switch
on an enumeration).  Doesn't lint provide type checking?  What semantic checks
does C++ provide that exceed those of (Unix) lint?  A comparison of features
would be most enlightening.

Ron Burk

bobk@hplsla.HP.COM ( Bob Kunz) (05/17/88)

As a followup to my question, I thought I would include some of the mail
responses I received on C++ as a better C......

(I've deleted the re-writes of my original question for brievity)

Bob Kunz

-------
From hplabs!snyder@hplabsz Thu May  5 08:38 PDT 1988
Date: Thu, 5 May 88 08:13:10 pdt
Organization: Hewlett-Packard Laboratories

In article <6590041@hplsla.HP.COM> you write:
> blah, blah, blah

Our observation is that most C++ users are either using it as a "better C"
or are using it to create data abstractions, as opposed to full
object-oriented programming (with polymorphism and inheritance).  The
latter folk might say they are doing object-oriented programming, however.
I think it is quite reasonable to use C++ as a "better C".

  Alan


-------
From sun!dhelrod Thu May  5 10:25 PDT 1988
Date: Thu, 5 May 88 08:31:34 PDT
Organization: Sun Microsystems, Mountain View

In article <6590041@hplsla.HP.COM> you write:
> blah, blah, blah

Caution:  I have written less than 20,000 lines of C++ code!

I would say that it depends on your programming style.  I am a
"hacker".  I tackle a problem by writing a piece, and then adding some
and then adding some more.  When I finally have a kludge that works,
then I figure I probably have a good understanding of the solution.
Only at this point do switch to "architect/planner" mode.  I sit down
and plan out an elegent solution that that I not embaressed to let
others see and use.

C++'s type checking is more forgiving than lint.  So, to me it does a
GREAT job as a "better C".  When I am in hacker mode, I find that my
code development goes MUCH faster with this kind of feedback.  (If C++
was a little better about actually identifying the line a problem
occured on, it would be easier, but I have learned to intuit what it
means instead of what it says.)  I have (twice) written programs in C++
and then "ported" them to C.  This would have been made easier with an
ANSI C compiler (function definitions, etc.).

Designing a good orthogonal, complete but not to restrictive, class is
REALLY tough!  More experienced C++ users say that there are no tricks
here.  Experience makes it easier, but choosing the right
functionality is non-trivial.  

When in "architect/planner" mode, I find the OO aspects of C++ to be 
fun and useful.  The programs are much easier to read, write and
understand.  I believe that as I get better at class design, I'll more
use the OO capabilities earlier in my process.

I hope you summarize the responses you get and post the summary to
the net.

David H. Elrod
Graphics Products Division.
Sun Microsystems, Inc.
dhelrod@sun


-------
From ihlpe!dopey Thu May  5 12:57 PDT 1988
Date: Thu, 5 May 88 08:54:08 PDT
Organization: AT&T Bell Laboratories - Naperville, Illinois

Funny (and very impractical).  I've found that I really don't
enjoy using C any more because C++ has the typechecking and all.
The problem is, I've also gotten used to classes, so it's hard
to do the "reduced"C++/"enhanced" C type coding.  As an
intermediate step, using C++ as enhanced C is reasonable; but
using C++ changes your model so eventually you really can't use
it as just an extension.

well, for me anyway.

james blasius
ihnp4!gomez!dopey


-------
From ulysses!north Fri May  6 10:53 PDT 1988
Date: Thu, 5 May 88 16:54:01 EDT
Organization: AT&T Bell Labs, Murray Hill

fact
wouldn't go back to C
even ANSI C
noalias indeed
-- 
Parturiunt montes, nascetur ridiculus mus!


-------
From fischer@uunet.UU.NET Sun May  8 19:31 PDT 1988
Date: Sun, 8 May 88 22:36:01 +0200
Organization: Dept. of Comp. Sci., Aalborg University, Denmark

In article <6590041@hplsla.HP.COM> you write:
> blah, blah, blah

Well, I shifted (incremented ?) from C to C++ a while back, and has
been using C++ ever since. This includes both larger projects, where
the OOP style is mandatory, and smaller thing where it is simply nice
to have the compiler check function arguments etc.

I do have an ANSI C compiler (GNU), but I don't use it much. Once you
have started using C++ you might as well stick to it---no point in
using two languages, you'll just end up being utterly confused.

It seems to me that if you start using C++ as simply a better C you
will soon start using some of the features that makes C++ stand out.
After a while you come to depend on it!

We will of course need to program i C for many years to come---there
is simply too much code out there that has to be maintained*.

Regards,
  Lars

*Fortran's not dead, it just smells funny.
-- 
Lars Fischer,  fischer@iesd.dk, {...}!mcvax!diku!iesd!fischer
Life is hard, and then you die.

jos@cs.vu.nl (Jos Warmer) (05/20/88)

In article <456@polari.UUCP> rlb@polari.UUCP (rlb) writes:
>In article <706@vlot.cs.vu.nl>, jos@cs.vu.nl (Jos Warmer) writes:
>> In article <6590041@hplsla.HP.COM> bobk@hplsla.HP.COM (Bob Kunz) writes:
>> >
>> 
>> I would advise using C++ to anyone. If not for its extra facilities,
>> then at least for use as a MUCH better lint.
>> 
>> 				  Jos Warmer
>> 				  jos@cs.vu.nl
>
>I can't glean too much from the C++ book about what kinds of semantic checks
>the compiler performs beyond type checking for expressions and function args
>(and checking to see that all possible values are accounted for if you switch
>on an enumeration).  Doesn't lint provide type checking?  What semantic checks
>does C++ provide that exceed those of (Unix) lint?  A comparison of features
>would be most enlightening.
>
>Ron Burk

I don't know all the differences, but generally the error-messages of C++ are
much more informative then those from lint. Sometimes C++ finds more errors
than lint.
Furthermore lint gives too many useless messages, which makes it more difficult
to find the messages that are important.

Some examples to show you the flavor of messages from lint and C++:
==========================================================================
In this example the message from C++ is much more informative:
This happened to me yesterday, when I added the variable report to main().
I did not understand the message from cc (which was the same as lint),
but after running C++ the mistake was clear.

    extern report();
    
    main()
    {
        char* report = (char*)0;
        /* lots of code, so you don't see the declaration of report anymore */
        report();
    }

lint: line 7: illegal function
C++ : line 7: error: call of report; report is a char *
==========================================================================
In this example the message from C++ is more informative:

    main() 
    {
	 char* s;
	 int  i = s;

	 i = s;
    } 

lint: line 6: warning: illegal combination of pointer and integer, op =
C++ : line 6: error: bad assignment type: int  = char *
==========================================================================
In this example the message from lint is useless, and the error that C++
found, was not detected by lint.

    int hello()
    {
    }

lint: hello defined( x.c(2) ), but never used
C++ : line 3: warning: no value returned from hello()
==========================================================================

		  Jos Warmer
		  jos@cs.vu.nl

karl@haddock.ISC.COM (Karl Heuer) (05/24/88)

In article <735@vlot.cs.vu.nl> jos@cs.vu.nl (Jos Warmer) writes:
>In this example [empty function] the message from lint is useless, and the
>error that C++ found, was not detected by lint.
>  lint: hello defined( x.c(2) ), but never used
>  C++ : line 3: warning: no value returned from hello()

I could just as easily say that the message from C++ is useless, and the error
that lint found was not detected by C++.

I'll grant that the C++ error messages may be more informative (though I don't
think "illegal combination of pointer and integer, op =" is so bad once you
get used to it; the only two things it could mean are "int = pointer" or
"pointer = int").  But lint is, by design, stricter than C -- it will flag
"questionable" constructs that are quite legal; it's not necessarily a Good
Thing to include all of lint's warnings in C++.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint