26286 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Juniper, Seagate
stuff cash down
Cloudscaling"s
OpenStack trousers
FLABBER-JASTED:
It"s "jif", NOT
".gif", says man
who should know
Bunging servers in
disk arrays
achieves nothing.
There, I said it
BMW offers in-car
streaming music for
cross-Europe road
trips
NetApp: We laid off
100s, profits dived
- and it"s all YOUR
fault
Ed Miliband brands
Google"s UK tax
avoidance "WRONG"
Yahoo "won"t screw
Tumblr"? Then
Tumblr will screw
its balance sheet
Slim Shady wannabe
Zuck"s Facebook
"STOLE" MY SONG -
Eminem
Stand back,
everyone! Dragons"
Den ace HAS FOUND
THE CLOUD
Blue Coat gobbles
CCTV-for-network-tr
affic maker Solera
Slashdot
Thousands of
Whistle Blowers
Vulnerable After
Anonymous Hacks
SAPS
Some Scientists
Question Whether
Quantum Computer
Really Is Quantum
Debian GNU/Hurd
2013 Released
Xbox One: No
Always-Online
Requirement, But
Needs To Phone Home
Ask Slashdot:
Moving From
Contract Developers
To Hiring One
In-House?
Quadcopter Drone
Network Will
Transport Supplies
For Disaster Relief
Congressional
Report: US Power
Grid Highly
Vulnerable To
Cyberattack
Google Chrome 27 Is
Out: 5% Faster Page
Loads
Special Ops Takes
Its Manhunts Into
Space
Aurora Attackers
Were Looking For
Google"s
Surveillance
Database
Article viewer

C++ Beginner : The Basics



Written by:Macavity
Published by:thinkt4nk
Published on:2004-11-11 05:42:41
Topic:C++
Search OSI about C++.More articles by Macavity.
 viewed 41853 times send this article printer friendly

Digg this!
    Rate this article :
This is basic and I mean EXTREMELY basic C++ for the ultimate beginner who may be stuck with trying to figure out other probably better c++ tutorials.

My Purpose for Writing this Tutorial

Before you go heading into the real good stuff below (and that was my own sarcasm by the way, can't you see it dripping from the text?) first I hope you'll read this for an understanding of what to expect from this tutorial.

First: I'm writing this not only for the reader but for myself.

I am learning C++ as well. I am, perhaps, a little farther up than those reading this, but the goal for both of us is the same: to further understand and use C++.

I have found that I am capable of learning programming languages and other things of this matter faster and easier by teaching them, it asissts me in providing myself with an understanding while trying to configure an understanding for the reader at hand.

Which is why, I will try to be as in depth into with the subbject as possible, sparing only the details I believe to be unnecessary for the current teaching level. I will probably have other such tutorials, breaking down the parts of C++ as I learn them fully and exhibiting them to you as fully and hopefully, as coherent as possible.

So why the hell am I telling you this?

Simple, if it helps me to learn, maybe it will help you as well, and it s also an excellent way of educating the masses. Now onto the programming....

C++ The General Idea

C++ is an object-oriented programming language, you really don't need to understand this until I go in further detail about classes (in another tutorial coming soon hopefully ;P). But for the curious few who have probably heard the term being carelessly flaunted: it means that the program is built particularly around what you would refer to as objects or created entitities or things entirely alien from the actual program it's in.

Please disregard this, as it is extremely confusing if you have no idea what a class is in the first place.

Programs are not as easy to create as one might perceive. It's like math (don't get too excited ;P) except in letters. Most programmers who don't want bugs (problems, very, very bad problems that can hinder the program from working correctly) right out the program and how they want it to work by way of an algortihm. An algorithm is just a list you could say, of how things are to work and by what way.

For an example: Say you want to order a series of...books!
This is how you would do it:
  • Input the names of all books
  • Input how many books there are
  • Ask how they should be ordered
  • Create a task that selects the correct task to order the books
  • Order the books and then display the now correct order

Not so bad right? Well, for most programmers (not that I would know ;P) they then use pseudocode to get a gist of how the program will look. Pseudocode is just a combination of letters and commands (I don't believe it's extremely necessary to further explain) that are used as a model for the programmer.

There are several ways to design your program-most of which I'm not going to say because I'm becoming very tired of this and I have to study, so I'll just tell you briefly that it is always best to find an algorithm and way to design that's correct for you. It has its benefits....

