Teen Programmers Unite  
 

 

Return to forum top

C++ Where do I start?

Posted by have_dinner [send private reply] at June 26, 2001, 07:05:03 PM

I'm sort of new to programming. Well, actual programming i'm pretty much a virgin, i haven't really programmed in my life. But, I have been scripting JavaScript for a while now, and I am getting a broader knowledge of CSS. But i would like to know where i could start with C++, should i just go and get one of those "C++ for Dummies" books? Please give me some guidance. Thanks :) Sam

Posted by SchizoChimp [send private reply] at June 27, 2001, 12:47:37 AM

You should join the military. They teach you to code by yelling at you constantly..

"DID I TELL YOU TO PRESS COMPILE?! WHO TOLD YOU TO PRESS MY COMPILE BUTTON? NO, NO I DID NOT! YOU WASTED VALUEABLE PROCESSOR CYCLES COMPILING USELESS CRAP THAT SEGFAULTS! YOU ARE A DISGRACE TO YOUR MOMMA AND YOUR UNCLE SAM! NOW DROP DOWN AND GIVE ME 100 "HELLO WORLDS" USING YOUR ASS AS STD::OUT"

"SIR YES SIR!"

Either that or you could get a book.... but then wheres the satisfaction of being yelled at?

Posted by Setherd123 [send private reply] at June 27, 2001, 03:49:57 AM

<giggles alot>

Okay. That *was* funny.

Posted by infryq [send private reply] at June 27, 2001, 04:39:12 PM

:) sounds like the guy that tried to teach us VB last year.

Anyhow, have_dinner, interesting s/n, but did you *really* want to call yourself a dummy? Personally, I don't tend to buy books that insult me. If you want a good book, for pretty much anything, the O'Reilly series are wonderful. If they don't have a C++ book, somebody should go and kick them so they'll print one.

If you know javaScript, you *ought* to know the OO vocabulary('method' 'object' and such) but C++ actually requires you to know how they work. If you're not going to use C++ for OOP, you might as well just learn C and get on with it... check out the "which programming language is easier?" thread for other options in learning how to program. I'm sure seth is itching to recommend python.

But you asked the best way to start. I think you've got three options. 1) get a book, read through it until you get bored, start programming, and learn from your mistakes. 2)find an online tutorial, follow the instructions, and start programming. 3)find someone in your area ie a friend/acquaintence/total stranger sans psychosis who knows C++, and ask if they'll teach you. I like #1. Some like the .. versatility? randomness? of #2. #3 takes forever because it's unlikely your friend'll hold sessions at 4AM like you can by yourself with a book...on the other hand, you don't have to spend all your time searching for the answers to your questions, you just ask. And there's the added bonus that your teacher'll get something out of it too, because in teaching something it's really difficult to *not* understand it better once you've finished.

Oh hey, and if nobody here has seen this site yet, www.codewarrioru.com holds free online courses in C++ and Java, proctored, with boards for student discussion and questions. It's pretty nifty.

So Sam, pick your poison, and good luck.
-katie

Posted by have_dinner [send private reply] at June 27, 2001, 06:52:15 PM

thanks katie.. i'll go check out that stuff... i don't reckon i'm goign to find anyone round my area who'll teach me, and really, i wouldn't be allowed to go out and learn, my mum would say, "you're not going out again are you?" boy that's annoying.. so i'll get a book and browse the online tutorials... thanks a bunch!!! :) bye

Sam

btw, my name "have_diner" looks better when put with "@hotmail.com" on the end... so "have_dinner@hotmail.com" nice and catchy... anyway, i'll catch you guys soon!! bye

Posted by have_dinner [send private reply] at June 27, 2001, 06:54:49 PM

argh, i have another question.. sorry guys... just wondering... is C++ just an enhanced version of C??? like, say i learn C, would i understand C++ heaps easier?

-Sam
sorry bout that! :P

Posted by Psion [send private reply] at June 27, 2001, 07:54:19 PM

It is essentially an enhanced version of C. There are minor incompatibilities, but learning C can be considered a first step at learning C++.

Posted by RedX [send private reply] at June 28, 2001, 03:11:55 PM

C++ is C with some OO (object oriented) stuff added. C++ can be used as a better C or as a OO language (or a combination of both).
One of the good things about C++ is the new and delete keywords. a lot more fun to use than malloc() and free().

I you learn C you'll know most of the keywords and language fundamentals of C++.
However you would also wast time learning the I/O functions of C: while those can be used in C++, C++ has better functions for handling I/O.
Better skip C and get a book about C++.

RedX

Posted by TheTutor [send private reply] at July 17, 2001, 11:21:39 PM

Hey have_dinner --

If you want to learn C++ from the ground up -- Look no further than www.GameTutorials.com :~)

Posted by TheTutor [send private reply] at July 17, 2001, 11:25:33 PM

As to RedX comment --

C++ actually encompass all of C -- C++ is an extension of C, everything you can do in C you can do in C++ -- Lately, there's been a trend of most things moving to C++ (PSII for instance) -- The GameCube uses a C compiler and the X-Box is based on D3D (Direct 3D) which of course can be written in conjunction with C/C++

Posted by lordaerom [send private reply] at July 18, 2001, 06:56:09 PM

Using the phrase 'can do' when talking about programming languages is tricky. I 'can do' everything in SmallTalk that I 'can do' in C or Scheme. IIRC, and I think I do, there is a small amount of valid C that is not valid C++, due to more stringent requirements for casting, or somesuch. So while the basic functionality is there, not every C program is a valid C++ program, at least, as I recall. </pedantic>

Posted by Psion [send private reply] at July 18, 2001, 07:01:08 PM

I add credence to your statement by my agreement now presented.

Posted by gian [send private reply] at July 20, 2001, 01:45:19 AM

Oooooh!
* Gian bows to Psion's might

Posted by Sky36 [send private reply] at August 06, 2001, 03:02:15 PM

No offense to those who say to go all with Web tutorials but hey you know how annoying it can get to read tutorials off of a computer screen for hours. Books are the way to go. Use some online though actually use a lot but you need a good book to start with. Heck if you can find someone to teach you I bet thats even better. I never had the privelege of a teacher. Oh and with books you dont have to keep switching windows unless you print or have multiple monitors or something. As you can see I like books. Well Im going to stop rambling about how much I like books.

Posted by gian [send private reply] at August 06, 2001, 05:11:07 PM

Indeed, that is the way with most electronic material, and that is one reason why e-books have not been a raging success.

But, when you spend 9 hours infront of a computer, and your eyes are permanentely focused 30cm infront of you, it really doesn't bother you that much.

Posted by have_dinner [send private reply] at August 07, 2001, 03:45:35 AM

I'm the guy who actually asked the question right... well, the way i have found... i hate the freakin web tutorials... first of all, you can't take em to work, or use them on seperate PCs without d/ling them again.. books!!! they are the best.. you can highlight passages you like, put flags on the pages you need to refer to, everything... BOOKS MAN

Posted by RedX [send private reply] at August 10, 2001, 01:39:54 PM

Books take up too much shelf-space. But they can be compressed (if your strong enough).

Books are a required evil. They tend to be expensive (those worth reading), getting them requires getting of your ass and walk to a store (or at least to your mailbox (the real-life version) ).
But reading them is more fun than reading the same thing from a screen.
Plus they can be used to trow at annoying people (the real-life people, those without a ignore-button) and are very handy to eliminate insects.

RedX

Posted by TheTutor [send private reply] at August 12, 2001, 04:39:35 PM

lordaerom --

Actually, every C program IS A VALID C++ programming -- That's not to say you might have to typecast something differently in the C++ version to get it to compile, but most definitely EVERYTHING that you can do in C can be done with C++

Posted by TheTutor [send private reply] at August 12, 2001, 04:52:07 PM

As for books -- I have never bought a book in my life that I actually used -- Programming tutorials are far superior (these would be source code and "project files" that contain a LINE by LINE explanation of everything that is happening in the source code) -- The ONLY way you're become a better programming, is by programming -- Reading about bicuspids and incisors doesn't make you a dentist -- It you want a great cheap resource with over 100 tutorials (it's portable cause it's on a CD) you can buy one at www.GameTutorials.com --

Posted by gian [send private reply] at August 12, 2001, 11:16:07 PM

