Log in using your OpenID or your Sol Union log in account.
[Cancel]     

C++ Language Tutorial Page 2
Hello World!
Ned Bingham
Previous Index Next
Your First Program
     To start, create a new project. If you are in Eclipse, create a Hello World C++ Makefile C++ Project, and make sure you don't put any spaces in while naming the project. If you are in Xcode, create a new Carbon Application Project, delete the Prefix Header and the .nib file, double click on Targets->Project Name, click on the Build tab, find Prefix Header and Precompile Prefix Header, and clear the vales for those. Then, for both Eclipse and Xcode, open the .cpp file and clear it. You are now ready to type in your own code!

Program: Bare Bones
File: Main.cpp
int main()
{
	return 0;
}
     All of your code will be contained within the two curly brackets. The compiler reads the code chronologically from start to end, and if a segment of code is not called in main, it will not be executed. To run this program, save the file, and press the button called Run. This is typically a big green button with a play triangle in it.
     If you are in Eclipse, you may have to go to the arrow next to the run button, then to Run Configurations.... In here you will double click on C/C++ Local Application. This will create a new run configuration. Click on New_configuration, then on the Browse... button under Project:, and select your project. Finally, under C/C++ Application:, click on Search Project..., and select the executable. Now your program should run.

     Now that you have a running program, the first thing you need to do is import libraries to add functionality. to do this, you type #include <library_name> if you are including an external library (one that's not defined in your program), or you type #include "library_name" if you are including an internal library.
Here are the names of some useful libraries and their descriptions:
stdlib.h      --> This library has all functions related to memory, and the environment.
stdio.h        --> This library contains the C functions for the output buffer.
iostream.h --> This has the C++ functions for the output buffer.
time.h        --> This has all of the functions relating to time and random numbers.
string.h      --> This has all of the functions for working with strings.
math.h       --> This library has all of the math functions including cos and sin.

Always include the library before you do anything else:
Program: Bare Bones
File: Main.cpp
#include <iostream.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	return 0;
}
The first thing to learn is how to print things to the screen. To do so, there is an object called cout. Here is the simplest way to use it.
Program: Bare Bones
File: Main.cpp
#include <iostream.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	cout << "Hello World!";
	return 0;
}
Here, what is being output is what is contained within the quotation marks or Hello World!.

cout << "Hello World!";

Not everything you will output will be in quotes, but phrases are a good place to start!
You can also output multiple things by using the << operator again.
cout << "Hello World! " << "My name is Ned!";

There are also special characters that output extra things. For example,
"\n" creates a new line.
"\r" acts like the return key.
"\b" sometimes works as a backspace.
"\a" sometimes works as a system beep.
"\0" tells cout that it is at the end of a phrase.
"\t" creates a tab.
Here is an example of how to use one:
cout << "Hello World!\n" << "My name is Ned!\n";

To use cout, you have to include iostream.h, because that is where cout is defined.
Congrats! You have just made your first program that does something!