Alright, finally, before jumping into your first program, let's briefly discuss compilers. Compilers are the translators between you and the computer. The computer can only speak in 1 and 0s, and unless you've got a thing for that or know Assembly (the sheer power of the word makes me shudder) then you're not gonna get anything from just sending the computer a mess of letters.

What the compiler does is translate your commands into corresponding and correct 1s and 0s .But don't get too hopeful, the syntax or the "grammar" if you will, of your commands must be correct or otherwise it's not going to be compiled into 1s and 0s. For beginners, I suggest what I have Dev-Bloodhshed C++! Might I add it is the bomb.

And maybe I'll make a tutorial on that too, I've yet to figure it out besides the basic factors...:p Lastly, the extensions are used differently by compilers: .cp for some is for c programs, .cpp is for c++ programs and .h are for headers. In some compilers, .cp and .cpp can be used interchangeably, but it is best to stick to one to prevent confusion later on.

FINALLY! Onto the example program and hopefully the end: -Do not insert the numbers if put into the compiler-

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


This is the very basic and widely used 'Hello World' program most books will use to assist beginners. Let's discuss this line by line.

The first line is a header file.

A header file is a file included into the program and already contains a set of commands itself that we the 'programmeries' are not required to fully understand the make up. Header files can be created by programmers but some such as iostream.h usually come with the compiler. iostream.h in this example holds the instructions for the command cout which will be discussed momentarily.

int main() is a function. A function (another hopeful tutorial) is a subtask if you will, that does something entirely outside of the program and then brings the value from that other program and inserts it into the program. In this case, int main() is just a basic function for this program to start.

The brackets {} are like red and green lights, the first one turned outwards { means go, the second one at the end of the program } means stop. Brackets are put at the beginning and end of a block of code.

cout Ahh yes, the legendary cout. cout << is a command that tells the compiler to print out whatever comes behind the << (insertion arrows). In this case <<"Hello World/n"; is within quotation marks, therefore it will print out the exact words: Hello World. The /n at the end means newline for and the ; semicolon means the end of this statement.

Notice that all statements (which do not include the header and the one identified as a function) end in a semicolon. The semicolon means that this statement is over, if there was no semicolon, the statement would not be over until one appeared.

Lastly comes the return 0; return is mandatory in this program and is further discussed when dealing with functions. But in this particular program, this block of code (which is what the statements within the parenthesis are) does not output any particular value. Don't dwell to much on the meaning of return 0; it's not direly important at the moment.

What is important is trying out this in your own compiler, -Do not insert the numbers-, and playing around with the cout<< command as it is the first command you should master and completely understand how to utilize before progressing onto others.

Hopefully I didn't waste any of your time. I'm a newb myself as I said and this is not intended for shocking gurus so all flames are dev/null and will not be commented on. But please, if any comments are needed to the positive modification of this tutorial (which means that if you don't like something about it, or if something needs editing or is wrong and you have suggestions) please send me a message or comment on it. I'd appreciate that. ;P

Did you like this article? There are hundreds more.

Comments:
TroPe
2004-11-11 13:58:53
Great work. Nice overview.
47thsnake
2004-11-21 13:25:15
Good, acurrate attention to detail.
robo1st1
2006-01-15 08:58:06
you are learning from"Teach yourself C++ in 21 DAY"....why don't you put a link so that everybody could download the file?...it will be much easier
shree
2006-10-01 13:23:23
its great
Anonymous
2007-12-12 01:59:05
what
Anonymous
2007-12-12 02:00:46
eat butt and suck a sack.
nimirjaleel
2008-02-03 16:53:17
Nice.....but more details required
Anonymous
2008-02-12 06:19:19
good job but it would help if u inclided how to use the compiler
Anonymous
2008-05-07 00:39:17
You know Assembler (basic ASM) isn't scary at all, I can understand the baics of ASM but not C++. Go figure.
Anonymous
2008-05-25 18:49:20
wat does "and" "=" "|" mean in c?
Anonymous
2008-05-27 18:09:16
goooooooooooooooooooooood
Anonymous
2008-08-04 04:25:04
Good tutorial. I have one question..... When I try to run the program I get this message:

------ Build started: Project: Ricks, Configuration: Debug Win32 ------
Compiling...
Ricks.cpp
c:\documents and settings\rick\my documents\visual studio 2008\projects\ricks\ricks\Form1.h(99) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
Build log was saved at "file://c:\Documents and Settings\Rick\My Documents\Visual Studio 2008\Projects\Ricks\Ricks\Debug\BuildLog.htm"
Ricks - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I'm completely new to C++ (Been using Delphi and VB forever) and I'm not totaly sure where I'm suppose to add this code in at. I pasted the code right inside the buttom1_click (minus the numbers). I also get 120 (no joke) errors anytime I try to include the <windows.h> file. Maybe you can help me out a little. BTW I'm using MS Visual C++ 2008 Express.

My name is Rick, and you can email me at wannagethuge@gmail.com. Or AIM at original m0rtis.

Thanks for any help in advance.

thinkt4nk
2008-08-04 15:24:29
I haven't done any C++ work in VS, but the compiler can't find iostream.h, a legacy module that deals with char I/O. try include <iostream>
Anonymous
2008-10-06 10:23:44
Hi

Thanks for the tutorial, really helpful.

I'm a beginner Game Designer, I have to learn and become pro at openGL/C++ in 2 years, also in future I would like to make a studio.

so I need to know people around london or close by, to join them for a successful future.

please add me at online_agent_2008@yahoo.com

I would like to chat with some one with such talents:)