I can't see why that is any different from a book... they often have step by step code explinations. I think the main advantage of web tutorials is that thay are often much more specialized than most books, as books have to be accpeted by a publisher, web tutorials do not.

Posted by lordaerom [send private reply] at August 12, 2001, 11:37:53 PM

And every valid C program is not a valid C++ program, to beat the dead horse!

Posted by TheTutor [send private reply] at August 12, 2001, 11:45:44 PM

I not even talking about a "web tutorial" per say -- Learning programming really needs to be an interactive experience -- That's why I'm saying if you have some source code in front of you that prints out "Hello World", you can look at the code, then look at what the code produces (by running the .exe) -- Then you can start modifying the code AND instantly see the results, this is where the real learning occurs :)

Posted by TheTutor [send private reply] at August 12, 2001, 11:50:55 PM

I think you're mixed up -- Every C++ program is not a valid C program, but every C++ program HAS TO BE a valid C program, C++ is just an extension of the C language -- For instance I can use printf() or cout in C++ -- In C, I can only use printf()

Posted by TheTutor [send private reply] at August 13, 2001, 12:01:54 AM

Just to clear up the C/C++ thing -- It is true if you have a program, with a .c extension and you change it to .cpp, it could NOT compile -- But everything you can do in C, you CAN do with C++ just might need a syntax tweak

Posted by lordaerom [send private reply] at August 13, 2001, 12:07:06 AM

... heh.
Everything I 'can do' with C++ I can do with Scheme.
Saying 'every c program is a valid C++ program' means every valid c source file is also a valid c++ source file. And that is not the case.

'Every C++ program is not a valid C program, but every C++ program HAS TO BE a valid C program, C++ is just an extension of the C language'

Contradict yourself much? =] But I get what you mean!

Posted by TheTutor [send private reply] at August 13, 2001, 12:18:36 AM

Not at all :) -- C++ encompasses C -- Meaning EVERY valid piece of syntax in C is valid in C++ -- However, vice versa can not be said -- Reminds me of the time I told somebody that the ONLY difference between an class and a struct in C++ is that one is default private (the class) and one is default public (the struct) -- And he swore up and down that polymorphism was only a class thingý He had a lot to learný :)

Posted by Cobbs [send private reply] at August 13, 2001, 12:26:29 AM

Reading web tutorials doesn't make you a dentist either! :p

Posted by lordaerom [send private reply] at August 13, 2001, 12:43:00 AM

... nope.

#include <stdlib.h>

int main()
{
char *p = malloc(23);
return 0;
}

bash-2.05$ g++ invalidcpp.c -ansi -o invalidcpp
invalidcpp.c: In function `int main()':
invalidcpp.c:5: ANSI C++ forbids implicit conversion from `void *' in initialization
bash-2.05$ gcc invalidcpp.c -ansi -o invalidcpp
bash-2.05$

Posted by TheTutor [send private reply] at August 13, 2001, 07:44:25 AM

You don't read too well do ya lordaerom? -- First off even the same .c file may compile on one compiler and not another --

I said in an eariler post you may have to "tweak" the syntax -- In this example just changes this line:

char *p = malloc(23);

to:

char *p = (char)malloc(23);

Everything works GREAT!

Don't worry we won't change our opinion of you cause you were wrong :)

Posted by TheTutor [send private reply] at August 13, 2001, 01:54:26 PM

Since this site is about learning, I want to point out that lordaerom code sample is an example of VERY POOR CODING -- When you are using malloc for memory allocation you should ALWAYS typecast to whatever memory type you are attempting to make. Why? It's for the simple reason of readability and compatibility to .cpp files -- If a .c file is coded well, you should be able to change the extension to .cpp and have NO difference when compiling -- So if you want to make an array of 23 chars, below is an example of how it could be done clearly and efficiently -- This is very clear as to your intent -- and yes sizeof(char) == 1 so it's unnecessary, but a good compiler will cut that out so in the .exe a multiplication won't be executed --

char *p = (char*)malloc(23 * sizeof(char));

Also his program leaks memory -- You need to do this before you terminate the program:

free(p);

So the program, coded well, should look something like this:

#include <stdlib.h>

int main()
{
char *p = (char*)malloc(23 * sizeof(char));

// If we couldn't malloc the memory
if(!p)
return EXIT_FAILURE; // Program encountered an error

free(p);
return EXIT_SUCCESS; // Program was successful

} // end of main()

Posted by lordaerom [send private reply] at August 13, 2001, 01:45:10 PM

The example was purely to demonstrate something that compiles as ANSI C and not as ANSI C++. This proves that every valid C program is not a valid C++ program, as you had said. It is that simple.

'C++ encompasses C -- Meaning EVERY valid piece of syntax in C is valid in C++'

That is simply not true. If you have to 'tweak' the code, then it is no longer the same.
-------------
In fact:
http://david.tribble.com/text/cdiffs.htm
'Incompatibilities Between ISO C and ISO C++'

Posted by TheTutor [send private reply] at August 13, 2001, 01:47:43 PM

Nope that is simply not true -- The following two lines of code are the same:

char *p = (char*)malloc(10 * sizeof(char));

char p[10];

They are NOT verbatim in their syntax -- But they are the same -- both give you an array of 10 chars

Posted by lordaerom [send private reply] at August 13, 2001, 02:04:45 PM

No, actually they're quite different. They do completely different things. Then, there is how they are not the same sequence of bytes. If those are the same, then are these the same?

printf("Hello, World!\n");

and

System.out.println("Hello, World!");?

After all, they both seem to do the same thing, and that's the only standard by which we judge, right? Even though for the most part C++ and C are compatible, there are differences. Those two lines are not the same code, as anyone could tell you. You might be able to say they have the same result, but even there you would be wrong, because one results in a function call, etc. Really, they're very different. Net Result perhaps? Even then, I dunno, char *p != char p[].

Posted by TheTutor [send private reply] at August 13, 2001, 02:11:31 PM

I suppose "2 + 2" is not the same as "4" (You're not thinking high level enough, obviously your a C programmer :) -- Bottom line is this -- Every .c file CAN be programmed in a way that you can change it's extension to .cpp and you CAN recompile it WITH NO ERRORS OR WARNINGS!

Posted by lordaerom [send private reply] at August 13, 2001, 02:55:32 PM

Well hey, that's not at all what you've said before. It is possible, but there is no relationship between C and C++ that indicates all C source is valid as C++ source. The simple fact is that it isn't. While it is possible to write C as valid C++, it's not the case that every valid c program is also a valid c++ program.

And 2 + 2 is the same as 4, if you only consider the result. But say for some wacky, braindead reason the C++ standards people decided that 2 + 2 was no longer legal, that allexpressions that can be evaluated to a single number at compile time must be done so in source, by the programmer, whereas C would've allowed it.

So then we have
int a = 2 + 2; //Valid C, invalid C++.
int a = 4; //Valid C, valid C++.

This doesn't make the sources the same, because they do the same thing. They're quite obviously different.

Posted by TheTutor [send private reply] at August 13, 2001, 03:14:43 PM

Again let me put in as straight forward English as I can: Do not add your interpretation to the words I use, use the English language's interpretation :)

If a function can be used in C ( example: malloc() ) -- That function can also be used in C++

If an atomic data type exists in C ( example: float ) -- It also exists in C++

And about your "if they change C++ standards" -- They CAN'T do something like that because indeed C and C++ are VERY MUCH SO LINKED -- You're way to hung up on this standards thing --

And like I said before a valid C file on one compiler MAY be invalid on another compiler -- You're taken very specific cases and completely over generalizing 'em

But no matter -- I just hope that any newbie who may read this learns something :)

Posted by Cobbs [send private reply] at August 13, 2001, 04:09:33 PM

Nope that is simply not true -- The following two lines of code are the same:

char *p = (char*)malloc(10 * sizeof(char));

char p[10];

They are NOT verbatim in their syntax -- But they are the same -- both give you an array of 10 chars


Actually... the first way you listed declares the data to be on the heap while the second declares it to go onto the stack, since they both do one thing in commom (gives you an array of 10 chars, like you said) it doesn't make them the same.

Posted by TheTutor [send private reply] at August 13, 2001, 04:41:26 PM

"the same" means: For all intensive purposes they are "identical" -- Of course they not 100% identically/verbatim/equivalent -- If they were, they'd be no need for different syntax ý Iým talking HIGH LEVEL (conceptually) here people :)

Posted by lordaerom [send private reply] at August 13, 2001, 05:04:22 PM

No, you're talking wrong.
These standards I'm hung up on are the things that define the languages. Your logic, or lack thereof is silly. The same means the same. Not nearly the same. Not almost the same. Not pretty much the same. The same means the same. a is a, b is not a, etc etc. If we're talking about functionality only, then every Scheme program is also a valid C program, after all, I just need some minor tweaking of the syntax to get it to work, right?

As for 'You're taken very specific cases and completely over generalizing 'em', sure it's a specific case, the implicit initialisation from void* to another type, but it's all that was required to prove your statements wrong.

Can we review and see if you think everything you've said is correct?

Take the set of all possible C programs that compile correctly as C programs.
Try and compile them as C++ programs. Some of them will fail, as I have demonstrated above. So every C program is not a valid C++ program. You are wrong. Deal with it. Saying you're talking 'high level' doesn't make the false true.

'C++ encompasses C -- Meaning EVERY valid piece of syntax in C is valid in C++'. Wrong. Thanks for playing!

Posted by CHollman82 [send private reply] at October 01, 2001, 09:14:40 PM

Posted by TheTutor [send private reply] at August 13, 2001, 05:30:59 PM

Nope actually I win -- The fact that you are so obviously disturbed by my posts means you know deep down inside that I'm correct -- But you will learn not to be so prideful when you mature --

The dictionary defines syntax as:
The pattern of formation of sentences or phrases in a language

