Post by speedyclock on Jan 24, 2011 22:52:11 GMT
C++ Learning Tutorials by Jordan Smith
Book 1
Learn about C++ and it's functions!
Hello, me and my partner Draxor are here to show you a little of C++ and how it works. In this Tutorial we are going to explain how to start coding C++ in it's own compiler and IDE. We'll also go over basic commands. And loops. After this, we are going to go into libraries a little bit. And then, we are going to finish off with a large scale lab.
Indeed, in this tutorial you are going to have to work with labs. Labs are just sections in the book where I will challenge you to make code. There are 5 labs in every book. Here are Book One's Labs.
Book 1 Labs
To be finished!
Let's go over getting your compiler. This is important. You need a compiler to compile your C++. Without it, running programs is impossible and writing them for personal benefit is pointless. I suggest DEV C++. Google it and find it to download. Here's a direct link to the one used in this tutorial.
sourceforge.net/projects/dev-cpp/files/Binaries/Dev-C%2B%2B%204.9.9.2/devcpp-4.9.9.2_setup.exe/download
After Set-up (I suggest going with Default), Start up a new project. Go to file > new > source file
Afterwards you should have a clear blank canvas of workspace. Here's some things I want you to type. On the first line. Type
All libraries are surrounded in <>s. This is needed for console applications that I will be teaching. Now edit your canvas to look like this!
This is something that might confuse you at first. Yet, I am certain you know what it does. It will open the command prompt, That black box thingie you see on computers. And it will print "Hey there, brown cow"!
Some things to note, when inside your main function. Put ; after every statement. (This is the finished line) (Note not every line is strictly a statement, later on you can move lines down or split them for organization)
"cout" means C++, output. This will do something that comes from the computer and is seen by the user. Always use << after c out commands. The quotation marks in the cout statement mean, C++, print this message. It's simple enough. endl; moves the entire pointer down a line. System pause and return 0 are things I put there that, you don't really need to know what they do at this point. That also goes for the include <iostream> and using namespace std;. int main() {} is our main function. Every program needs to have a main function.
Go ahead and experiment with the couts and endl. With it you can create very basic output commands. Also notice after every program is over, due to the system pause and return 0. The program will require one press a key to end the program. Without the last two lines in the main function. The program would run and close faster than you can blink (Unless your computer is dated). This is because your computer computes this program SO fast that we cannot process it. You might see it blink up and back down of course. (The console that is)
Using these experimentations. It's possible to make something that simply does the same thing over again manually.
I put the (n) there to represent the line count. You do not need these.
So far we have covered basic computer output using text. Now we must go into something that is a core basic. Variables.
In math class, you probably learned that variables are symbols that can hold a certain value. The same is absolutely true in C++ too.
There are ints, strings, doubles, chars, and bools just to name a few variable types off the top of my head. It's imperative that you know this concept. If you don't, it'll make programming near impossible.
Our first variable will be an int. Int means integer, and from it's name you can probably decide what it does. It states the variable as a real number integer. {-2, -1, 0, 1, 2} To state something use the following layout!
Say you did not want to state a variable value because it would be pointless. Maybe you wanted the user to input it. Then just use the same layout but exclude the equal sign and the value. As shown.
How do you show a variable? Unlike a standard message. All standard variables simply require you to enter the variable ID as stated in the function.
For example,
If you run and test the following code, you will notice that even though we didn't type 21 in the message. It still showed. This is using a variable and outputting it to the screen! Fun right?
It may seem like too much right now. Indeed it is wasteful to make an entire variable for something that you are just going to output only anyways right? Yet, integer variables can be rather useful. You will find out why now!
User input is fairly important. Without it, the code seems like it's doing it's own thing. This is pretty bad. Especially in something like a game where user input is nearly required to make it interactive.
So how do we get user input in the console. It's not the standard keypress statement sure. Go with cin. Cin is a define inside the IOSTREAM library.
Think of it as the opposite of cout. Instead of "C" "outputting" (key words) something. CIN will make the user input into C something. (I hope you know why it's named COUT and CIN now) Since I said CIN was the opposite. It will need different signs. Here's the layout of a cin. I will compare it to cout.
Cin's most basic function is to insert variables. Obviously one of it's most important! Inserting variables is key to a lot of programs! So, here's a program that, when the user inputs a variable. The variable is then displayed.
Something to remember is that after a cin, the line automatically breaks, (Correct me if I'm wrong on this!).
Now, you know what what this program does if you tested it. And if you are a smart individual you might have guessed! The user will input their number. And then the program states that this is your favorite number. And then says the number! Clever!
I want to take this time to go over math operations! In C++ these are all valid. Just like real life!
The second one from the end point is rather strange...
A completely invalid statement in math yes...
In C++ this sort of states that X is now 2 more than the original value of X.
Also, in C++,
The equal sign (=) states the value of a variable.
The check sign (==) checks if the variable is a specific value.
Keep that in mind!
Here is a code I am about to come up with on the top of my head. It's a very impressive first program. Check this out!
Yes, it took about 4 minutes to type. Calculators are something I used to challenge myself when I first started C++. I've come up with some wild ones...
This is a calculator, it may sound impressive. But in C++, and in other programming languages. Calculators like this are nearly as basic as Hello World scripts. They are still fun to code though! There should be nothing here you don't understand.
Take note when assigning a variable the sum, product, or other similar math value of other variables. Make sure the variable you are assigning the value comes first.
The first is a valid statement, the second... is not...
Although it may seem counter-intuitive. This isn't math. At least not standard. In actual math, x + y = z is a totally valid equation. In C++ the variable you are stating the value of is always first.
I'd like to make this a lab and have you make a calculator for yourself. Try not to refer to my code but your past lessons! When you are finished, continue reading on!
Something I want to put on before we continue is just a small lesson. It's called Arrays. Say you don't want to type your variables out and define them over and over and over. Or you want a variable with multiple values.
An array is like this!
This should display all the variable's values.
To be continued (Correct me on any false points. I am not a programmer OR a writer.)
Book 1
Learn about C++ and it's functions!
Hello, me and my partner Draxor are here to show you a little of C++ and how it works. In this Tutorial we are going to explain how to start coding C++ in it's own compiler and IDE. We'll also go over basic commands. And loops. After this, we are going to go into libraries a little bit. And then, we are going to finish off with a large scale lab.
Indeed, in this tutorial you are going to have to work with labs. Labs are just sections in the book where I will challenge you to make code. There are 5 labs in every book. Here are Book One's Labs.
Book 1 Labs
- Simple "Hello World" style script!
- Learn how to use BASIC IOSTREAM commands!
- Get your first C++ Program under your belt!
- Learn about the main structure of console programs
- Learn how to use BASIC IOSTREAM commands!
- Simple Single Use Addition Calculator!
- Learn about editing variables using math operators!
- Learn about User Input and get Practice with it!
- Learn about Math Operations in C++!
- Learn about editing variables using math operators!
- Counter Program!
- Get first account experience with While Loops!
- Learn about other loops along the way (Not in the Lab)!
- Learn about short hand math functions! (variable++ and variable--)
- Get first account experience with While Loops!
- Practice Drill!
- Recap all of your lessons in a program of your VERY own!
- You have to do the work on this one, but it leads you with a feeling of pride!
- Have your first original program!
- Recap all of your lessons in a program of your VERY own!
- Advanced 4 Operation Calculator
- Make a large scale first program!
- Put together loops, if statements, and math operators!
- Unlike other labs, This lab is almost all up to you. Do what you want with it!
- Make a large scale first program!
To be finished!
Let's go over getting your compiler. This is important. You need a compiler to compile your C++. Without it, running programs is impossible and writing them for personal benefit is pointless. I suggest DEV C++. Google it and find it to download. Here's a direct link to the one used in this tutorial.
sourceforge.net/projects/dev-cpp/files/Binaries/Dev-C%2B%2B%204.9.9.2/devcpp-4.9.9.2_setup.exe/download
After Set-up (I suggest going with Default), Start up a new project. Go to file > new > source file
Afterwards you should have a clear blank canvas of workspace. Here's some things I want you to type. On the first line. Type
#include <iostream>
All libraries are surrounded in <>s. This is needed for console applications that I will be teaching. Now edit your canvas to look like this!
#include <iostream>
int main() {
using namespace std;
cout << "Hey there, Brown cow" << endl;
system("pause");
return 0;
}
This is something that might confuse you at first. Yet, I am certain you know what it does. It will open the command prompt, That black box thingie you see on computers. And it will print "Hey there, brown cow"!
Some things to note, when inside your main function. Put ; after every statement. (This is the finished line) (Note not every line is strictly a statement, later on you can move lines down or split them for organization)
"cout" means C++, output. This will do something that comes from the computer and is seen by the user. Always use << after c out commands. The quotation marks in the cout statement mean, C++, print this message. It's simple enough. endl; moves the entire pointer down a line. System pause and return 0 are things I put there that, you don't really need to know what they do at this point. That also goes for the include <iostream> and using namespace std;. int main() {} is our main function. Every program needs to have a main function.
Go ahead and experiment with the couts and endl. With it you can create very basic output commands. Also notice after every program is over, due to the system pause and return 0. The program will require one press a key to end the program. Without the last two lines in the main function. The program would run and close faster than you can blink (Unless your computer is dated). This is because your computer computes this program SO fast that we cannot process it. You might see it blink up and back down of course. (The console that is)
Using these experimentations. It's possible to make something that simply does the same thing over again manually.
#include <iostream>
int main() {
using namespace std;
cout << "Hello!" << endl;
cout << "This is a new line (2)" << endl;
cout << "This is a third line (3)" << endl;
system("pause");
return 0;
}
I put the (n) there to represent the line count. You do not need these.
So far we have covered basic computer output using text. Now we must go into something that is a core basic. Variables.
In math class, you probably learned that variables are symbols that can hold a certain value. The same is absolutely true in C++ too.
There are ints, strings, doubles, chars, and bools just to name a few variable types off the top of my head. It's imperative that you know this concept. If you don't, it'll make programming near impossible.
Our first variable will be an int. Int means integer, and from it's name you can probably decide what it does. It states the variable as a real number integer. {-2, -1, 0, 1, 2} To state something use the following layout!
int variablename = value;
[b]Or if you wish to not state a value
int variablename;[/b]
Say you did not want to state a variable value because it would be pointless. Maybe you wanted the user to input it. Then just use the same layout but exclude the equal sign and the value. As shown.
How do you show a variable? Unlike a standard message. All standard variables simply require you to enter the variable ID as stated in the function.
For example,
#include <IOSTREAM>
int main(){
using namespace std;
int variabletest = 21;
cout << "Clocky's favorite number is " << variabletest << endl;
system("pause");
return 0;
}
If you run and test the following code, you will notice that even though we didn't type 21 in the message. It still showed. This is using a variable and outputting it to the screen! Fun right?
It may seem like too much right now. Indeed it is wasteful to make an entire variable for something that you are just going to output only anyways right? Yet, integer variables can be rather useful. You will find out why now!
User input is fairly important. Without it, the code seems like it's doing it's own thing. This is pretty bad. Especially in something like a game where user input is nearly required to make it interactive.
So how do we get user input in the console. It's not the standard keypress statement sure. Go with cin. Cin is a define inside the IOSTREAM library.
Think of it as the opposite of cout. Instead of "C" "outputting" (key words) something. CIN will make the user input into C something. (I hope you know why it's named COUT and CIN now) Since I said CIN was the opposite. It will need different signs. Here's the layout of a cin. I will compare it to cout.
cout << "This is a cout statement from the IOSTREAM library" << endl;
cin >> variable;
Cin's most basic function is to insert variables. Obviously one of it's most important! Inserting variables is key to a lot of programs! So, here's a program that, when the user inputs a variable. The variable is then displayed.
#include <iostream>
int main() {
using namespace std;
int favoritenumber;
cout << "Please enter your favorite number!" << endl;
cin >> favoritenumber;
cout << "Your favorite number is " << favoritenumber << "!" << endl;
system("pause");
return 0;
}
Something to remember is that after a cin, the line automatically breaks, (Correct me if I'm wrong on this!).
Now, you know what what this program does if you tested it. And if you are a smart individual you might have guessed! The user will input their number. And then the program states that this is your favorite number. And then says the number! Clever!
I want to take this time to go over math operations! In C++ these are all valid. Just like real life!
n * u
n/u
n + u
n - u
x++
n--
--n
++x
x = x + 2
With the cmath library. One could use these!
sqrt(144) = 12
The second one from the end point is rather strange...
A completely invalid statement in math yes...
In C++ this sort of states that X is now 2 more than the original value of X.
Also, in C++,
The equal sign (=) states the value of a variable.
The check sign (==) checks if the variable is a specific value.
Keep that in mind!
Here is a code I am about to come up with on the top of my head. It's a very impressive first program. Check this out!
#include <IOSTREAM>
int main() {
using namespace std;
int fn;
int sn;
int answer;
cout << "x + y | Enter first number to add!" << endl;
cin >> fn;
cout << fn << " + y | Enter second number to add!" << endl;
cin >> sn;
answer = fn + sn;
cout << fn << " + " << sn << " = " << answer << "!" << endl;
system("pause");
return 0;
}
Yes, it took about 4 minutes to type. Calculators are something I used to challenge myself when I first started C++. I've come up with some wild ones...
This is a calculator, it may sound impressive. But in C++, and in other programming languages. Calculators like this are nearly as basic as Hello World scripts. They are still fun to code though! There should be nothing here you don't understand.
Take note when assigning a variable the sum, product, or other similar math value of other variables. Make sure the variable you are assigning the value comes first.
answer = x + y;
x + y = answer
The first is a valid statement, the second... is not...
Although it may seem counter-intuitive. This isn't math. At least not standard. In actual math, x + y = z is a totally valid equation. In C++ the variable you are stating the value of is always first.
I'd like to make this a lab and have you make a calculator for yourself. Try not to refer to my code but your past lessons! When you are finished, continue reading on!
Something I want to put on before we continue is just a small lesson. It's called Arrays. Say you don't want to type your variables out and define them over and over and over. Or you want a variable with multiple values.
An array is like this!
int arrayname = {20, 25, 30, 35, 100};
|
cout << arrayname[1] << arrayname[2] << arrayname[3] << arrayname[4] << arrayname[5] << endl;
This should display all the variable's values.
To be continued (Correct me on any false points. I am not a programmer OR a writer.)