Sign in to follow this  
Peter Stephenson

C vs C++

Recommended Posts

Ive been programming for a few years now and consider myself to be a reasonably strong intermediate programmer. I am trying to teach myself game development (with the help of a few books) and have a question that I would like answering if possible (I apoligise if this has been asked b4 i usually search forums but the search is down) Which is better to use? the procedural and structure based approach of C or the Object Oriented development of C++ im concerned with both efficiency of the method of programming and with the ammount it is used in the industry as well as any other important factors Any help with this would be appriciated Thanks Peter

Share this post


Link to post
Share on other sites
I think it all ultimately comes down to your own preference.
Play with both and you will find what you are most comfortable with.

I personally prefer C++ (started with C then moved on to C++) with the OO approach but there are benefits in both camps...

All I can really say is try both and see how you go.

Share this post


Link to post
Share on other sites
To be honest I'm not sure
I think that after its all been compiled it has a negligable difference. I know the time and true saying that gets dismissed is what follows but its true. On todays systems the difference is not going to matter unless you are making software for embeded (sp?) systems.

Share this post


Link to post
Share on other sites
Quote:
Original post by Peter Stephenson
does using inheritance have an impact on performance? (i know the use of virtual functions does but other than that?)

Inheritance doesn't impact performance itself, virtual functions which make an important part of inheritance does however theoretically do this.

Know what kind of code your compiler generates before starting to talk about performance issues on this kind of level. The little extra time it takes to do two lookups and a indirect call is irrelevant for a virtual function call is in 99% of the time irrelevant.

Using the correct data structures and algoritms is the key to getting maximum performance, focus on that instead.

Share this post


Link to post
Share on other sites
To be brutally honest: if you have to ask this question, you're not an intermediate programmer. Well, you might be an intermediate programmer, but you're still a beginner software engineer.

Languages are tools; each one has situations in which it excels, and situations in which it falls apart. Your question is akin to "Which is better, a wrench or a screwdriver?" It depends entirely on what you plan to use it for. ("Developing games" isn't a valid answer, because both languages are suitable for different parts of different kinds of game - to extend the metaphor, it's like "For building a house.").

C gives you very low-level control and flexibility, at the cost of ease of use. C++ gives you less control, but most people find it easier to 'think in objects.' The language isn't important - decide whether you want a purely procedural method of development, or whether you want object-oriented. Once you've made that decision, consider that C and C++ are not the only language of each type.

And no, inheritance has no effect on performance beyond your aforementioned point of virtual functions (which takes two extra instructions to call).

Share this post


Link to post
Share on other sites
1. OO is no panacea, its only suited to a set of problem domains for example it excels in GUI frameworks.

2. If you only focus on OO your being closed minded and restricting yourself, its like you only have a hammer to build everything.

3. Traditional OO is typically not enough, you will most likely find yourself using Post-OO and a mix of paradigms.

4. Saying "I prefer OO over procedural" is nonsensical as in essence OO is merely an extension of the concept, it does not try to solve the problems of procedural programming.

5. You can do OO in C.

6. C++ is not merely an OO language, its not "just C with OO" its a multi-paradigm language, natively you can do procedural, OO, generic, generative, pure functional programming (at compile-time when doing template meta-programming). With libraries you can also do some form of functional too.

7. Your worrying about the wrong kind of efficiency, choosing more efficient algorithms & data structures for the job is more important.

