Basics of a C++ program

NOTE: This article is part of a C++ guide, therefore only C++ code will be displayed.

structure of a C++ program

Like Java, every C++ program has a specific structure required to make it function. When making a program we need a function int main() like so:

int main(){
    // main code goes here
}

Standard library

By default our program has limited functionality, but we can access more features by including libraries. As we introduce different concepts we will introduce the library that goes along them as well.

simplyfying syntax when using the standard library

Here is a basic C++ program (we will learn these concepts later):

#include <iostream>

int main() {
    int a,b,c;

    std::cin >> a >> b >> c;
    std::cout << "The sum of these three numbers is " << a + b + c << std::endl;
}

Whenever we are using anything from the standard library we have to prefix it with std::. This can become tedious, especially on longer programs. We can avoid this by adding the line using namespace std; at the beginning of our program, like so:

using namespace std;

int main(){
    // ...
}

Running C++ programs

Programs can be used either through online IDE's (Integrated Development Enviroments) or local IDE's. Setting up a local IDE is more involved but can provide a better programming experience, while online IDE's have no setup.

Here are some options for Online IDE's:

Additional Resources