The Difference Between C/++?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

The Difference Between C/++?

Post by XianForce »

Well every time someone is explaining the difference between C and C++, I only hear: "C++ is object-oriented". Well I thought I'd give a small little post that explains the difference.

First off, every program starts with a "problem".

In C, these problems are broken down into functions(or methods) to solve these problems. Each function is usually quite short, and has a specific task. Structures are the tools to store your data with C. Throughout a C program, you'll build up many structures designed specifically to hold data for certain items within your programs, and your functions all throughout. This is procedural programming, everything is broken up into procedures so a problem can be more simply solved.

In C++, the structures and functions are combined into one: the object. In object-oriented programming, problems are broken down in a way objects can be made to match real life objects. You start this by using a class. a class can hold variables and methods, and the data can be private, public, or protected. With this, you could have a class Dog, then you could declare the dog Bash, and with the appropriate functions and data members you can make him bark, wag his tail, and so forth.


I hope this helped people who want to know the difference between these two languages. If I said something wrong, please tell me, or if there's something to include, also inform me.

This probably wasn't necessary but I was bored as hell xD
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The Difference Between C/++?

Post by avansc »

XianForce wrote:Well every time someone is explaining the difference between C and C++, I only hear: "C++ is object-oriented". Well I thought I'd give a small little post that explains the difference.

First off, every program starts with a "problem".

In C, these problems are broken down into functions(or methods) to solve these problems. Each function is usually quite short, and has a specific task. Structures are the tools to store your data with C. Throughout a C program, you'll build up many structures designed specifically to hold data for certain items within your programs, and your functions all throughout. This is procedural programming, everything is broken up into procedures so a problem can be more simply solved.

In C++, the structures and functions are combined into one: the object. In object-oriented programming, problems are broken down in a way objects can be made to match real life objects. You start this by using a class. a class can hold variables and methods, and the data can be private, public, or protected. With this, you could have a class Dog, then you could declare the dog Bash, and with the appropriate functions and data members you can make him bark, wag his tail, and so forth.


I hope this helped people who want to know the difference between these two languages. If I said something wrong, please tell me, or if there's something to include, also inform me.

This probably wasn't necessary but I was bored as hell xD
technically you can make structures with functions as member variables in C aswell.
and you can also make data in structure "private". and actually you can also do pseudo inheritance with C.
the only thing that C cant do exactly is polymorphism. and honestly. i have never had the need for polymorphism, infact i dont
think i have ever used it other then trying some code out of a book.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: The Difference Between C/++?

Post by XianForce »

avansc wrote:
XianForce wrote:Well every time someone is explaining the difference between C and C++, I only hear: "C++ is object-oriented". Well I thought I'd give a small little post that explains the difference.

First off, every program starts with a "problem".

In C, these problems are broken down into functions(or methods) to solve these problems. Each function is usually quite short, and has a specific task. Structures are the tools to store your data with C. Throughout a C program, you'll build up many structures designed specifically to hold data for certain items within your programs, and your functions all throughout. This is procedural programming, everything is broken up into procedures so a problem can be more simply solved.

In C++, the structures and functions are combined into one: the object. In object-oriented programming, problems are broken down in a way objects can be made to match real life objects. You start this by using a class. a class can hold variables and methods, and the data can be private, public, or protected. With this, you could have a class Dog, then you could declare the dog Bash, and with the appropriate functions and data members you can make him bark, wag his tail, and so forth.


I hope this helped people who want to know the difference between these two languages. If I said something wrong, please tell me, or if there's something to include, also inform me.

This probably wasn't necessary but I was bored as hell xD
technically you can make structures with functions as member variables in C aswell.
and you can also make data in structure "private". and actually you can also do pseudo inheritance with C.
the only thing that C cant do exactly is polymorphism. and honestly. i have never had the need for polymorphism, infact i dont
think i have ever used it other then trying some code out of a book.
ehh, well I can see how it has its uses in classes with virtual functions and such...but not too far aside from that xD
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The Difference Between C/++?

Post by avansc »

XianForce wrote:
ehh, well I can see how it has its uses in classes with virtual functions and such...but not too far aside from that xD
im not sure what you are trying to say. can you explain better. i dont know where you get virtual functions from.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: The Difference Between C/++?

Post by Falco Girgis »

avansc is saying that every problem that can be solved with an OO C++ approach can be solved with a procedural C approach. He's right.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: The Difference Between C/++?

Post by XianForce »

avansc wrote:
XianForce wrote:
ehh, well I can see how it has its uses in classes with virtual functions and such...but not too far aside from that xD
im not sure what you are trying to say. can you explain better. i dont know where you get virtual functions from.
Well I'ts like polymorphism + inheritance isn't it?

if you declare a function in a base class virtual, you can use overrided functions in the base class.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The Difference Between C/++?

Post by avansc »

im still not sure what you are refering to. sorry.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: The Difference Between C/++?

Post by MarauderIIC »

He was saying "I can see how [C++] has uses with ... virtual functions and such"

And re:
if you declare a function in a base class virtual, you can use overrided functions in the base class.
Also you can override a function without declaring it virtual, but if you do that...

Code: Select all

class A {
 do();
}
class B : public A {
 do();
}
fn (A& aobj) {
 A.do();
}
B bobj;
fn(B)
fn will call class A's do(), not class B's. Which probably has its uses. Virtual prevents this (by binding the call to do() at run-time instead of compile-time). This is why you should always declare inheritance-related destructors as virtual, or you might destruct the wrong object and not deallocate any memory that B allocated.

EDIT:
The 'binding at run-time' step involved in the virtual keyword makes your program require more memory (definite, your program keeps a table of virtual functions and looks them up when its running) and probably has a speed hit, I'm not sure if it's negligible or not.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: The Difference Between C/++?

Post by Falco Girgis »

MarauderIIC wrote:by binding the call to do() at run-time instead of compile-time
That's also going to be slower than not using polymorphism. It's the overhead for doing the OO things such as this that makes C faster than C++. Just thought that I would point this out, as it is a C vs C++ thread.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: The Difference Between C/++?

Post by avansc »

okay i see what he is saying. yeah inheritance and polymorphism is really not on my list of important things to know. especially for game programming.
its a java thing that C++ tried to compete with, but in actuality its so very rarely used. i mean how many times have you ever used inheritance. and polymorphism.
there is nothing C++ can do that C cant do better.

(we have to define better, yeah C++ can do it in less lines of code, or maybe there is easy strings and you can make classes, but in performance, and portability, and just pure beauty C is hard to beat.)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dejai
Chaos Rift Junior
Chaos Rift Junior
Posts: 207
Joined: Fri Apr 11, 2008 8:44 pm

Re: The Difference Between C/++?

Post by dejai »

I always saw C++ as more manageable than C while not being necessary more powerful. C++ was made from C so you can technically do anything in C that you can in C++ but C++ standardizes object management. Both are important for different things.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: The Difference Between C/++?

Post by MarauderIIC »

avansc wrote:i mean how many times have you ever used inheritance.
Every time I do something with players and NPCs, I have an Actor class that has the common attributes.
and polymorphism.
Only in that case, tbh :P
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply