1.1.5 Program Structure
Last updated
Last updated
Earlier you saw a little about program structure, but let’s look at a few additional details. It is important to understand that C++ is mainly an object oriented language like Java, but it does share some structural language components, similar to Python.
C++ reads from top to bottom, so things like functions need to be defined above their call statement.
Since C++ is not a pure Object Oriented language, you can define variables, functions, etc. outside of any function or class.
You can add comments to your code in a similar fashion to Java, using /* */ for multiline comments and // for single line comments.
Like other languages, a C++ program has a common structure or flow to it. In the first lines of the program, you write any include statements. Libraries from the Standard (std) library are added with the name between <
and >
.
After the import statements, you would then write any namespace
statements. In this course, we will use the using namespace std;
statement for all our programs.
From there, you can now start declaring variables and/or functions. Remember, the order is important, so you will often define variables and functions before the main function. While the main function is where the program starts execution, C++ will only recognize variables and functions that have been seen above in the code.