Therefore without a doubt every piece of syntax in C is valid in C++ (which means since I can use the function printf() in C (notice how I didn't put any parameters just said you CAN use printf() in C) you know with 100% certainty that you CAN use printf() in C++)

By your trying to over complicate this simple matter, youýre doing a disservice for anyone coming to this site hoping to learn something ý

And like Forest Gump said, "That's all I got to say 'bout that." (or something the same as that :)

Posted by Psion [send private reply] at August 13, 2001, 05:41:19 PM

I just noticed the commotion here, and just want to add my usual point that there are minor differences that prevent all C code from being valid C++ code, but it looks like that doesn't intefere with TheTutor's point.

Posted by lordaerom [send private reply] at August 13, 2001, 05:54:31 PM

... OK.
So, now, I will refer to you my previous post and show you a piece of C syntax that is invalid as C++, thus proving you wrong!

---Source!---
#include <stdlib.h>
int main()
{
char *p = malloc(23);
free(p); //Let us be a good citizen!
return 0;
}

---Compilation!---
As C++, denoted by -xc++
bash-2.05$ gcc -xc++ invalidcpp.c -ansi -Wall -o invalidcpp
invalidcpp.c: In function `int main()':
invalidcpp.c:5: ANSI C++ forbids implicit conversion from `void *' in initialization
invalidcpp.c:5: warning: unused variable `char * p'

As c, denoted by -xc
bash-2.05$ gcc -xc invalidcpp.c -ansi -Wall -o invalidcpp
invalidcpp.c: In function `main':
invalidcpp.c:5: warning: unused variable `p'


This is a cut and dry issue. C has a very specific set of syntactical requirements, as does C++. Certain things C allows as syntactically correct, however, C++ does not. Therefore, every valid C program is not a valid C++ program.

Syntax is very precise. In the context of C, an implicit conversion from void* is syntactically OK. In the context of C++, however, such a conversion is not.

This isn't a complicated matter. Test it out in your own compiler, I've tried it both with GCC and C++ Builder, both yielding the same results. I'm not doing anyone a disservice here.

So, if you do reply, explain to me how every 'piece of syntax' in C is also valid in C++, when I have just given an example of something that is valid in C and not in C++. And, please not that if you add a cast to it, it is no longer the same code. Thanks!

Posted by TheTutor [send private reply] at August 13, 2001, 09:22:37 PM

Okay one last timeý these are pieces of syntax in C -- They come together to make a function declaration --

void* malloc( size_t );

This is a line of code in C. It is made up of multiple pieces of C syntax

char *p = (char*)malloc(10 * sizeof(char));

See the difference?

I think Psion pretty much summed it up in his post :)

Posted by lordaerom [send private reply] at August 13, 2001, 06:19:10 PM

Of course the minor differences interfere with what he said. He made a blanket statement that all valid C code was valid C++ code, and that's incorrect. He has in no way rephrased, or retracted what he's said, so I'll just have to go on believing he thinks it.

His point is that you can do the same stuff. Yipee. I can do the same stuff in Java. That doesn't make Java source compatible with C.

Can I do everything in C++, that I can do in C, on a conceptual level? Yes.
Can I use the same syntax to accomplish it? Not necessarily.

And, as for those two lines, they're both lines of code, I don't understand what you mean, by one being a piece of syntax and one being a line of code. Unless you mean '--' as the 'piece of syntax'.

Posted by have_dinner [send private reply] at August 14, 2001, 06:54:33 AM

guys.. i withdraw my question about what the differences between c and c++ are... you guys seem to contradict each toher too much.. now hush hush guys

Posted by henrik [send private reply] at October 23, 2001, 10:34:48 AM

There are subtle incompatabilities between c and c++ that extend beyond silly compiler warnings, however you're not very likely to encounter them. In practice, pretty much every c program is also a c++ program. However, the following snippets have different meanings in c and c++:

Example 1:
-------------
{
int class; char virtual; /* valid c, not valid c++ */
}

Example 2:
-------------
main()
{
double foo = sqrt(2); /* poor c, not c++ */
}
Example 3:
-------------
{
enum foo {bar,baz};
foo d = 1; /* valid c, invalid c++ */
}

There are examples of code that compile in both languages but have different meaning, such as the following:

Example 4:
-------------
int foo[10];
{
struct foo { int bar; };
sizeof(foo) /* size of the array in c, size of the struct in c++ */
}

Example 5:
-------------
{
int foo = 4//* */2; /* foo is two in strict c, four in c++ */
;
}

So if you want to convince someone that c and c++ are nothing like each other, here's some ammo to misuse. :)
-henrik

Posted by gian [send private reply] at October 23, 2001, 10:35:43 PM

Hi, Iwanttobea Dr. Henrik! Everyone say hi to Henrik!

Posted by have_dinner [send private reply] at October 24, 2001, 12:28:10 AM

how come someone would reply to a post that was 1 month and 10 days old?

Posted by henrik [send private reply] at October 24, 2001, 01:12:17 AM

Public service announcement :)

Acctually i didn't notice it was so old until i had already typed the reply. I've been away too long. lordaerom and TheTutor were both right (or wrong) and i thought i'd be nice to post some snippets that illustrated it.

But if you're still interested in your original question: C++ is a horrible first language, but if you decide to use it, the C subset is probably a good start. Of course, as you move on to more advanced c++ you'll have to unlearn some of that, but it's still less complex than learning the more advanced concepts directly.

For a beginner i'd suggest java or python or maybe pascal. The borland dos compilers make excellent learning tools. I'd suggest you find a copy of either turbo pascal or turbo c++.

-henrik

Posted by have_dinner [send private reply] at October 24, 2001, 07:36:52 AM

yeah, i kinda asked that question back in june :P... but well, i dunno, i started work not long before that... so i haven't really had much time to take on any language... if there is one, it's perl

Posted by henrik [send private reply] at October 24, 2001, 02:17:07 PM

Better late then never.. or something like that. psion, whouldn't it be a good idea to sort posts by date?

though perl is mostly good for quick and dirty hacks, understand that and prosper.

-henrik

Posted by Psion [send private reply] at October 25, 2001, 06:18:01 AM

henrik, they are sorted by date of original post in the thread, but only those with the last post in your selected time interval appear.

Posted by henrik [send private reply] at October 26, 2001, 06:40:25 AM

yep (noticed that after i posted), but sorting by the lastest post makes more sense - it seems to be what most messageboards use, and makes it easier to see what's current. Old threads noone posts to will eventually migrate down, and new and active ones will stay on top.

As it should be imho. How about we give it a try?

-henrik

Posted by Cobbs [send private reply] at October 26, 2001, 04:38:05 PM

New ones at the top!
It'll stop new people from posting in old threads, which seems to get some people excited (on the angry side, but not angry, excited!)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.