Introduction
A short introduction to C++ programming
Introduction
This will be a quick tutorial explain how to make a C++ program. The basic steps are to:
Depending on what tools you are using to create your program, some of this might be automated by a development environment or you might have to do them manually. Either way works and the advantages of one over the other is not that important at this stage. Use what you have.
If you don't have anything, there are free and commercial compilers available. Get one and get busy.
Some of the representations of the computer I will make will be non-exact. Where things are really stored are a reflection of the hardware that will be running your programs. There are some complex orgaizational things that you get into later as you get more advanced with the program but at this level it is un-important to know exactly where things are.
Let's start with a minimalistic program that is not the traditional first program.
The formatting of this is not the normal way it is written but you should look at it this way first. There are three things on the program above. The first is the keyword int. This says that when the program finishes, it will give back an integer as a result of the program. The second thing is the function name main(). The name "main" is the special function name that marks the starting place of your program. The parenthesis do two things. One is to signify that it is a function and the second is to hold things that you send into the program which we are not using right now. The last thing is in our program is a block of code. A block of code is the braces {} and everything inside it. In this case, there is one statement inside the block of code. It is return 0; The keyword return is the command to stop the program and send whatever is on its right back as the result. The semi-colon " ; " is the end of statement marker.
A more normal writing of this program would be organized a bit differently to make it easier for people to read. It does not make any difference in the way the computer works.
The idea behind formatting the code this way will make it easier to read and follow the logic of the program.
One last way to write this program will add the final missing part. When you write a program, you only want to solve the problem once. So you put the information about the program inside the program and mark it so the computer ignores tex extra information. It is for people, not the computer. They are called comments. In C++ there are two ways to mark comments. The first is a double slash mark // This means for the compiler to ignore everything from this mark to the end of the line. The other is for longer comments and is marked with the pair of symbols /* ignore me */. So our final first program is written like this:
In the next section, we will expand this by making a program that actually does something.
This will be a quick tutorial explain how to make a C++ program. The basic steps are to:
- Edit a text file with the instructions you want the computer to carry out
- Run the compiler to translate your instructions into machine instructions
- Run the linker to create a program from the translated instructions.
Depending on what tools you are using to create your program, some of this might be automated by a development environment or you might have to do them manually. Either way works and the advantages of one over the other is not that important at this stage. Use what you have.
If you don't have anything, there are free and commercial compilers available. Get one and get busy.
Some of the representations of the computer I will make will be non-exact. Where things are really stored are a reflection of the hardware that will be running your programs. There are some complex orgaizational things that you get into later as you get more advanced with the program but at this level it is un-important to know exactly where things are.
Let's start with a minimalistic program that is not the traditional first program.
int main() {return 0;}
The formatting of this is not the normal way it is written but you should look at it this way first. There are three things on the program above. The first is the keyword int. This says that when the program finishes, it will give back an integer as a result of the program. The second thing is the function name main(). The name "main" is the special function name that marks the starting place of your program. The parenthesis do two things. One is to signify that it is a function and the second is to hold things that you send into the program which we are not using right now. The last thing is in our program is a block of code. A block of code is the braces {} and everything inside it. In this case, there is one statement inside the block of code. It is return 0; The keyword return is the command to stop the program and send whatever is on its right back as the result. The semi-colon " ; " is the end of statement marker.
A more normal writing of this program would be organized a bit differently to make it easier for people to read. It does not make any difference in the way the computer works.
int main()
{
return 0;
}
The idea behind formatting the code this way will make it easier to read and follow the logic of the program.
One last way to write this program will add the final missing part. When you write a program, you only want to solve the problem once. So you put the information about the program inside the program and mark it so the computer ignores tex extra information. It is for people, not the computer. They are called comments. In C++ there are two ways to mark comments. The first is a double slash mark // This means for the compiler to ignore everything from this mark to the end of the line. The other is for longer comments and is marked with the pair of symbols /* ignore me */. So our final first program is written like this:
/*
This program does nothing. It is just a simple program that has the basic parts. It will run and immediately end
*/
int main ()
{
return 0; // Terminate the program and send a 0 as the value
}
In the next section, we will expand this by making a program that actually does something.