Hi guys, I'd like to help people out there who want to learn c++. There are probably tutorials that are better than mine, but I'm bored, and i'd like to use some of my time via teaching. NOTE: I'm still currently learning c++(can't self-study due to school), so i'll probably be able to only teach the basics until I have time to learn/not lazy to. I will try to release tutorials weekly, if possible.
--------------------------------------
Let's begin. First start off by downloading a C++ compiler. There are many out there: Codeblocks, Visual Studio, Turbo C++, etc. But I recommend using Visual Studio C++ 2010.
NOTE: I recommend using Visual Studio C++ 2010!
Download Link for Visual Studio:
Visual C++ 2010 Express | Microsoft Visual Studio
run the software and install it. (Sorry for not having any instructions about the installation process. :c)
After installing it, open up Visual Studio C++ 2010.(If you're not using VC++2010, ignore and open your compiler)
NOTE: You may be prompted to register for a key, just sign up and receive your key.
After opening VC++2010, a 'start page' will appear.
- Click on 'New Project'
- Click on Win32 Console Application and give it a name. EG: Tutorial1
- Click on Finish
Now there should be some code on your page that looks like gibberish.
Delete/erase this part off your code:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Now you should only have this: (
only if you're using visual studio C++)
Code:
#include "stdafx.h"
Don't worry if you don't understand what the heck these are, I will explain them soon.
Write/copy+paste this down onto your compiler now:
Code:
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
std::cin.get();
return 0;
}
Hmm. now what are these 2?
Code:
#include "stdafx.h"
#include <iostream>
'#include' <- Now this means what it means. you're including a certain file(s) for your c++ project.
'#include "stdafx.h" <- This means to include the file "stdafx.h" into your project.
'#include <iostream> <- This means to include the file <iostream> into your project.
For now, this is all you need to know about these 2 files:
stdafx.h is a precompiled header
iostream is used so that we're able to use certain functions like 'cout' and 'cin', which i'll get to later.
Code:
int main()
{
std::cout << "Hello World!" << std::endl;
std::cin.get();
return 0;
}
int main() <- int means integer, main() is a function. This is where the compiler starts executing the code.
{ <- this is an opening bracket. This is used to signify the beginning of code. if you don't use this, the compiler won't understand where the beginning of the code is at and will give you errors.
} <- this is a closing bracket. Any code before this is executed by the compiler. Must be used with the opening bracket! without this, the compiler won't understand where the ending of the code is at and will give you errors.
--------------------------------------------------
std::cout << "Hello World!" << std::endl; <- Now what you might be thinking of is "oh, std, that stands for sexually transmitted disease. derp." well, sorry to burst your bubble, but it doesn't. std means standard.
cout << "Hello World!" << std::endl; = basically means: "Print out to the screen whatever that is between the quotes." So in this case, it would print out to your screen (in a CMD console) "Hello World!"
std::endl; = Now why is there a semicolon there? The reason there's a semicolon there is because you need one after each statement. If you try compiling without any semicolons after your statement, you'd receive errors.(Try it if you want!)
--------------------------------------------------
std::cin.get() <- this basically means "wait for the user to input something". (The reason we use this is to stop the console from closing so fast in a split second, without it, your console would exit out in a blink of an eye)
--------------------------------------------------
return 0; <- Remember int main() from before. 'int' means integer. integer means a number. There has to be a value to int, so we return a value to it, in this case, is 0.
Info about std::
now later on writing std::function over and over again will be tiring/annoying. To make our lives easier, there is a statement for that.
Code:
using namespace std;
using namespace std; <- this basically means that we'll be using the namespace standard. (not sexually transmitted disease.., standard) By using this, we will no longer have to write std:: for each function that we want to use.
So by implementing this into our code, we should now have this:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cin.get();
return 0;
}
Now doesn't that make our lives easier?
NOTE: Don't worry, I didn't waste your time. the "std::" method may be used at certain times instead of just using this statement.
--------------------------------------------------
Now you've created your first c++ program!
Sorry if this is so messy, I've gotten little sleep today and i've been doing nothing but homework.