L34rn teh C++ Lesson 1

Cunbelin

Newbie
Joined
Aug 3, 2003
Messages
281
Reaction score
0
Alright I give up, you are all so needy to learn C++ so I and hopefully others here will try and teach you. The plan is I'll try and post once per day and througout the day try and answer questions. I have a few things to say before we get started

First, I'm not a C++ god, I will make mistakes, and I'm perfectly happy to take corrections written in English, and in a kind tone.

Second, I will do my best to answer questions but I'm counting on other people here to help me out as well.

Third, get a book, it is all well and good to follow along here, and I hope I can pass some knowledge to you, but you are going to want to have something solid to flick through to remind you of library functions, and unusual syntax.

I think that's it though I do reserve the right to add more things to this list as we go on.

Lesson 1
OMGWTF HOW DU I MAKEZ GAMEZ!!!!

Ok I am going to assume you have a compiler of some sort, and have a readme to go with your compiler that tells you how to use it. I am also going to assume you know the difference between source code(what you will be writing), and machine code(the application that is run), and that is about it, so for you with more experience you might want to check back in in a few days.Alright enough assumtions lets talk structure.

C++ has a pretty simple structure, heres a quick walkthrough of what our first few programs will look like:

Precompile Directives
this means these things get done before the compiler gets to your code.
Declarations
Strictly speaking this isn't nesscary in C++, but it just makes good sense to declare things at the top.
main
For now all our programs are going to have a main loop here
Defintions
This is where you put your define what you declared

And that is it, not alot to it, we'll start expanding our structure as we go along but for now lets focus on Precompile Directives. Most of what goes here is "#include" commands, these commands are used to include other files with our main file, for now we are going to just include libraries.

Heres an approximation of what code will look like:

precompile directives

declarations
main
{
instructions
}
definitions

note that I added the two curly braces {}, and "instructions" in them, instructions is a generic term for anything that is not a declaration, a precompile directive, or a definition. Instructions are almost always surrounded by curly braces, and they either are followed by a semi-colon (;) or another pair of curly braces {}. Instructions are the heart of programming, basically they work like instructions in the real world, like this:

main ()
{
read_on;
}

The instructions you send to a computer work the same way, they just have to be in the computer's memory. To add instructions to it's memory we either have to write them ourselves, or we can load a library, to do that we use the "#include <library.h>" preprocessor directive. Now C++ has several different libraries ready to use, for our purposes we will be using the iostream library, lets do a little code.
PHP:
//These double slash marks allow me to add comments that are ignored by the compiler
//get in the habit of using them, for now use them on every line of code
//put in questions, and information about each line. remember though as soon as you hit
//return you have to put another set of slashes to comment again

#include <iostream.h> //here we include the iostream library, library files use the .h file extension

int main() //for now it is enough to know that this marks the starting point of your program
{
	cout << "piss off"; //here you can see we have an instruction, we'll be using this quite a bit
	return 0; //here we have a special instruction, this one ends our main function	
}

Alright well the above code is a basic program, before I sign off for the night, I want to talk a little about the cout line, basically what the instruction does is print whatever is in the quotes, go ahead and try altering it.

We've covered basic structure of the program, talked about the #include directive, touched on the main function, and introduced an instruction, not bad for a first lesson, I know I know, half of you have probably done this, but you have to learn structure, tomorrow I promise something more fun.
 
libary files ue .lib extensions, .h's are header files

(also this is never going to work)
 
ya .h is for header files hes calling the header file for the input output stream that definds cout and cin and also loads the code needed for them both.

That above program will work 100% I dunno if the text "piss off" is very helpfu but it is a type of hello world program.
 
Heh, yeah guess "piss off" is going to stay, it seemed a good alternative to the cliched hello world at 3AM. Anyway the .lib, seems a bit advanced at this point, though I do see your point. Heh lets call it an experiment, if it works cool, if not meh, whatever.
 
this may be doomed to fail but it's far more interesting to read than "OMG THERE'S A PIXEL OUT OF PLACE IN BUGBAIT-VALVE SUXORZ!"
keep it up.
 
Originally posted by hLABS]tryptefan
this may be doomed to fail but it's far more interesting to read than "OMG THERE'S A PIXEL OUT OF PLACE IN BUGBAIT-VALVE SUXORZ!"
keep it up.

Oh dear god...cant you change your sign? its WAY to big!
 
my point was that you called iostream.h a libary which is completly wrong, also the code does work, i was referring to the whole idea of teaching people c++ over a forum super fast isnt going to help them one bit
 
i kind of like the weird idea that my first program was the same one that millions other used
 
..i was referring to the whole idea of teaching people c++ over a forum super fast isnt going to help them one bit

i've learned a lot of valuable information from forums.
i agree noone's going to get a comp sci degree from this thread, but i do think it can be valuable to some extent.
 
No you are right nobody will learn merely by reading something, but I hope that being here to answer questions as well will help, by the way is there anyone who might be up to writing a lesson 0 on how to setup compilers ? I can probably write a MVC one from memory but I'm not sure I am going to be 100% right on it. And to be honest I don't have the time to learn how to work with the free version of Borland, or figure out how to install cygwin to get the gcc up an running, at least at this point. Oh and I corrected the thing about the library in the new lesson, thanks for that.
 
I'm going to stick with my multitude of books for now :)

my point was that you called iostream.h a libary

yeah, I thought that too.... I was like... erm , surely thats wrong :P (but then again, I wouldn't comment, since I'm still a programming newbie: I've only make 5 programs [need to do 10 to be a proper programmer] :) )

But, yeah if this is used, sure keep it up :D
 
if u uploaded these tutorials somewhere where we could read them and not get slowly lost in the forums it would be much easier
 
If you wanna make them into text or host a site on them let me know. I got plenty of web space and id let you host on my server.
 
Back
Top