# Translating c examples to c++ as I go along...

This topic is 3164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, My summer vacation just started, and I figure this would be a great time to teach myself some new things. I've found this book "Game Programming All in One, 2nd Edition" which teaches the reader to build up a 2D game engine in C. I'm not entirely new to coding, and have made a game using a pre-existing engine from school, but this was coded in C++. My question is: are the similarities between c and c++ big enough that I can read the book to grasp the basic design concepts and then translate that to c++ code, or would it be smarter to teach myself c syntax first? (this would probably look good on a resume and it would potentially make understanding code from libraries easier). Also, any suggestions for books that teach the reader to build up a (2D) game engine using c++ would be very welcome.

##### Share on other sites
Since C++ merely extends C, every valid C program is also a valid C++ program.
So if you can read and write C++ code you should have no problem at all grasping C code, you don't have to "teach yourself c syntax".
(There are a few subtle differences.)

But you might be better off picking up a book that uses C++ and an OO approach instead.
I guess I can't really recommend you a book since most of my books are in german.^^

##### Share on other sites
Quote:
 Original post by DraganOSince C++ merely extends C, every valid C program is also a valid C++ program.

Incorrect.

##### Share on other sites
Quote:
 Original post by DraganOSince C++ merely extends C, every most valid C programs is also a valid C++ program.

Example:
int main (int argc, char *argv[]) {    int foo [argc];    return 0;}

C++ 2003 doesn't know variable length arrays, C99 does.

And this I leave to you as an exercise:
int main (int argc, char *argv[]) {    int template;    return 0;}

For starters, see this.

##### Share on other sites
Quote:
 Original post by picklejuiceMy question is: are the similarities between c and c++ big enough that I can read the book to grasp the basic design concepts and then translate that to c++ code, or would it be smarter to teach myself c syntax first?

When writing code, the knowledge related to the business area (APIs, idioms, strategies, designs, specific algorithms) represents about 90% of what you need to know. The generic knowledge related to the language itself represents about 10%, of which 1% is the syntax of the language itself.

Learning a book like that will teach you how to write games, how to write games in C, and how to write programs in C, at various levels of knowledge. Some of that knowledge (mostly, how to write games) will translate fully to other languages, including C++, while other parts will translate incompletely (how to write games and programs in C) because the languages are superficially similar.

C and C++ are different languages, the mindset and approach to using each is wholly different from the other. C is about allocating values and applying procedures to those values (to intialize, process and uninitialize them), while C++ tends to make a lot of those processes implicit and therefore involves idioms that look, feel and react differently.

Quote:
 Also, any suggestions for books that teach the reader to build up a (2D) game engine using c++ would be very welcome.
Programming an engine is harder than building a game, in the same way that programming an accounting program is harder than being an accountant, and programming a program to do X is harder than doing X—not only do you need to know what doing X involves, you also have to think about creating a program that does it.

Until you know what developing a game involves, you will have trouble understanding what an engine should do. And knowing what developing a game involves, sadly, requires you to develop a game first and learn firsthand from the experience.

Quote:
 Original quite by CromulentIncorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original quite by CromulentIncorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.

No his point was incorrect.

Not every valid C program is a valid C++ program. I also fail to see how you can misquote that as the context of his post was consistent.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original quite by CromulentIncorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.

I checked DraganO's post again. Without being a logician, his post reads like "Every valid C is also valid C++. Not every valid C is also valid C++". Hence I think the content of Cromulents post was really undefined, because a paradoxon can't be incorrect, but it also cannot be correct.

Quote:
Original post by Cromulent
Quote:
Original post by ToohrVyk
Quote:
 Original quite by CromulentIncorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.

No his point was incorrect.

Not every valid C program is a valid C++ program. I also fail to see how you can misquote that as the context of his post was consistent.

What ToohrVyk wanted to say is that the block you misquoted from DraganO had a postfix of "(There are a few subtle differences.)", which you have oversighted.

##### Share on other sites
The essence of his post was "With a few minor changes, every valid C program is also a valid C++ program". Can you think of any major changes that would need to be applied to a C89 program (small syntax changes, and variable renaming, are minor) for it to be compiled by a standard C++ compiler?

I will admit that the post was poorly worded, since the "With a few minor changes" only appeared two sentences later as the "There are a few subtle differences" parenthesis. However, the document he knowingly linked to (and phresnel also linked to later in the thread) is in direct contradiction with the unmitigiated assertion that "Every valid C program is also a valid C++ program" and I can only see two possible explanations:

1. DraganO is stupid enough to link to a reputable document that directly and fully contradicts what he is trying to say, or
2. DraganO intended the wikipedia reference to act as a "With a few minor changes" clause in his message, but messed up the wording and it did not come out right.

Out of respect for my fellow forum members, I will choose the explanation that assumes the least stupidity on their parts—DraganO may feel free to prove me wrong [smile]

