Page 1 of 1
The Difference Between C/++?
Posted: Sun Nov 23, 2008 12:27 am
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
Re: The Difference Between C/++?
Posted: Tue Dec 02, 2008 9:24 pm
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.
Re: The Difference Between C/++?
Posted: Wed Dec 03, 2008 7:11 pm
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
Re: The Difference Between C/++?
Posted: Wed Dec 03, 2008 7:14 pm
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.
Re: The Difference Between C/++?
Posted: Wed Dec 03, 2008 7:23 pm
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.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 7:59 am
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.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 9:51 am
by avansc
im still not sure what you are refering to. sorry.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 10:02 am
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.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 10:08 am
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.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 11:44 am
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.)
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 8:37 pm
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.
Re: The Difference Between C/++?
Posted: Thu Dec 04, 2008 10:30 pm
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