[Beginners guide] Getting started with C++ (g++/Visual C++)

DON'T post new tutorials here! Please use the "Pending Submissions" board so the staff can review them first.
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[Beginners guide] Getting started with C++ (g++/Visual C++)

Post by ayu »

Introduction:

Learning a programming language is not very hard. Usually it's where to start, that beginners get stuck at. This small guide will help you get started with the right equipment that you need to get going in your quest to learning a programming language, and in the case of this guide, C++ (applies to C as well). Please note that this guide is only written to support Windows and Linux environments and I can not guarantee that anything will work the same in other operating systems.

Getting started:

First of all, you will need reading material. A lot of people suggest that you simply Google a guide on the Internet and get started from there, but I disagree, it is not healthy for your eyes to read that much text during longer periods of time. Therefore I suggest that you get a book, and if you don't already have one in mind, then I have one that I strongly recommend.

Title: Starting Out with C++: Early Objects
Author: Tony Gaddis (Author), Judy Walters (Author), Godfrey Muganda (Author)
Language: English
ISBN-10: 0321515692
ISBN-13: 978-0321515698
Product Dimensions: 10 x 8 x 1.5 inches


When you write your code, you will need a compiler or IDE (Integrated development environment, includes compiler).

[Windows users]

For Windows users I recommend that you use Microsoft Visual C++ (There are other very good compilers and IDEs, like Dev-C++, but I will not cover any more here), which can be downloaded here:

[Download]


When you have downloaded it, start the installation.

If the box is checked "yes, send information about my setup ... etc", then uncheck it and press Next.

Image
http://teresa.binarykitten.com/files/cppguideimg/1.png

Accept the license agreement (alternatively read it if you have too much time on your hands), uncheck the "Allow Visual Studio to receive and display RSS content" and press Next.

Image
http://teresa.binarykitten.com/files/cppguideimg/3.png

Uncheck both alternatives (you don't need that now, so no use in installing it), and press Next.

Image
http://teresa.binarykitten.com/files/cppguideimg/4.png

Choose a destination folder for the installation (default is fine) and press Install.

Image
http://teresa.binarykitten.com/files/cppguideimg/5.png

The install will start, this might take a while (do something else while you wait)

Image
http://teresa.binarykitten.com/files/cppguideimg/6.png

The installation is now done, notice the "Register" text, we will take care of that later. Press Exit to continue.

Image
http://teresa.binarykitten.com/files/cppguideimg/7.png

Now, start Visual C++ (it should be located under Start -> Visual C++ 2008 Express Edition).

Image
http://teresa.binarykitten.com/files/cppguideimg/8.png

To create a new project, you can press "File -> New Project"

Image
http://teresa.binarykitten.com/files/cppguideimg/9.png

We want to make a console application, so choose that and enter a name for the project and solution, then press OK.

Image
http://teresa.binarykitten.com/files/cppguideimg/10.png

General information, press Next.

Image
http://teresa.binarykitten.com/files/cppguideimg/11.png

Choose "Console Application" and check "Empty Project", then press Finish.

Image
http://teresa.binarykitten.com/files/cppguideimg/12.png

You should now see that your project has been created and you can add new project files as you wish. But let's start with a quick example, shall we?. Right click on the "Source Files" folder in your hierarchy, choose Add -> New Item.

Image
http://teresa.binarykitten.com/files/cppguideimg/13.png

Now choose C++ File(.cpp), enter a name and press Add.

Image
http://teresa.binarykitten.com/files/cppguideimg/14.png

Now, you should see an empty page before you where you can start your coding from scratch. We will start with a simple coding example called "Hello World" that will print the text "Hello World" to the screen (console window).

I will give you the code without comments here so that you can copy/paste it easily if you wish.

Code: Select all

#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World\n";

	return 0;
}
Image
http://teresa.binarykitten.com/files/cppguideimg/15.png

Now, build your solution (compile it). Alternatively you can do this by pressing F7.

Image
http://teresa.binarykitten.com/files/cppguideimg/16.png

As you can see, the compilation was a success, and a binary file has now been completed, it should be located in your solution folder under the name that you enter before (HelloWorld.exe).

Image
http://teresa.binarykitten.com/files/cppguideimg/17.png

You can now try and run your program, do this by pressing CTRL + F5.

Image
http://teresa.binarykitten.com/files/cppguideimg/18.png


[Register your product]

You can stop reading here if you do not wish to register your product, although I recommend that you do it, or else the product will expire in 30 days.

Goto Help -> Register Product

Image
http://teresa.binarykitten.com/files/cppguideimg/19.png

As you can see, it wants a registration key to be entered, so press Register now, to get to the registration page.

Image
http://teresa.binarykitten.com/files/cppguideimg/20.png

Login with your account (MSN, Hotmail or Passport)

Image
http://teresa.binarykitten.com/files/cppguideimg/21.png

Copy your registration code

Image
http://teresa.binarykitten.com/files/cppguideimg/22.png

And paste it in the text box, then press Complete Registration

Image
http://teresa.binarykitten.com/files/cppguideimg/23.png

Choose "No, I would not like to participate", and press Close

Image
http://teresa.binarykitten.com/files/cppguideimg/24.png

There, you done, and I suggest that you get started with reading that book, the one I recommended before is really good ;)

Happy coding! :)



[Linux users]

Now, for Linux users I will not go that much in depth, because I expect a little more knowledge of the surrounding system from a Linux user. Also there isn't much GUI to take screenshots of using the methods I am about to describe.

You will need a compiler and an editor. For this guide I recommend that you use g++ as a compiler, and a text editor of your choice (Vim, Vi, GEdit, nano, Emacs... etc) to write the code.

Now, create a new document named "HelloWorld.cpp", and save it to a location of your choice (your home folder for example). Write some example code like the code below.

Code: Select all

#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World\n";

	return 0;
}
save the information and open up a terminal window. In the terminal window, navigate (cd) to the location of the HelloWorld.cpp file, and then write the following.

Code: Select all

$ g++ -o HelloWorld HelloWorld.cpp
you should now have a file called a.out in the same folder, run it by typing ./HelloWorld in the terminal.

That's it! That's basically all you need to know to get started in Linux

Happy coding! :)
Last edited by ayu on 06 Aug 2009, 04:53, edited 3 times in total.
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Nice one cats.

You might want to make it more explicit that the result of the compilation is an actual native executable. I'm guessing in the case of Windows a HelloWorld.exe would be produced, so you would want to show them that. Then to make it more consistent in the Linux version, you could change the command line to

Code: Select all

$ g++ -o HelloWorld HelloWorld.cpp
$ ./HelloWorld

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Hmm, good idea leetnigga. I'll change that in a bit : )
"The best place to hide a tree, is in a forest"

Daka666
Newbie
Newbie
Posts: 1
Joined: 06 Aug 2010, 10:58
13

Re: [Beginners guide] Getting started with C++ (g++/Visual C++)

Post by Daka666 »

you are king, thank you very much

Post Reply