C++ Lesson 4 – Get ready for the first program

C++ Lesson 4 - Get ready for the first program

If you haven’t studied our C++ lessons yet, look below.

C++ full tutorial | How to start C++ | Part 001

C++ Lesson 2: Setting the PROGRAMMING Background for WINDOWS

C++ Lesson 3 – Setting the background PROGRAMMING for LINUX.

I think lessons learned in the past have made your computer’s development environment. This means that you are already able to create and run a program.

In this lesson I hope to talk about these things, we know that when we create a program, we have to write the source codes. This source code is writing using that vocabulary. So let’s talk about some of the recurring concepts in this vocabulary and how a program is structure by the main function and how to add comments to a program. And a little guide on how to think likes a programmer.

Let’s start with a simple program.

This is a very simple program and we cannot see any output in this.

int main ()

{

}

Let’s see what the first line does here.

int main ()

Basically this means that there is a function and its name is main. And this main function is the only function in a C++ program. Another thing is when you run the program that starts with this main function.

Curly braces { }

 Display the start and end points of the program. You may think this is the beginning and end of the program. Likewise, there is nothing between the two braces and the function means nothing. This means that when you run this program, you will not get any output. Well, now let’s go a little further.

#include <iostream>

using namespace std;
int main ()
{
      cout << “HEY, you, I’m SecDevi.com, and Hello World!\n”;
}

First of all, as you can see, this program has a line between two braces. This means that this program has an output.

Let’s start with the first line.

#include <iostream>

This statement includes tells our compiler to add a header file called iostream to the source code before running the program. These header files are part of the code provided with the compiler. In the Borland compiler, these files are located in the Include folder. #include Using this command we can add the whole header file to our source code. It is therefore through these header files that we add functions to our program.

Whenever we want to access the functions, the first thing we need to do is to include the header file with the corresponding functions in our source code. iostream is a header file which has many functions. So you can look at these included statements and what we do.

The line behind the include statement is:

Using namespace std;

This is calling the boilerplate code. This is including in every C++ program. Remember, this is exactly what you want below the statement includes at the top of the source code. We’ll talk about it later exactly what happens with the boilerplate code. Then add this line immediately without forgetting.

And see, at the end of this line, a semicolon has been added. Adding a semicolon is part of the C++ programming syntax. This semicolon is use to tell the compiler where an instruction ends. One of the biggest mistakes made by most new programmers is forgetting to apply this semicolon. So if your program isn’t working properly, the first thing to see is how to apply the semicolon correctly.

Well, then we have the main function, which is where the program starts:

int main ()

This is our next line.

cout << “HEY, you, I’m SecDevi.com, and Hello World!\n”;

In C++, the cout object is use to display text. We have included the iostream header file to use this cout.

In the cout object we use the << symbol to show our output. In short, the result of this (cout <<) is to present a function or a line of text. There are some programming cases that we cannot give a correct idea in Sinhala because if they do, they will change their mentality. I can’t even explain it in English because it’s programming in Sinhala. So I hope you understand this.

We usually call the result of a function like this as an argument. These functions take arguments for that function from its code. In this case you see that this argument string is the function argument.

“Text” – This is where the compiler cites the text string as output here. The next important one is this newline sequence (\n).

Newline sequence (\n) – Works in C++ as a single character. This is basically like when you press the Enter key, the cursor will move to the next line.

And as you can see, in the future we will use a special value called endl. This is exactly what happens if you give an example:

cost << “Hello” << endl

cost << “Hello \n”

As you can see, the first line I used is the value of end1 and the new line is replace by the new line character. We will talk about their further use.

And at the end of the line, you see, I used a semicolon. We need to apply this semicolon as a function.

At the end you have to close the function using the brace. You should definitely run this program and not copy and paste this little program. Because it helps you get to know the little things better.

So don’t just run this program, you can change it and see what other lines are use. Thinking & Speaking is the best way to learn a language, so Thinking & Executing is the best way to learn a programming language.

What happens if your program is not displayed?

Depending on the operating system and the compiler you are using, what happens if you run a program that flashes before the program gets close enough to see the result? If you use the environments I explained to you in the previous lessons, you won’t get this problem, but if you run a different program, you can run into this problem. You can solve it by adding this line to the program.

cin.get();

This is exactly the end of the program. This will keep the program from closing until you press a button. So you can take care of the result.

Leave a Reply

Your email address will not be published. Required fields are marked *