--- Phers <
[email protected]> wrote:
> So what's the difference between C and C++?
>
> -phers
uhhh, thats not such an easy question to answer. dad would
know better than I but C and C++ are really very different.
the difference is a paradigm difference in the way you
program.
so in C you start programing and when run it just goes down
line by line and executes the commands sequencially. at some
point, tho, your program either gets too big or you start
doing the same thing again and again and its a waste of time
writing it twice and its hard to maintain cuz you have to
remember to apply all fixes to both copies of it.
so what ppl do is create "functions". these are chunks of
code that you pull out of the main list and seperate into
its own fragment. you put a name on it and there is special
syntax for defining "parameters" to pass in and out of the
function. then, inside the main code where it used to be
you just call that function name and the execution jumps to
it and then comes back to where you called it when it is
done. this allows you to concentrate on one specific task
in a function and then call it as many times as you want
from the main section. you can then share these functions
with ppl.
example: you might have a main C program that does this...
. ask user for a number, call it "A"
. ask user for another number, call it "B"
. ask user for another number, call it "C"
. add all numbers together and print the total
. add all numbers together and divide by 3, print this average
. exit
well, it might look like this...
main()
{
int A, B, C, D;
float E;
A = get_value_from_user();
B = get_value_from_user();
C = get_value_from_user();
D = A+B+C;
print "this is the sum: ", D;
E = (A+B+C)/3;
print "this is the average: ", E;
}
but lets say that you do that print the sum and
average of 3 numbers alot of times in your program.
instead of writing those last 4 lines over and over
again just pull them out and put them into 2 functions
and then expect that someone will give you 3 numbers
and you will return them the answer like this...
main()
{
int A, B, C, D;
float E;
A = get_value_from_user();
B = get_value_from_user();
C = get_value_from_user();
D = add_em(A, B, C);
print "this is the sum: ", D;
E = average_em(A, B, C);
print "this is the average: ", E;
}
function add_em(int foo1, foo2, foo3)
{
float foo4;
foo4 = foo1 + foo2 + foo3;
return foo4;
}
function average_em(int fooA, fooB, fooC)
{
float fooD;
fooD = (fooA + fooB + fooC)/3;
return fooD;
}
now... obviously that didnt save alot up in the main
code but if you're going to break something out into
its own function its going to be something that does
alot of work and has lots of code so it will be much
more adventagous. (sp?)
so, to get to your question... if you wanna do something
like keep track of someone's personal data like address
book, phone number, favorite color, and birthdate then
you create functions like add_birthdate, edit_birthdate,
add_address, edit_address, etc.. you then create a thing
called a struct which is a collection of data all in
one big ball that you give a name. so like you might have
these variables...
string name, addr_street, fav_color, addr_city, addr_state
int addr_zipcode, phone_num, addr_street_num
date birthdate
but in the above case Ive got like 9 variables to keep
track of for one person. that's alot. so if I call my
functions Im passing alot of variables. so a struct is
a C thing that gathers them all together and gives it
one name. like this...
struct a_person
{
string name, addr_street, fav_color, addr_city, addr_state
int addr_zipcode, phone_num, addr_street_num
date birthdate
}
ok, basically its cleaner to now have a struct with all your
data in it. problem is that you still have like 6 different
functions just sittin around and you have to call it with
passing so many variables like this...
add_addr(addr_street, addr_city, addr_state, addr_zipcode,
addr_street_num);
what C++ does is that it is "object-oriented" so instead of
thinking in variables and functions you now "think in" objects.
an object in C++ is grouping the data with the functions that
do stuff to that data together in one ball that is called an
object. its a way to organize stuff. so now you have that
struct but the functions are defined in that struct too (well
kinda... I think you can think about it that way).
now, instead of adding someone's address like this...
A = get_address_from_user();
add_addr(addr_street, addr_city, addr_state, addr_zipcode,
addr_street_num);
you can just call it like this...
a_person.add_address = get_address_from_user();
cuz the a_person object knows what to do when you give it
an address. it called add_addr function that is defined
inside and it knows what data ball ("struct" in C) to add
it to.
anyway, thats kinda it in a nutshell (I think). ask dad if
this is the right way to think about it.
basically C is raw and C++ is a wrapper ontop of C that has
extra keywords and syntax to setup these organizations of
data and functions together in what they call an object.
then new syntax for creating, populating, manipulating, and
deleting those objects. C++ is a superset of C - I think you
can do anything in C++ that you can do in C plus all the new
C++ stuff but not the other way around.
-dj