CPLUSPLUSHello World!

Posts 1–11 of 11 · Page 1 of 1
Hello World!
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.
good stuff. My C++ professor hated cout so he made us learn printf on our first program lol.

Thanks for sharing! should be helpful for those new to C++!
good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.
Quote Originally Posted by obsta View Post
good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.
system("pause") does the same thing as cin.get(), but not using system("pause") is preferred(imo). :v
Quote Originally Posted by obsta View Post
good tutorial. However it might be more logical for new users to use system("pause") instead of cin.get because they will be running into that later. just my opinion.
not really, its true that they'll be using cin.get later on but its very good practice to start off with it too, there are hundreds of articles explaining why you shouldnt use system(''pause'') However, it is not encourage to use system("pause") to pause command windows from closing in C++. There are two main reasons why:-

1. A call to system("pause") will pause the programs execution and make a system call and thus allocate unnecessary resources.
2. Another reason it should be avoided is because it isn't portable. It works only on systems that have the PAUSE command at the system level, like DOS or Windows.

That means, when you do use system("pause"), it says: Press any key to continue...

I reckon system("pause") is introduced to new beginner to make their life easier in learning and exploring programming language. Just like how we were taught that gravity is equal to 10 when we were in primary/secondary school, and then few years later they told us that actually we should calculate gravity as 9.81.You wouldn't tell a little boy that he should calculate gravity as 9.81 while he is still struggling with multiplication involving decimal, would you??
Well thank you! This is very helpful for beginners to programming and is essentially what my java teacher told us(except about java of course). I currently am still an amateur programming only know a few non-compiled languages well (VBS, Java, Batch, Lua). And very few compiled languages, basically Obj. C. This was nice to see the functions of C++.

EDIT: +1 rep to you!
Quote Originally Posted by Jumboperson View Post
Well thank you! This is very helpful for beginners to programming and is essentially what my java teacher told us(except about java of course). I currently am still an amateur programming only know a few non-compiled languages well (VBS, Java, Batch, Lua). And very few compiled languages, basically Obj. C. This was nice to see the functions of C++.

EDIT: +1 rep to you!
Glad to see that this is actually helping a few people.
If you create an empty project instead of Win32 console, You dont need to delete anything and its cleaner
Quote Originally Posted by merp. View Post
Glad to see that this is actually helping a few people.
Yes it is, I am quite new to C++ and I am still learning some basic commands the most advanced thing I made is a simple php hack for a website game.
#include <iostream>
#include <conio.h>
int main(){
printf("Hello Merp ");
system("pause");
return 0;
}
Quote Originally Posted by Akar View Post
#include <iostream>
#include <conio.h>
int main(){
printf("Hello Merp ");
system("pause");
return 0;
}
You include <iostream> but yet you use printf (stdio.h)
You include conio for no reason.

No thanks.
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?