ASAP,wrote,tutorial,those,wish,fully,understa
Quick Search for:  in language:    
ASAP,wrote,tutorial,those,wish,fully,understa
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 481,463 lines
 Jobs: 1,069 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
SQL 'LIKE' clause clone in C
By JosÚ Luis Gallego on 10/19


Determing File Version, System Language and Memory Status
By Ixac on 10/19


How to create custom messages
By Xeron on 10/18


DeathBrain
By DeathBrain on 10/18


Click here to see a screenshot of this code!A C++ Tutorial for Complete Beginners #2
By Jared Devall on 10/18

(Screen Shot)

Click here to see a screenshot of this code!_PAGE REPLACEMENT ALGORITHMS (FIFO)_
By ENRICO X on 10/18

(Screen Shot)

extract file from resource and write it to disk
By cpsim on 10/18


Click here to see a screenshot of this code!CurrencyConvert er 2002
By Xeron on 10/17

(Screen Shot)

File Saver/Opener
By Jek on 10/17


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

A C++ Tutorial for Complete Beginners # 1

Print
Email
 

Submitted on: 7/28/2001 3:54:02 AM
By: Jared Devall 
Level: Beginner
User Rating: By 35 Users
Compatibility:C++ (general), Microsoft Visual C++, Borland C++, UNIX C++

Users have accessed this article 26617 times.
 

(About the author)
 
     I wrote this tutorial for those who wish to fully understand things from the beginning. If something is not explained clearly or is incoherent, please let me know and I will try to fix it ASAP!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


I will continue to write these (If you people actually enjoy them.) until I come across things I do not yet fully understand.

First things first. To start programming you will need to get you a Compiler. There are several free as well as a few Commercial ones. Unless you have a few hundred dollars to buy the Commercial Compilers I suggest you get you a free compiler.

Here is a list of a few free compilers for Windows: Linux and Unix come with a C/C++ compiler(I think). Just make sure you installed it and read the manuals!

Okay. You'll need to download one of those compilers and read the manuals on them. I use Dev-C++. If you have some questions using that specific compiler I might be able to help.

Let's get on with the code Shall we?

Windows users: Open up Notepad.
Dev-C++ : Just open it up and click on the new file icon.
*nix: Open up your favorite Text Editor. :)


1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << "Hello World!" << endl;
6: return 0;
7: }


Go ahead and copy that little piece of code. (Starting with #include and ending with the last } ) You can paste it or just type it up in the text editor. Save it as hello.cpp . The .cpp extention lets the compiler know it's a C++ Source File. To compile in Dev-C++ click on the compile icon or Execute->Compile.
Note: The Remove the numbers. They are there only to help analyze the code!
You will have to read the instructions on how to compile it in Compilers other than Dev-C++.


Congratulations! You are now a C++ Programmer!

On Windows Machines the application will close very quickly if you run it. THAT IS OKAY! We will learn how to fix that later.. The point is you got the program to compile! Now let's break down the code.

#include <iostream.h>

The first symbol is the pound symbol. It is a symbol to the preprocessor. (Any words that are links will link to another tutorial with definitions in it. For now it DOES NOT EXIST)
After that is the word include. Together #include tells the compiler to include a file into your program. It is just as you had written it in there yourself!
Iostream.h is called a header file and is included our program. Why? Iostream stands for Input-Output-stream and is needed for cout, which prints things to the screen. iostream.h is surrounded by a < and a >. This tells the compiler to search in the 'include' directory for the file. iostream.h can also be surrounded by Quotations like so: "iostream.h"
This tells the compiler to look in the current directory for the file. iostream.h is obviously not in your current directory so leave the greater than and less than sign there!

Line 2 is just a blank line. It's just there as a part of whitespace.

Line 3 is the beginning of the Actual Program. On it is the main() function. Every C++ program has a main() program. When your program starts it automatically calls main(). According to ANSI Standard we must state main() to be int. This will be discussed in another tutorial.

Line 4 begins the body of the main() function. All functions begin with an Opening brace ( { ) and a closing brace ( } ) just as main() does. Everything inbetween is considered to be a part of the function.

Line 5 is what the program is all about! The object cout is used to print a message to the screen. This is how cout is used:
You write cout followed by the output redirection operator ( << ). The operator is created by hitting shift-comma twice. Everything after the < is printed to the screen. (Or atleast tried to.) until it reaches a semi-colon. ;
endl is way to make the string go to a new line. You also put a \n in the string instead of using endl like this:

cout << "Hello World\n";

If you want a string of characters to be written to the screen you put your text between two Quotations: "Hello world!" like so! You then put a semi-colon at the end signifying the end of the statement. Semi-Colons are somewhat like an English period. It tells the compiler that a statement is over. You'll learn where to put them and where not to put them as we get further into the tutorials.

Line 6 is a return statement. It returns 0 to close the program. It also ends in a semi-colon. This will be discussed in more detail in a later tutorial!

Line 7 is just a closing brace and the end of our program. The closing brace is required to 'close' the function! That's the 'hello world' program fully explained. You can experiment with outputting text by simply making a couple more cout statements.

If anything is to complicated to understand or if I screwed up. >:) then be sure to let me know. Please let me know if you would like anything more, want something explained in a little more detail or whatever. I'll try my best to fix it! - JARED


