How to use C++ for Absolute Beginners


By: sw0rd95
March 28, 2010

C++ is a very useful programming language.

To use C++ one must have a compiler, I would reccomend Microsoft Visual C++ 2008 Express Edition, It is free and the one that I use.

This is a basic program:

// abcd.cpp : main project file.

#include "stdafx.h"

#include

using namespace System;

using namespace std;

int main()

{

cout<< "Hello world!";

return 0;

}

It would get the output, Hello world!     At the moment, you are probably questioning the usefulness of C++ as it is WAY easier to just type that. C++ can do much more, as said later in this tutorial.

Now let's think about each part of the program:

// abcd.cpp : main project file.    Is a comment, comments are ignored  and are just used to help make the program easier to read.

#include "stdafx.h" and   #include have a # infront so they are messages to the compiler's preprocessor

using namespace System; and using namespace std; are refering to namespaces, all stuff in C++ is under a namespace, for most basic programs, System and std are the only namespaces you would need to worry about.

int main() must be in EVERY program in C++ but what is inside the brachets can change.

{ opens the main part of the program

cout<< "Hello world!";  Is what shows Hello world! in the output.

return 0; is the normal end to a basic program

And finally } ends the main part.


And that is your first program!


C++ can also deal with numbers such as in this program:

// abcd.cpp : main project file.

#include "stdafx.h"

#include

using namespace System;

using namespace std;

int main()

{

const int number = 5;

cout<< number;

return 0;

}

Most of the stuff from the first program is there, but now there are numbers.

const int number = 5;  Is a thing that has/can have a value assigned to it, that's why there is an int.  const is saying that the integer will not change, it's very handy later on when compiling more complex programs. And the = 5 is assigning it a value of 5.  number is the name of the integer.

cout << number;  Is showing the value of the integer 'number' in the output, which is 5 as specified in the program.

 

Thank you for reading the basic C++ Tutorial, more things covered in the next one such as strings and input.

By Sw0rd95


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question