Programming and C/C++ Basics
This page is a guide on how to start a new project. This section will cover several concepts and basic programming knowledge. I invite you to reproduce the examples on your own even if they are simple and modify them to learn by observing the behavior of your program.
Creation of a project
What is the #include?
The namespaces
The standard library
What is a function?
The use of comments
Consultez la version francophone de cette page.
Don’t hesitate to provide us with your feedback, comments and suggestions on our content. Contact us at info.crc@sciencetech.ca.
My first project
I invite you to listen to the following capsule to learn how to create your first project and write your first program!
What is the #include?
The #include allows you to add source files to your own file. And now, some of you will say, "Yes, but still....". The goal is actually to split your code and make it easier to maintain and reuse. When you put a #include at the beginning of your file, when it’s time to compile, the compiler will add the included file to your file.
In the code that follows, I modified the previously made project to split it into 2 files and show you a simple example of how a #include works. We can see that, in the following example, the main() function of the HelloWorld file calls the helloWorld() function of the Message file. The Message file is included at the beginning of the HelloWorld file and will therefore be added under this line when it’s time to compile.
HelloWorld.cpp
#include "Message.cpp"
int main(){
helloWorld();
return 0;
}
Message.cpp
#include <iostream>
using namespace std;
void helloWorld(){
cout << "Hello world!";
}
File upon compilation
Note that I didn't insert the code of the #include <iostream>, because this library is simply too big and would ruin the example.
#include <iostream>
using namespace std;
void helloWorld(){
cout << "Hello world!";
}
int main(){
helloWorld();
return 0;
}
The namespaces
Namespaces are used to allow the user to have variable names, function names and much more with identical names. When you write using namespace std, you remove the need to specify from which namespace the variable or function you are using comes.
In the following example, we will create 2 namespaces that we will use in our main.
main.cpp
namespaceA.cpp
namespaceB.cpp
Console output:
You can see in the previous example that we declare namespace A as the namespace used in main.cpp. Then, when we call the helloWorld() function, the program takes the function in namespace A. When we call the helloWorld() function in namespace B, we need to add B:: to tell the program which namespace to use. The same behavior can be observed for the standard library when using the cout function.
The Standard Library
C/C++ uses the standard library hence the name std. The standard library is a collection of general and very useful functions. In fact, when you include iostream, you include part of the standard library! If you want to discover the multiple functions of the standard library, I invite you to click here and have a look.
What is a function?
The function is a kind of “subprogram”, a "subroutine". It is mainly used to avoid code repetition and to make a program easier to read and maintain. Several tips can help when creating a function.
Make a function that performs a single task
Give it a representative and clear name
Minimize the number of arguments
Here is an example of a function call that multiplies 2 integers (int) and returns the value of an integer product (int).
Return value type
Name of the function
First argument type
Name of the first argument
Second argument type
Name of the second argument
The Use of Comments
Although comments are a good way to document our code, we must avoid abusing them. It should never be necessary to comment on the code line by line. Often, this practice is due to a lack of clarity in variable and/or function names. Sometimes, functions that are too long and that do several actions at once can be the cause of this problem.
Here are some cases where the use of comments is relevant:
In order to provide information on copyright, the usefulness of the file, its version and others.
This information is always found in the header of the file.
The information on a function - either a small explanation of the purpose of the function, the variables in parameters as well as the return variable.
Leave a note temporarily and remove it once the correction is made.