• Articles
  • How To Learn The Basics Of C++ Part 1/5
Published by
Oct 7, 2011 (last update: Oct 7, 2011)

How To Learn The Basics Of C++ Part 1/5

Score: 3.8/5 (1994 votes)
*****
Greetings,

You're probably reading this because:

1. You want to learn C++


2. You need a refresher course on the Basics of C++

First of all, before you get into "real coding", you'll need to create a Basic Program called "Hello World"- I know the name's corny, but hey, it serves it's purpose. The "Hello World"(C++ Version), is designed to teach beginners the basic parts to writing a simple program and is a very fundamental step to learning C++.
I'm sure we'd be lost without it.(:D)

Without further a do, you'll need to open up your compiler. You may want to "Google" search a list of free compilers, if you do not have on installed on your computer (I'll be using Xcode, this program will work on any compiler.) Assuming you've already created a new project, label it "Hello World.cpp" (You do not need the quotations around the project name. )After you've named it, I want you to go to the first line of your empty programming space and type the following:

 
// Your Name - Project Title - Date - (Whatever Else-Optional) 


What you've just typed is called a comment. A comment does not affect the program technically, but it's useful to have when you start to create huge programs. Secondly, You'll need to type in:

1
2
3

#include <iostream>

This is called a directive (or preprocesser directive ). There are many directives(#include <windows.h>,#include<ctime>) but for now we will be focusing mainly on the iostream library. This directive is used most commonly in every program for it is the basis of the input/output in C++. Now Type in:

 
using namespace std;

I won't go into details now (I'll explain more in PART 2), but for now just include that bit of code in your programs. Now type in:


1
2
3
4
5
6
7
int main (void){


// Code Go Here 

return 0;
}

What you see above.. int main (void), is a VERY important part when learning programming. If you do not include this, your program will not run. It typically denotes where the main part of your program will begin.
Your code should look like this now:


1
2
3
4
5
6
7
8
9
10
11
// Your Name - Project Title - Date - (Whatever Else-Optional)
#include <iostream>
using namespace std;
int main (void){

// Code Go Here


return 0;
}


If you do not have all the code listed above make changes now. So, now we are going to enter what specifically goes into the program. What makes the program do, what it does... Delete the comment located within the two curly braces and add this:


1
2
cout << "Hello World!";


Make sure you type exactly that, and make sure you've deleted your comment (We don't need it now, It was just a placeholder). You should have something that looks exactly like this:


1
2
3
4
5
6
7
8
9
10
// Your Name - Project Title - Date - (Whatever Else-Optional)
#include <iostream>
using namespace std;
int main (void){

cout << "Hello World!";
return 0;

}



You probably will want to know what "cout" means. Well, its pronounced (CEE-OUT), and it displays text on the screen.. think of it as a prompt. Whenever you want to write message that will display on the screen you must always use this format:


1
2

cout << "Whatever you want goes here";

Always make sure you have those to arrows pointing towards cout. Make sure your text is within the quotations.

ALSO: You do not need to have the:


1
2
3

 return 0;


at the end of your program, but I recommend it just for the purpose of this tutorial (Plus it's the traditional way of signifying your program has exited successfully ). So run the following Program:
1
2
3
4
5
6
7
8
// Your Name - Project Title - Date - (Whatever Else-Optional)
#include <iostream>
using namespace std;
int main (void){

cout << "Hello World!";
return 0;
}


and your Output should result to:


Hello world!



Stay Tuned for PART 2 :)
May the code be with you...
(If I have missed anything please let me know )