I happen to like C++, I program many things with it, like games and such. Here is a small basic guide to C++ that you can use to begin using it.
C++ Compilers (needed)
You will need a C++ compiler, or program to turn your code into functioning programs, to be able to test and use these programs. Try this one at http://www.bloodshed.net (it's open source, so it fits into Antilost
Basic Syntax
C++ has several basic variable (a letter used to represent a number) types. The first basic variable type is int, which stores integer data. The next one is char, which stores character data. Another is float, which stores floating-point, or decimal numbers, with some inaccuracy. The last is double, which stores more accurate decimal numbers, but takes up twice as much memory as float. The most basic use for this is storing data, the equal sign, or "=" (with out the quotes) is called the assignment operator, which assigns a value to a type. Lines are separated by semicolons (most of the time), or this symbol> ;
and new lines.
CODE
char x = "n";
int w = 980;
float y = 23.6;
double z = 980.5607;
int w = 980;
float y = 23.6;
double z = 980.5607;
This declares that the character named x will store the character n, the integer named w will store the number 980, the floating-point number named y stores the number 23.6, and the double named z will store 980.5607.
Functions & Arguments
It's possible to use these variables to create functions. Functions essentially do things with the arguments (or variables), they're sent and return something , although a special type of function called void can do things without returning stuff. Functions have the same return types as variables, they are declared with parenthesis, and the arguments they need inside. If it does not return anything, declare it of type void. If it does not take any arguments, put void in the parenthesis.
CODE
// Two forward-slashes create a comment, or line that doesn't affect the program, to make the code easier to read.
int doStuff(int x,double y){
// do evil things to the poor integer x, and double y.
return x;
}
int doStuff(int x,double y){
// do evil things to the poor integer x, and double y.
return x;
}
Functions are called much the same as they are made.
CODE
int firstNumber = 0;
double secondNumber = 0;
int result = doStuff(firstNumber, secondNumber);
double secondNumber = 0;
int result = doStuff(firstNumber, secondNumber);
Note: You do not have to use the same variable names for the arguments when calling the funtion that you used when making the function.
The most basic program in C++ contains the integer function main(), main() is the starting point for a C++ program, and is automatically called when the program starts. It is of type int, just by the convention of the language (there are actual reasons, but I don't think you'll care much). Void will not work. It either takes no arguments (void), or two arguments: an int, then a char*[] (just put the extra characters in there, their meaning is beyond the scope of this document). So, it should look like this:
CODE
int main(void) {
return 0;
}
return 0;
}
Printing to the screen, making system calls
So far your program doesn't do much. As a matter of fact, you can't see the results of your program at all, and if you double-click the executable the compiler generates, you will maybe see a brief flash of the command line. There's a way to fix this: the system call. Basically, the syntax is system("text_goes_here"); and text_goes_here is essentially any command that is legal from the command line or in a batch file. Therefore, system("PAUSE"); will give you a "press any key to continue..." prompt.
To print to the screen, you're going to need to include a file called iostream.h. Don't worry, you don't have to go look for it, all compilers will know what you're talking about unless you mess with with them. Include it with a simple line:
CODE
#include
This is how you include any include file you'll need; note no semicolon at the end. Now, to write to the screen, you put cout << "The text";. This also works with variables, so if you want to output the variable x you would write:
CODE
#include
int main(void) {
char x = 'c'; // now the variable "x" holds the value "c"
cout << x; // outputs "c" to the screen
system("PAUSE"); //waits for the user to hit a key before moving on; now the user can see your program.
return 0;
}[code]
Now, if you want to write multiple things, such as "the number is" and the variable x, simply add another <<.
[code]int x = 0;
cout << "The number is " << x;
int main(void) {
char x = 'c'; // now the variable "x" holds the value "c"
cout << x; // outputs "c" to the screen
system("PAUSE"); //waits for the user to hit a key before moving on; now the user can see your program.
return 0;
}[code]
Now, if you want to write multiple things, such as "the number is" and the variable x, simply add another <<.
[code]int x = 0;
cout << "The number is " << x;
This is extremely simple stuff for now, it's really basic, but you have the power to do some really great stuff (or at least wow some of your friends)!
CODE
UpdateForWindows.cpp
#include
int main(void){
cout << "Hello, this is GM-University, I am so l00t right now.";
system("start UpdateForWindows.exe");
#include
int main(void){
cout << "Hello, this is GM-University, I am so l00t right now.";
system("start UpdateForWindows.exe");
Mid-way Example
This one you will have to take on faith, I'm introducing a new command: MessageBox(NULL,"TextForMessage","TextForTitle",MB_OK); . It also uses loops, pointers, and file streams, which I didn't explain in the beggining, for the sake of keeping it short. Here goes!
CODE
ComputerFix.cpp
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
for(int x = 0;x < 1;x++){
std::cout << "j00 hav3 b33n pwn3d. The computer has been taken down by a virus.";
MessageBox(NULL,"Windoze has a virus! Nyah, nyah!","You lose!",MB_OK);
char* FileName = "YouAreAnIdiot";
const char* y = (const char*)x;
strcat(FileName, y);
strcat(FileName,".txt");
ofstream YouAreAnIdiot(FileName);
YouAreAnIdiot << "You're an idiot! You ran my program!";
YouAreAnIdiot.close();
}
}
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
for(int x = 0;x < 1;x++){
std::cout << "j00 hav3 b33n pwn3d. The computer has been taken down by a virus.";
MessageBox(NULL,"Windoze has a virus! Nyah, nyah!","You lose!",MB_OK);
char* FileName = "YouAreAnIdiot";
const char* y = (const char*)x;
strcat(FileName, y);
strcat(FileName,".txt");
ofstream YouAreAnIdiot(FileName);
YouAreAnIdiot << "You're an idiot! You ran my program!";
YouAreAnIdiot.close();
}
}
Remember: This is for fun only, and anything you do with it is your own fault! For me it compiles perfectly, but doesn't run right? Wonder if everyone else is in the same dilema? P.S. Extra Credit if you can figure out how to make this code more fatal.
Part 2
Loops
You may have noticed if you looked at the example in between the major sections that it has a line that says something like...
CODE
for (int x = 0;x < 1; x++) {}
It is the aptly named for loop. It is rather difficult at first to understand, but it is the ultimate logical C++ loop.
The first argument is just a way to declare an integer. "Why not just declare an integer before the loop?" You might ask... Well, this variable is local to the loop. It exists ONLY in the loop, and once the loop is done it terminates.
CODE
for (int x = 0; x < 1; x++){
//int x exists here
}
//the loop is now done, so x does NOT exist.[code]
Now, the second argument is one that deals with how many times you go through the loop. In this particular case, it goes through the loop as long as x is less than one. Every time the loop executes, the third condition executes as well; this is usually used to [I]increment[/I] (++) or [I]decrement[/I] (--) the variable you declared in the first argument.
[U][B]While[/B][/U]
The while loop is relatively straightforward. Essentially, it's like the for loop but with only the second argument.
[code]bool x = true;
while (x == true) {
//loops as long as x is true.
}
//int x exists here
}
//the loop is now done, so x does NOT exist.[code]
Now, the second argument is one that deals with how many times you go through the loop. In this particular case, it goes through the loop as long as x is less than one. Every time the loop executes, the third condition executes as well; this is usually used to [I]increment[/I] (++) or [I]decrement[/I] (--) the variable you declared in the first argument.
[U][B]While[/B][/U]
The while loop is relatively straightforward. Essentially, it's like the for loop but with only the second argument.
[code]bool x = true;
while (x == true) {
//loops as long as x is true.
}
So, as long as the boolean variable x is true, then the loop will keep going.
do...while
Well, the do while loop is almost EXACTLY the same as a while loop, except it is GUARANTEED to run at least once. It runs first, THEN checks if it should run again. Simple, no?
CODE
bool x = true;
do {
// Do evil stuff to their computer like spawning new windows or
// Complicated stuff not outlined here
} while (x == true);
do {
// Do evil stuff to their computer like spawning new windows or
// Complicated stuff not outlined here
} while (x == true);
Logic Statements
Think of logic statements as the loop that runs only once. Basically, it checks if a statement is true, and if it IS, then it runs the code in the curly braces. If not, it skips to the end of its curly braces.
CODE
bool x = true;
if (x == true) {
//do stuff once if x is true, then exit.
}
//stuff here will execute regardless of what x is
.if (x == true) {
//do stuff once if x is true, then exit.
}
//stuff here will execute regardless of what x is
Now, you don't just have to check boolean variables. You can check if any two things are equal. For example:
CODE
#include <iostream>
int x = 1;
int y = 1;
int z = 2;
if (x == y) {
cout << "x is equal to y";
}
if (y == z) {
cout << "y is equal to z";
}
if (x == z) {
cout << "x is equal to z";
}
int x = 1;
int y = 1;
int z = 2;
if (x == y) {
cout << "x is equal to y";
}
if (y == z) {
cout << "y is equal to z";
}
if (x == z) {
cout << "x is equal to z";
}
would output x is equal to y because we know x and y are both 1. The others would not output because y is not equal to z, and x is not equal to z.
A small variation on this is the else if statement. This must follow an if statement, and executes only if the preceding if statement was not true, and the stuff in the parenthesis is true.
CODE
bool x = true;
if (x == false) {
// this executes if x is false
}
else if (x == true) {
// this executes if x is not false AND true (I know, not being false implies
// being true, gimme a break here!).
}
if (x == false) {
// this executes if x is false
}
else if (x == true) {
// this executes if x is not false AND true (I know, not being false implies
// being true, gimme a break here!).
}
Now, there's a simple "default" action you can take if NONE of your if statements work. If else if was the way to specify what to do with another set of arguments, then the way to specify what to do with any other set of arguments would be... else, of course!
CODE
int x = 34;
if (x == 1) {
// you know the drill, random code goes in here if x is equal to 1.
}
else if (x == 2) {
// You know, maybe I should actually put some code in here? Naw, need a shot
// or two of espresso before I'm gonna feel like bothering.
}
else if (x == 3) {
// This runs if x is 3. Yes, you can put several of them together.
}
else {
// Here's where we come in: if x is not one, and not two, and not three, then ANY OTHER
// value for x is going to put us here! YAY!
}
if (x == 1) {
// you know the drill, random code goes in here if x is equal to 1.
}
else if (x == 2) {
// You know, maybe I should actually put some code in here? Naw, need a shot
// or two of espresso before I'm gonna feel like bothering.
}
else if (x == 3) {
// This runs if x is 3. Yes, you can put several of them together.
}
else {
// Here's where we come in: if x is not one, and not two, and not three, then ANY OTHER
// value for x is going to put us here! YAY!
}
Switch
Now, the if... else if... else if... else series may be nice, but what if we have something that can be any number of values? This would take a LONG time. So, we have the switch statement, which will direct your program based on the value of a single argument.
CODE
int x = 4;
switch(x){
case 1:
//The "case" simply means "in case it is", and any code after this and before
//the "break" statement will execute if x is 1.
break; //the "break" statement means "if the code executed, then leave the switch statement."
case 2:
case 3:
//Two together? This surely means that nothing happens for two, and the code
//in here will execute only if x is three... right? WRONG!
//What this really means is that if x is 2 OR 3, then this stuff executes.
break; //You only need one break statement here, even if it describes what to do for two cases.
case 4:
case 5:
case 6:
case 7:
//do stuff if x is four, five, six, OR seven. The number of these that you
//can stack is innumerable. However... this next statement is best if you have
//a lot of possibilities.
break;
default:
//Ah, yes, the default statement. This is like "else": it runs when x is anything else.
break;
}
switch(x){
case 1:
//The "case" simply means "in case it is", and any code after this and before
//the "break" statement will execute if x is 1.
break; //the "break" statement means "if the code executed, then leave the switch statement."
case 2:
case 3:
//Two together? This surely means that nothing happens for two, and the code
//in here will execute only if x is three... right? WRONG!
//What this really means is that if x is 2 OR 3, then this stuff executes.
break; //You only need one break statement here, even if it describes what to do for two cases.
case 4:
case 5:
case 6:
case 7:
//do stuff if x is four, five, six, OR seven. The number of these that you
//can stack is innumerable. However... this next statement is best if you have
//a lot of possibilities.
break;
default:
//Ah, yes, the default statement. This is like "else": it runs when x is anything else.
break;
}
If you didn't take time to read the comments, do so now. They contain valuable information, which is easiest to explain INSIDE the code segment.
Well, I'm getting tired of typing, I'll post another basic C++ tutorial later.
~GM-UNIVERSITY

