Friday, September 10, 2010 10:36:26Login · Register
 

    Challenge Activity
09:22:09 - l0gan_l135
     - Completed real [2]
07:42:10 - l0gan_l135
     - Completed real [3]
05:55:17 - aspen_23
     - Completed recon [1]
02:27:21 - GemaRastem
     - Completed basic [2]
02:26:21 - GemaRastem
     - Completed basic [1]
02:21:00 - GemaRastem
     - Completed recon [3]
01:55:39 - GemaRastem
     - Completed recon [2]
01:43:45 - GemaRastem
     - Completed recon [1]
01:31:59 - veerendragautam2009
     - Completed decrypt [3]
01:26:45 - veerendragautam2009
     - Completed decrypt [2]
01:25:16 - veerendragautam2009
     - Completed decrypt [1]
12:31:00 - veerendragautam2009
     - Completed basic [1]
12:26:04 - veerendragautam2009
     - Completed recon [6]
11:13:29 - veerendragautam2009
     - Completed recon [3]
10:59:23 - veerendragautam2009
     - Completed recon [1]
09:30:05 - sirEgghead
     - Completed real [4]
 

    Scoreboard Top 20
UserPoints
Abhineet4795   
auditorsec4795   
ne0114795   
Null Set4795   
blandyuk4780   
bluechill4750   
Teddy4730   
TurboBorland4475   
Qwexotic4460   
tiiger11114205   
preet4180   
LiquidFusi0n4175   
OnlyHuman4125   
samthg4110   
satishek3900   
pilchdragon3660   
Override3655   
chronic123640   
sirEgghead3625   
dash803590   
 

    Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
 

    Users Online
· Guests Online: 8

· Members Online: 0

· Members on IRC: 10
Xires, TurboBorland, ryan1918, Polynomial, louve, LK, IFailStuff, epoch_qwert, connection, bubatime

· Bots Online: 0

· Total Members: 1,491
· Newest Member: elostaz3omda
 

 

 

 

    Top 10 Forum Posters
UserPosts
bluechill918   
Qwexotic699   
cruizrisner487   
Null Set363   
TurboBorland335   
madf0x311   
Stormc1nd3r308   
auditorsec302   
Override238   
jakecrepinsek235   
 

    Affiliates
 

C++ Tutorial 2: Writing Our First Program
     
C++ Tutorials: Writing Our First Program

This tutorial builds on things discussed in C++ Tutorials: Basics, please go read that tutorial if you do not know basic C++ types such as char, string, int, double, and floats.

Now, Writing Our First Program!

Ok so I'm just going to hit you with it:

Code

#include <iostream>

using namespace std;

int main()
{
    cout << \"Hello World! This is my First Program!\";
    return 0;
}





Ok right there is a lot of code and a lot we haven't talked about. So lets break it down line by line:

Code

#include <iostream>





This line here does 2 things (technically 4):

# is used to tell the compiler to do something

include is used to tell the compiler that this file references functions, classes, namespaces, variables, etc. defined in another file (a header file)

Now you can either use <> (brackets) or "" (double quotes). Brackets are mostly used for referencing a file not in your project and double quotes are used for header files defined in your project.

iostream is a header file for the I/O library. (google it if you don't know what this is). In C++ system header files do not use the .h (unlike in C).

So this line does the following:

It tells the compiler that this file needs the header file iostream.

Now why do we need this header file?

We need it because of cout (more on this later)

Code

using namespace std;





hmm.... can you guess what this line does? If you guessed that this line tells the compiler that the following code uses the namespace std (so you don't have to do eg. std::cout), then you are correct. :)

So

using tells the compiler that the code is using something

namespace is the thing it is using

and std is the name of the namespace (std = standard library, for standard library functions and classes (like string :) ) )

Code

int main()





Now you may be wondering the following:

Why doesn't this line have a semicolon after it?

The reason is:

This line is the definition of a function, main in this case.

the functioned called main is the start of every program. It is called by the OS when the program first runs. (normally main isn't defined as int main() but as int main(string[] args), more on this later)

int tells the OS (and the program for functions that are used in the program) what this function should return.

The main function of a program always (well, almost always anyway, more on this later) returns an int. This int is used to tell the OS if an error occurs. 0 means no error, and -1, 1, 2, -50, 1000, etc. is used for telling the OS there is an error (and the program there was an error in a function).

A function can also have the following types:

void (returns nothing)

int

float

double

char

ClassName (string, vector, your own class, etc.)

A Function can also have unlimited parameters so

Code

void someFunction(parameter A, parameter B, parameter C, parameter D, parameter E, parameter F,etc..)
{
    //Do something
}





Example Functions:

Code

int GetMax(int FirstNumber, int SecondNumber)
{
    if (FirstNumber > SecondNumber)
    {
        return FirstNumber;
    }
    else
    {
        return SecondNumber;
    }
}





Now if you notice there is something we haven't covered yet. It is called an if statement.

An if statement works like the following:
Code

if (condition)
{
    //then do something
}
else
{
//then do something else
}




There is also another version:
Code

if (condition)
{}
else if (anothercondition)
{}
else
{}





There we have an else if statement. Which is used if you want to combine multiple if statements. Also if an if statement is true it will not check the others so nesting if statements and combining them is a good idea for saving cpu and memory usage.

In this tutorial we have covered functions, the main program function, and if statements. In the next tutorial we will look at classes and switch statements.
 
Comments
 
#1 | Killordie on 01/24/2010 09:48
Thanks for a fine example of code.
This will be sure to help me on my way once I get started.
-- edit --
I am sure I posted this code under teddy's task kiler...
#2 | Qwexotic on 01/29/2010 13:09
Nice tutorial; I fixed up the [code] issues.
#3 | PhAntom H@x on 02/11/2010 22:58
Lol. Wow I remember when I used If Else statements in VB. Speaking of which Ima write a program in it tommorow For fun. Pretty easy stuff. Thanks for this, again, nice work.
#4 | Torrment on 04/03/2010 04:39
nice tut the backslash outside of the quotation marks causes an compile error i also had to add a system pause to see the out put before the console window closed plus the n for the new line

i guess it depends what compiler your using im using dev c++
 
 
Post Comment
 
Please Login to Post a Comment.
 
 
Ratings
 
Rating is available to Members only.

Please login or register to vote.

Awesome! Awesome! 0% [No Votes]
Very Good Very Good 50% [1 Vote]
Good Good 50% [1 Vote]
Average Average 0% [No Votes]
Poor Poor 0% [No Votes]