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 c

orrect. :)

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 statemen
ts. 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.

No comments:

Post a Comment