##### Share on other sites
Oh, come on guys...
Okay, I admit that my choice of words was a bit inauspicious.
What I meant to say is that a valid C program is also (at least most of the time) a valid C program, since the OP was about if it is possible to read and understand C code with C++ knowlege, which most certainly is the case.
I did add the link to point out that there are differences, but they are irrelevant if he doesn't want to compile C code as C++ code, but only read it.

Quote:
 Out of respect for my fellow forum members, I will choose the explanation that assumes the least stupidity on their parts—DraganO may feel free to prove me wrong

Actually I am quite known to behave stupid frequently, so you won't have to wait long for proof.^^

Parkinson's Law of Triviality

##### Share on other sites
Quote:
 Original post by DraganOOh, come on guys...Okay, I admit that my choice of words was a bit inauspicious.What I meant to say is that a valid C program is also (at least most of the time) a valid C program

But, but, a valid C program is always a valid C program *ducks and covers*

##### Share on other sites
Quote:
Original post by phresnel
Quote:
 Original post by DraganOOh, come on guys...Okay, I admit that my choice of words was a bit inauspicious.What I meant to say is that a valid C program is also (at least most of the time) a valid C program

But, but, a valid C program is always a valid C program *ducks and covers*

*throws shoe and misses*
So, for the record: a valid C program is most of the time a valid C++ program

Well at least ToohrVyk got his proof now.^^

##### Share on other sites
Quote:
 Original post by ToohrVykThe essence of his post was "With a few minor changes, every valid C program is also a valid C++ program". Can you think of any major changes that would need to be applied to a C89 program (small syntax changes, and variable renaming, are minor) for it to be compiled by a standard C++ compiler?

That is a fair enough assertion, although I would point out that C89 is no longer standard C as it has been superseded by the C99 standard (and relevant amendments since then).

##### Share on other sites
Quote:
Original post by Cromulent
Quote:
 Original post by ToohrVykThe essence of his post was "With a few minor changes, every valid C program is also a valid C++ program". Can you think of any major changes that would need to be applied to a C89 program (small syntax changes, and variable renaming, are minor) for it to be compiled by a standard C++ compiler?

That is a fair enough assertion, although I would point out that C89 is no longer standard C as it has been superseded by the C99 standard (and relevant amendments since then).

I am not sure whether there is a standard which specifies which of the standards is The Standard, quasi a standard standard. A valid C89 program is and always will be a valid C89 program. A valid C++2003 program will always be a valid C++2003 program, and it's not like it will magically become invalid by the rise of a new standard.

##### Share on other sites
Quote:
Original post by phresnel
Quote:
Original post by Cromulent
Quote:
 Original post by ToohrVykThe essence of his post was "With a few minor changes, every valid C program is also a valid C++ program". Can you think of any major changes that would need to be applied to a C89 program (small syntax changes, and variable renaming, are minor) for it to be compiled by a standard C++ compiler?

That is a fair enough assertion, although I would point out that C89 is no longer standard C as it has been superseded by the C99 standard (and relevant amendments since then).

I am not sure whether there is a standard which specifies which of the standards is The Standard, quasi a standard standard. A valid C89 program is and always will be a valid C89 program. A valid C++2003 program will always be a valid C++2003 program, and it's not like it will magically become invalid by the rise of a new standard.

By definition there can only ever be one standard, otherwise it wouldn't be a standard would it?

##### Share on other sites
Quote:
 Original post by CromulentThat is a fair enough assertion, although I would point out that C89 is no longer standard C as it has been superseded by the C99 standard (and relevant amendments since then).
Certainly—there is currently a C89 standard, a C99 standard, and the expression "standard C" refers to the C99 version by default (and a standard C89 program that does not respect C99 requirements cannot claim to be a standard C program anymore).

On the other hand, this discussion is not so much about standard C as it is about the version of the C language used in the "Game Programming all in one" book—which, from my recollections and the 2002 publishing date, seems to be C89.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by CromulentThat is a fair enough assertion, although I would point out that C89 is no longer standard C as it has been superseded by the C99 standard (and relevant amendments since then).
Certainly—there is currently a C89 standard, a C99 standard, and the expression "standard C" refers to the C99 version by default (and a standard C89 program that does not respect C99 requirements cannot claim to be a standard C program anymore).

On the other hand, this discussion is not so much about standard C as it is about the version of the C language used in the "Game Programming all in one" book—which, from my recollections and the 2002 publishing date, seems to be C89.

Is this is the J Harbour book? If so, yeah he uses outdated and convoluted C code throughout his books. In the second edition of the book he does make a partial attempt to migrate the code to C++ so you might want to check out that copy if you get lost.
In my experience and from what I've seen of others it seems easier to pick up C after C++ than the other way around so it shouldn't be too hard.
I have a C programmer friend right now trying to learn objective-c which is sort of like Apple tacking on the parts of C++ on of C that they like + some Smalltalk and he's totally lost since accessor,setter,getter,inheritance,objects, etc is all lost on him.