Bye
Anonymous
2008-12-22 05:28:40
Great starter tutorial man, Very accurate and in-depth explanation of what each line does and how it works.

I've just started trying to learn the basics of C++ (My first attempt at some coding) but all of the other websites I found assumed you had previous knowledge of some coding languages...

You should do some more of these tutorials :)
Thanks again..
Anonymous
2009-02-12 15:41:20
What a waste of time lol, you don't know shit
Anonymous
2009-03-18 10:51:10
Nice Work! Finally Someone Explain's The "Basic's" Of C++
Anonymous
2009-03-20 14:49:31
Is there any tutorial available related to the use of header files in C++

please let me know, anam.alvi2@gmail.com
Anonymous
2009-04-22 08:37:29
hi very nice
Anonymous
2009-07-04 20:36:51
Nice tut. There is a slight problem in the code though. You must add "using namespace std;" at the top or use std::cout.
Newbies (like me) would have a problem compiling and would receive a compiler error if typing the code as shown. Otherwise great job.
Anonymous
2009-09-17 01:08:18
I didn't get any errors doing it the way he showed but it just flashes on and off when running i tried using the getchar command but it didn't work!
Anonymous
2009-09-17 01:18:05
It does run through command prompt though
Anonymous
2009-10-21 12:28:19
nice one
Anonymous
2010-02-07 12:46:21
Top marks .. this has helped me big time ....Martin Bristol uk
Anonymous
2010-03-01 14:49:36
very good tutorial man
Anonymous
2010-03-07 23:20:51
no comment
Minnie02
2010-11-14 03:48:23
Thank you dude this is awesome sauce. It's cool that you explained every line, which is what I've been looking for like CRAZY. And you made it interesting with your "dripping sarcasm" :D
Anonymous
2011-01-21 10:04:02
nice work mate.... would love to see more of c++ tutorials and examples, but great foundation to start c++ programming ;-)
Anonymous
2011-03-13 19:20:35
Nice work! Yeah, the reason I believe the command prompt only appears for a few seconds is because the program is finished with the line "return 0;'=" If you want the command prompt to stay indefinitely so you can check everything work okay, replace "return 0;" with system("pause");
Anonymous
2011-03-23 20:55:24
great tutorial but i got an error about an antiquated or deprecated header: (iostream.h)?
Anonymous
2011-04-20 07:48:36
C++ is hard :P - Dumbass American kid
Anonymous
2011-04-20 07:49:51
@Dumbass American kid ....C++ is not hard. Its just your dumbass brain with IQ 0
Anonymous
2011-04-20 07:58:30
me has 2 learned c++ in 1 months. who book did me uses? has somebody helping i. sorry english me bad broken .
Anonymous
2011-06-16 08:50:29
Thank you for the explanation. Good job.
Anonymous
2011-06-30 21:36:56
if yuo're getting the error saying that you ca't do it because of <iostream.h>
try only typing in #include <iostream>
some of the newer VC don't need/want the ".h" for some reason, i think 2008 and up.
Anonymous
2011-07-04 18:54:15
good imformation
Anonymous
2011-07-04 18:56:50
hope you will be sucsesful in life
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
harry
Blog entry for Thu 28th Sep 12pm on Thu 28th Sep 12pm
Hi In school i want to net send my mates but hide who its coming off any ideas. no programs though as the machine in school sets off an alarm if it detects any batch files etc... thanks Harry


     
Your Ad Here
 
Copyright Open Source Institute, 2006