![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Hello World! - C++ Tutorial
This article will show you the very basics of C++ programming.
I'll assume that you have never programmed in or even looked at C++. C++ (pronounced See Plus Plus) is a compiled programming language, useful for making desktop applications, algorithms, games etc. You can write you C++ program in any text editor and use a program called a compiler to create an executable of your program. The best free compiler/IDE (Intergrated Development Environment) is Dev-C++, you can download it for free at http://www.bloodshed.com. Install it using the install wizard, and then your are ready for your first program. Open Dev-C++, click File>New>Source File or press CTRL + N. From here you can type in your C++ program and it will higlight your syntax and auto indent your code. Now type the code from Program-1 into Dev-C++. I suggets you type it rather than copy and paste it, that way you will remember it and learn it more than if you just copy and paste it. Program-1: //Hello World Program
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
cin.get();
return 0;
}Now, click File>Save and save it as: HelloWorld.cpp (the cpp extension stands for c++). Then click Execute>Compile, it should compile without any errors, so you can just press Execute>Run and it will run the program you just made. Also look inside the directory where you saved HelloWorld.cpp and you will see a file called HelloWorld.exe you can run this by double clicking it. Congratulations! You have compiled and ran your first C++ program. Hopefully, by now you will be wondering how it works, I'm going to go through the code line-by-line so that you will understand what each line does. Code: //Hello World Program Description: This is called a comment, if a line begins with // it will be a comment. A comment is ignored by the compiler so you can type anything after the //. It is designed so that you can write human readable comments in your code. Code: #include <iostream> Description: This includes the iostream header, iostream stands for input/output stream. If you include this into your files it will allow you to take in input and send out output. Code: using namespace std; Description: This makes the std namespace global. It is hard to explain it when you have only just started, but you should include this in your code. If you dont you will have put std:: in front of standard functions, such as cout. cout<<"Hi"; would become std::cout<<"Hi"; Code: int main() Description: This is the main function. Every C++ program must have a main function. And code between the { and } will be executed when the program is run. Code: cout<<"Hello World"; Description: This code writes the text Hello World onto the screen, as you saw when you ran the program. Code: cin.get(); Description: This code makes it so that you have to press a key before the program continues, without it the program would just flash on the screen and then off again. Code: return 0; Description: This makes the program end successfully, if you use return 1; it means that there has been an error and it has to exit unsuccessfully. Ok I hope you have learnt something from this tutorial, it should help you when you go onto a complete tutorial about the C++ language. Recommended Links: http://cprogramming.com http://cplusplus.com http://cplus.about.com
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Nice tutorial, looks really thorough. Good job.
You even told us how to pronounce C++...
__________________
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
LOL @ tempest. I always just say "See", then spit twice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Expert Programmer
|
@tempest
thanks ![]() I've heard people say "See add add" and "See Positive" so i thought i'd say how to pronounce it so that people dont make that mistake ![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
When i was a n00b i called C# C-pound... i still do if there's a programmer around me who needs a good laugh.
__________________
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
Lol i thought it was C-Hash before i read any tutorials.
![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Apr 2005
Posts: 96
Rep Power: 4
![]() |
I just found out that "cout" is pronounced "see out" a while ago from my for dummies book.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
i still pronouce it "cowt", it just sounds better to me..
__________________
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i thought C++ was pronounced kuh-plus-plus.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
So who pronounces "char" as in charred steak, rather than as in the first syllable of "character"? Blows my mind every time I hear it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|