Let’s look at a simple program and work through it step by step.
#include <iostream>
using namespace std;
int main()
{
// display message to the screen
cout << "C++ is not Python" << endl;
return 0;
}
#include
#include
statements are at the top of the file, and let you include any libraries you might need. iostream, in our example is used to help process input and output. You might think this is standard, but if you are building a library, automated program, etc., you might not have any input or output, thus it is optional.
iostream in this example is inside of angle brackets. That is because it comes with C++ and is a standard library. This header file could be used as something a user wrote, and you wouldn’t see the angle brackets (and would see a .h extension) at the end of the name.
using namespace
Namespaces allow you to define what type of libraries and methods are being called and to help simplify what happens if two or more libraries use the same name. By defining a default namespace, you get avoid conflicts.
using namespace std – std is short for standard, and you will see it throughout the C/C++ language.
cout, which we are using in this program, as well as other commands, are all part of the standard namespace.
int main()
This is our function definition. All functions in C++ follow the same flow.
First is a return type. C/C++ is a typed language, which means every variable had to defined as having a type. This reduces the chances of variables being reused as different types, although it makes us have to control type when we define a variable.
In this case, we will return an integer, so we specify int. If we return nothing, we specify void. We’ll look at other variable types as we move forward.
main – this is the function name. Function names must start with a letter, cannot have spaces, and may include numbers and underscores. Additionally, it cannot be a reserved word. (Does this sound similar to Python’s rules – it’s fairly similar to most programming languages actually.)
As a C/C++ program requires a function called main, this is normal to find.
Parentheses () – here we define the list of parameters that will be passed to our function. In our case, the parentheses are empty, and therefore we are not expecting anything to be passed into our program function. We can list as many variables as we want, but we must list data type and variable name when we do, we’ll discuss this more in the future.
The Function Body
The function body is contained within curly braces. In the book, they use one of two standard methods – there is that they put the braces on their own line. Some people like to put the opening brace on the same line as the function definition. Both will work, and you should use the standard provided to you where you are (class, work, etc).
Note that you do not have to indent for the function body like you do with Python. However, it is very wise to indent to make your code easier to read. By not having to indent, you won’t get an error for having tabs in some places and spaces in others. That is a huge benefit of C++ over Python, as I’m sure many of you can attest to.
cout
cout
is a command in C++ to send information to the console out (cout). To use the command you specify cout << (information to send). You can send strings, numbers, etc.
endl
is short for end line. It moves the console cursor to the new line of the console screen.
Notice how between each variable, static string, etc, there is a set of “<<
“. This is showing that the next element is being added to the output string.
return
The return
command is used to exit from a function. While not required if the return type is void, it is good to always use the return command.
There should be only one return command for a given function if it is well designed. That doesn’t mean that its not easier to have multiple return commands, but it can be harder to debug when you don’t know when you are supposed to be exiting from a function.
As the function header had a return type of int, we need to return an integer. Therefore, the command is return 0;
In C/C++ you can return at most one value from a function. After the return, you cannot execute any other commands. This is normal in most languages.
Semi-colons ;
You might have noticed that after every line of code, there is a semi-colon. This is how C++ knows the line of code has ended. You can technically put “two lines” of code on one, but using the semi-colon, but that just makes code that is hard to read.
Many languages use the semi-colon as a terminal character, so it is good to get used to it.
Since the semi-colon is used to terminate a line of code, it makes it easy to put multiple lines of code as one. If you are going to run out of space, and don’t want to scroll left and right to read your code, just go to a new line. Most people will indent the “second line” a little more than the first, just so it is easy to read and maintain.
A Sample C++ Program was originally found on Access 2 Learn