Other 4 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
7/28/2001 11:17:34 PM:Mark Lu
This is an almost perfect tutorial. I'll give you a rating of five! I love this. Even though I'm half intermedite in C++. This kind of thing starts it out. It would be better if you teach variables, scapnf, fprintf, printfm, and loops. I also need to learn how to make a window's program. Also how to color text, select text position, draw gifs, and buttons, etc.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/29/2001 12:29:21 AM:Jared Devall
I appreciate your feedback. I don't really know much Win32api so I most likely won't be putting a tutorial on that. I'll teach variables in another tutorial. (Just the basics in this one) scanf fprintf and the others are mainly C so I doubt I'll get into those. But I will teach loops. :) -Jared
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/29/2001 12:40:59 AM:fatal
Mark... to make windows applications you have to learn Win32 API, and learn that stuff. Its <b>WAY</b> different than console, just get a good book. <p> And Jarid I thought i was a great example, i will also go with 5. I know my C++ tho, anyway its good for begginers.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/29/2001 5:03:59 AM:Jared Bruni
win32 is easy :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/29/2001 5:04:51 AM:Jared Bruni
I like the way you structured your tutorial. So 5 globes
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/1/2001 2:56:30 AM:Polat Kadir
I like this tutorial very much, and I hope I will find more instructions explained this way. Thank you,Jared!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/7/2001 11:42:12 PM:James
yes, this is kinda embarresing, but i just made i small program using youy tutorial & code & no matter what the program always closes how do i fix that? =) sorry i am a hopeless newbie!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/8/2001 12:34:24 PM:Jared Devall
Well you can open up your dos prompt. Go into the directory that the .exe is in and run it from there. The program will still close but you will be able to see the output because the dos window will still be open. :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/21/2001 8:07:41 AM:winsome pal
please send me more beginner instructons for c++ as iam interested to learn at this mail id- mr_winsome_4u@yahoo.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/15/2001 12:14:06 PM:Cuke
Hey Jared, AWESOME tutorial. did a really good job explaining the basics. I gave it 5 globes. The only problem I can see is you havn't made more yet. :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/7/2001 2:39:08 AM:Beginner
This is the 1st C++ thing I've made as I have only done VB in the past. Visual Basic's capabilities are very limiting so I wanna move on to a more complex language. Thank You!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/15/2002 4:31:24 PM:Eric
This is Great! I'm native to VB and ASP so this realy helps me out for starters! Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/15/2002 4:32:23 PM:Eric
I give you: (glob x 5)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/22/2002 6:30:53 PM:Tiki
Thanks for this great tutorial. I'm taking Basic C. I've never programmed before and the teacher moves so fast. Thanks to you I'm actually starting to understand. If you had loops and bubblesort I could actually get an "A". Thanks again.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/17/2002 5:40:12 AM:Sandro Heinzelmann
Great tutorial... Just one question: i did everything you said and it closes the window to fast when i run it,so i ran it in does. it does something but i can't see the text. It's only a blank line. PS: I am a complete newbie...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/17/2002 5:41:27 AM:Sandro Heinzelmann
with "i ran it in does" i meant i ran it in dos
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/18/2002 7:09:39 AM:chuck otis
it is ausom that someone is willing to have stuff an absolute beginner can start to learn with. I am using win95 and got an error message that said does not recognize the code.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/9/2002 2:19:22 AM:Steel
if you want it to stay open: #include <stdio.h> then at the bottom put a cout: cout <<
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/9/2002 2:23:37 AM:Steel
uhh the whole thing didn't post, if I did something wrong someone let me know lol
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/10/2002 10:23:38 PM:Alan
This tutorial rocks. I'm a newbie, and I don't understand all this confusing code...but I'm learning. Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/11/2002 5:17:37 PM:Tommy
To do a pause at the end of your program (or anywhere else in your program) include <stdlib.h> and then write "system("PAUSE");" w/o the outer quotes where you want a pause. Dev c++ adds this in whenever you create a new file and VC++ (microsoft visual C++) does this without any coding by you (at least it does in version 6.0 which is all that i have used). hope this helps.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/22/2002 9:11:26 PM:Jerry
Where is the second tutorial?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/22/2002 9:44:51 PM:Jared Devall
Hmm? Everyone want and expecting a 2nd one? ( if you do, give me ideas. simple basic stuff... since this is beginner )
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/2/2002 3:43:08 AM:nightwolf
Hey man. Im really glad that u took the time and tought some noobs like me 2 get the most important part - START. Im v. v. grateful. 5 points
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/12/2002 5:49:50 AM:ayham
wondeful words can't speak my mind it's an excelent explaining for the (Hello World)Program no one can explain it much more thank you man.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/19/2002 12:25:12 AM:Devin
I am absolutely, completely new in any kind of programming except HTML. I can't figure out how to compile the code. when I open the Dev-c++, it looks just like the My Computer screen. There is no compile icon and no way to make a new file. Please help.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/19/2002 12:26:26 AM:Devin
But the tutorial was great with the code.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/11/2002 12:17:26 AM:hani
I still get confused with #include<iostream> and #include<iostream.h> What the difference??? Please help me.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/15/2002 9:49:59 PM:(V)enace
for some reason Im getting an error when trying to compile this
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/28/2002 6:34:50 PM:Mngsk
Great tutorial, Im waiting for the second one. I found a Hello Wolrd! sample in Dev-C++ and it is a little different and the window doesnt close. Can you tell me something about it?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/30/2002 7:11:32 PM:Juan C
Good article. It goes to the point.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/18/2002 10:43:12 AM:
I'm just beginning C++ and I've searched online for tutorials but this one by far is the most easy to understand tutorial for beginners. I Know a hell of alot more than I did before I read and studied youre article.THANKS ALOT
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/30/2002 8:06:55 AM:
why does some c and c++ programs always close when you run it? making it impossible to view the results, unless u stick a getch() before closing the program. Any thoughts? Thanx.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/30/2002 10:45:04 PM:
Thanks for this. I'm only thirteen and most of the tutorials out there are using big words like syntext, parenthesis, ect. 5 Stars
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/7/2002 5:54:38 AM:
The tutorial is just fine for the beginner. But if I want to print the messeage " Hello world" to the printer?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/18/2002 12:38:24 PM:
it is a brilliant tutorial and has realy helped me
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.