Intro To C++/First Program

From Wikibooks, open books for an open world
Jump to: navigation, search

Welcome to Intro To C++.

Let's get this show started and give you a very simple example:

#include <iostream>
 
//single line comment
 
/*
multi
line
comment
*/
 
int main() {
    int myNumber;
    myNumber = 72;
 
    std::cout << "Hello World!" << std::endl;
    std::cout << "myNumber is: " << myNumber << std::endl;
    return 0;
}

Try not to act overwhelmed. I will help you understand this

//single line comment. Can you guess what it means? When your computer is making the program, the computer skips over this whole line of code, and goes to the next one.

/*multi line comment*/ Now can you really guess what that means??? If you can't, then you should go pray (or something).

#include <iostream>

This bit up here, it tells the computer to look for a file called iostream. The use of these files is referred to as including (the file(s)).

Next:

int main() {
    int myNumber;
    myNumber = 72;
 
    std::cout << "Hello World!" << std::endl;
    std::cout << "myNumber is: " << myNumber << std::endl;
    return 0;
}

Now lets take this slow. You may be wondering why i am going over the whole thing at one shot, but you will understand. int main() tells the computer to create a new section of the code called main(). Each C++ program has to have one and only one of these. int stands for integer, which in computers, is a fairly large number, and main, well, its main. As for the (), that states that the code under it is a whole section of code. When you first start your program, the program first looks for in main(). Once it finds it, int main() tells the computer what to do from there. Then you see the { directly under it. This states that the code for int main() is going to start.

Now, time to learn some vocabulary! Something you can kinda use as reference:


section/block of code - From now on, a section or block of code will be called a function, since that is what it is actually called.


Computer - More than half of the time that you see computer (the computer looks for int main(), etc, etc.) in this tutorial, it actually means compiler.


Compiler - Takes all of the lines of code that you wrote, and turns that into an actual language that the computer (this time i really mean computer) can read/execute. If you want to get a little technical, the compiler (plus linker), turns your code into a low-level language called Assembly (ASM).

Please note the ;s. Just about every time you make a C++ statement, the ; tells the compiler that it is the end of that line.

Back to coding; Now you see int myNumber;. What that does is it creates an int(eger) called myNumber. So every time you say myNumber, your computer looks at where myNumber is located and uses the info that is in that slot. That is called declaring a variable (when you create a new variable). Then the next line is myNumber = 72;. That tells the compiler that you are going to make myNumber equal to 72. That is called initializing. So that every time you call/reference/ask for the value, it returns 72. By the way, i included a variable for a good reason. Yes, i know, i made a section dedicated to variables, but why am i introducing it to you now? Its not for you to learn about variables. Its for you to better understand cout<<, which is our next topic!


Next is all of the std::cout<< stuff. cout (pronounced see-out) tells the compiler that you are going to print stuff to the screen. cout actually stands for console out, so you're telling the console to send something, or to display it I should say. If i wanted to say trololol, i would type this:

std::cout << "trololol";

Take notice on how i add the quotes around the text "trololol". This tells the compiler that it is actual text that is being printed to the screen. If you don't quote your strings, it will compile with an error about variable not declared.

Now, you see similiar items, such as:

    std::cout << "myNumber is: " << myNumber << std::endl;

We already know that cout<< means to print stuff to the screen, but why is it that you dont have to add quotes around myNumber even though i want to print it to the screen. Simple, because we already declared and initialized it. So when you want to print stuff to the screen from a variable, you just type the variable without quotes after the <<, but still add that ; at the end.

Now, we see std::cout << std::endl; That tells the compiler to create a new line, or end the line, like when you are typing and you press the enter button. Just like that. Then you see cout<<myNumber;. You may be wondering why we typed that again. This will be explained.

Then, the second to last line of the code, return 0;. You know how int main() technically is a number (int = integer), well at the end of each function, you HAVE to return something. The use for this will also be explained at a later time.

Last, but definetly not least, is }. Yep that is the line that ends it all. Once your last } of the file is read, your computer lets out a huge sigh, and falls asleep. Im just kidding. But the last } of your file does tell the compiler that thats it. The program is done.

Now, to see our code in action. Once you are done, you are going to hit compile and run, located in your toolbar. So when you press that, your compiler goes into action, from reading the #includes list, to the last }. Then it turns all of that good stuff into assembly, in which the computer can actually understand. Then when it is done compiling and linking, you will see a window flash open, then close. "OH NO!!! I BROKE MY PROGRAM!!!" you may say. but that is normal. The computer did all the stuff located in int main(), and finished. We never told the computer to pause right there. So, the line before return 0; we are going to add this line of code:

system("PAUSE");

In the window that pops up, in general it is referred to as a cmd (command) prompt, command prompt, and a few other things. From now on, we will refer to it by those names, since that is what they are. If you open your run menu (windows key + r) and type cmd, a command prompt will pop up and say a few things, most noticeably, the last line of words. They should say something like C:\Users\randomname>, randomname being your username that you are logged into. Type in ping 127.0.0.1. Notice how it does a few things, then goes back to saying C:\Users\randomname>. Now type in pause. Notice how it says press something to continue...? And C:\Users\randomname> didn't pop up again? Press something. Did your computer blow up yet? Just kidding. After you pressed something, C:\Users\randomname> popped up again. We can use that pause to pause our application. But typing in pause; isnt going to do anything. Thats why we use system("PAUSE");. That goes to a function called system that we included in the includes list, and when we type "PAUSE" inside the quotes, we send it to the function, and the function pauses the cmd prompt. So go ahead and add that line system("PAUSE"); at the line before return 0;. Compile run, and you should see something like this:

Hello World!
myNumber is: 72
Press any key to continue . . .

So congratulations on making your first cliche "Hello World!" application!

This page is being reviewed. Please, as the reader, don't be afraid to rate the page on the bottom (below this text)!