The next one is going to be dealing with classes and pointers!
main.cpp Code:
/**
* Classes example script by RageD
* (C) 2008 RageD.
*
*/
#include "class.cpp" // This is our own header file!
#include <iostream>
using namespace std;
int main()
{
Main * Main; // Define Main to go to the Main class
Main->loop(); // This is a pointer. Make this go right to
// the loop(); function within the Main class
}
Very simple main.cpp. But how can this program actually do anything you ask? Well continue on. Main * Main is the definition for the class. Because Main is defined as the Main class, you can use the pointer (->) to go to the function "loop" inside the Main class

Make sense? It will when you execute the application.
class.cpp Code:
/**
* Example class script by RageD
* (C) 2008 RageD.
*
*/
#include <iostream>
using namespace std;
/**
* Create the class structure here
*
* Not too difficult and pretty self explanatory but take a look anyway :)
*
*/
class Main
{
public:
int loop();
int a();
int b();
int c();
private:
bool w00t(); // Not coded in example :P
}; // Yes, semi-colon is supposed to be here in this one. Even with the "}"
/**
* Now let's make the structure useful :P
*
*/
int Main::loop()
{
char opt; // Define opt as "char"
cout<< "Welome to the main menu!\n\nPlease select one of the following options:"<< endl << endl << "a\nb\nc\n\nx to exit" << endl << endl << "Your option: ";
cin>> opt;
switch(opt)
{
case 'a':
return Main::a();
break;
case 'b':
return Main::b();
break;
case 'c':
return Main::c();
break;
case 'x':
cout<< "Exiting...\n\n";
return 0;
break;
default:
cout<< "Your option '"<< opt <<"' is not a valid option!" << endl;
return Main::loop();
break;
}
}
int Main::a()
{
cout<< "Great, you selected option A!\n\nPlease hit return to continue..." << endl << endl;
Main::loop();
}
int Main::b()
{
cout<< "Sweet, you picked option B!\n\nPlease hit return to continue..." << endl << endl;
Main::loop();
}
int Main::c()
{
cout<< "Erm.. Why C? C++ FTW! :D\n\nPlease hit return to continue..." << endl << endl;
Main::loop();
}
This class file is where all the work is done. The functions, etc. are all coded here.
Well let's begin with our declarations. We've declared our variable "opt" as a char so it can be read as such. We could declare as int but because it is not an integer, it would crash the program, so it is important to declare variables according to type.
Next, the switch(). This is very similar to doing just if(this) { return that }. But since it's the same variable just with a different option using the case 'OPTION': stuff here break; is a better way to go really. If there's any confusion on that let me know but the syntax is all right there.
int Main::a(), etc. These are the actual functions. Remember, because everything is directly declared here you can use the Class::FUNCTION(); to call these as refrences. However, in other documents such as main you must declare something to the Main class then use pointers (->) to call the
PUBLIC function. It is important that the function be public.
Compiling C++ Programs! Windows
Now that you have all this code, I'm sure you're wondering how the hell you compile it into a program. Well, it's pretty simple depending on your operating system. If you're using Windows, I suggest downloading the free MS Visual C++ compiler. Sure it's Microsoft, but hey, it's free and it's (believe it or not) the best compiler you'll find for Windows if you're looking for a built-in GUI. Other than that, GCC (what will be used for linux) is truly the best way to compile. For Windows you will need to download MSYS and MingW which is the Windows port of GCC. For help with command line options, read on.
Linux
Ah, yes. The best place to really compile

. Well, all you'll need is GCC. However, we won't be using the gcc command as that is a C compiler. We will be using G++

. There are no Makefile's so we're going to be using the raw commands to get this to work!
This example is assuming that your main file is main.cpp. If there are any errors, the compilation will not complete but it will spit out an error.
Code:
g++ main.cpp -o Program_Name
As long as everything is properly linked (via #include, etc.) within the files, everything should be ok if programmed properly

If there error log is too lengthy to read without scrolling, etc. (which often happens as you develop larger programs) try something like the following command to output the errors to a separate text file you can read at your own pace:
Code:
g++ main.cpp -o Program_Name > err.txt
And last, but certainly not least, I mentioned I'd possibly dabble into cross-compiling for Debian based systems as it's a little simpler to setup :P
Well you're going to want to run the following command. For Ubuntu/Gentoo or systems like that you may need to "sudo" in front of the command. On standard Debian you must be logged into root or at least "su" to it.
Code:
apt-get install mingw32
Once that is installed, edit your ".bash_profile" file. Add the following line:
Code:
alias ming++="/usr/bin/i586-mingw32msvc-g++"
That will make ming++ your command to cross compile for Windows!
Now, you would run it exactly like you would for normal g++:
Code:
ming++ main.cpp -o Program_Name.exe
Well I think that will about wrap this one up. If there's anything I've overlooked/wasn't thorough on due to familiarity with the subject, please let me know and I will revise!
I have also included a ZIP file. It has ALL examples listed here as well as 2 or 3 other example scripts you can take a look at explaining how to make your own namespaces, declaring variables as part of a function, etc.!
-RageD