[Edited by - snk_kid on June 9, 2005 3:19:00 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Peter Stephenson
Which is better to use?
the procedural and structure based approach of C
or the Object Oriented development of C++

The best is obviously to have both available, so you can use what makes sense to use.

C++ is a superset of C, so in other words, use C++, and then write code using whatever approach is best suited. If you feel OO would be a waste of time for your particular problem, then write it "C-style" instead.

(And yes, you can do OO in C. You can also do functional programming in C, if you like. But languages which explicitly support are still better suited for it. In the same way, C++ is better suited for OO.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
C++ is a superset of C


As of this moment its not quite, standard C++ does not incorporate some of the additions mades to C99 like restrict pointers but i do know what your trying to get at.

Share this post


Link to post
Share on other sites
Quote:
Original post by Peter Stephenson
Which is better to use?
the procedural and structure based approach of C
or the Object Oriented development of C++


It depends on your situation. This isn't the type of question that has one "true" answer. I've found that generally OO has done a better job for me. Also note that you can use a procedural and structure based approach in C++ and mix it with OO, if you so desire.


Quote:
Does using inheritance have an impact on performance? (i know the use of virtual functions does but other than that?)


Not really. Where i've noticed improvements is the amount of time it takes me to get a project finished and the amount of bugs in my games (both of which are better for me in OO).

Share this post


Link to post
Share on other sites
I think that OO design works quite well with game development. Many of the artifacts are quite representative of real objects.

>Object
->Player
--->Player_Human
--->Player_AI
--->Player_Network

->Vehicle
--->Vehicle_Air
------>Vehicle_Air_Jet
------>Vehicle_Air_Helo
--->Vehicle_Land
------>Vehicle_Land_Car
------>Vehicle_Land_Tank

->Weapon
--->Weapon_Ranged
----->Weapon_Ranged_Rifle
----->Weapon_Ranged_Pistol
--->Weapon_Mele
----->Weapon_Mele_Knife
--->Weapon_Frag
----->Weapon_Frag_HighExplosive
----->Weapon_Frag_Smoke
----->Weapon_Frag_Flash

In other types of programming (business) objects become more difficult to define, but objects in a game are quite obvious. The OO paradigm as is it relates to games offers quite a bit in the way of organization and code reuse that isn't possible or is much more sloppy using procedural methodologies.

C++ is going to perform fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
Quote:
Original post by snk_kid
5. You can do OO in C.


How so? I never learned plain C, but I haven't really heard this before.


Sure you can, its just that there is no support in the language itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
Quote:
Original post by snk_kid
5. You can do OO in C.


How so? I never learned plain C, but I haven't really heard this before.

Also, from the little "grammar nazi" in me, "you're" means "you are" :P
Since C has structures, functions and pointers, you could do something like:

struct Example {
int someData;
};

// self would be the be used as "this".
void doSomething(struct Example* self) {
// ...
}

The C++ equivalent would look something like this:

struct Example {
int someData;

void doSomething() {
// ...
}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Quote:
Original post by load_bitmap_file
Quote:
Original post by snk_kid
5. You can do OO in C.


How so? I never learned plain C, but I haven't really heard this before.


Sure you can, its just that there is no support in the language itself.


There is not support for advanced structures that are more r less a help in OO programming (polymorphism, inheritance, and so on). OO programming is not about creating classes, it is about cutting your problem in object and messages (a message is sent by an object to another. C++ call them methods). There is no real need to reproduce a complex OO language system to use OO.

Some project tried (with some success) to reproduce the behavior of known OO language. or example, you might have a look to GTK+ and GLib.

Regards,

Share this post


Link to post
Share on other sites
Well, here's my 2 cents...

I think both C and C++ are wonderful languages. Perhaps that is because they are the only two that I know! :-)

Seriously though, the C++ performance impact is usually negligible [sp?]. The only place where virtual functions would make a noticeable impact is for objects that have many instances in your program. For example, many books claim that it is unwise to use any virtual functions in something as fundamental as a Vector/vertex class. This is because if you have several thousand polygons, the vertex count is going to shoot up pretty quickly, and those extra couple of cycles for virtual functions may make a slight impact.

Bottom line... don't really worry about C++ performance... it's still faster than Java. ;)

However, I am currrently finding out the following: If you're trying to learn 3d programming, often it is easier to just use plain C. Let me explain why. If you're just starting out, and trying to master something, often it makes sense to concentrate all of your efforts on learning the new material, as opposed to learning new stuff AND making it architecturally sound via OOP. In other words, once you learn how to do the stuff, then go ahead and go overboard with OOP and concentrate on software engineering approach (ie, design and plan of the code).

There's nothing that takes you away from learning 3D programming faster than spending the time to make all these classes with overloaded operators, and making sure that you get the design right. For example, you can do vector operations in C using a few typedef's and a few #defines, but for an OOP approach, you want to "design it right" and it ends up being a couple hundred lines of code just to do what you were doing before.

I know many people with disagree with me, but that's my opinion on it. As I am still trying to master 3D programming, I find it is much simpler to stick with C until I have a firm grasp on what's going on.. then later I'll probably take the time to plan everything out and implement in C++.

Remember, it's not reusable until it's useable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
There is not support for advanced structures that are more r less a help in OO programming (polymorphism, inheritance, and so on). OO programming is not about creating classes, it is about cutting your problem in object and messages (a message is sent by an object to another. C++ call them methods). There is no real need to reproduce a complex OO language system to use OO.


Thats not exactly what i mean't and never said its going to be easy either.

Quote:
Original post by Emmanuel Deloget
Some project tried (with some success) to reproduce the behavior of known OO language. or example, you might have a look to GTK+ and GLib.


That was the first example that came to mind.

Share this post


Link to post
Share on other sites
Quote:
Original post by morx
Bottom line... don't really worry about C++ performance... it's still faster than Java. ;)


*zips up flamesuit and slowly creeps towards the exist*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this