Before we write our first program in C++ we need an IDE (Integrated Development Environment) and a C++ compiler.
We could use notepad to write out code but an IDE would be better due to its additional functionality. I personally use VS Code. You can use any IDE you like.
What is an IDE?
An integrated development environment (IDE) is a software application that helps programmers develop software code efficiently. It increases developer productivity by combining capabilities such as software editing, building, testing, and packaging in an easy-to-use application
What is a Compiler?
A compiler is a special program that translates a programming language’s source code into machine code, bytecode or another programming language. The source code is typically written in a high-level, human-readable language such as Java or C++.
IDE’s for different Operating Systems
Here are a few IDE options for different operating system
For Windows:
For Linux:
For Mac:
Compilers for C++
Here are a few examples of C++ compilers available on Windows,Linux and Mac
In practice it would be a good Idea to have multiple compilers installed on your device.It would help to run your code on multiple compilers and figure out how it would respond on a device having a different compiler.
You can check which C++ compilers support which C++ feature by clicking here.
Note: We will not go the installation process over here.
Our first program : Hello World!
Now,after downloading and setting up our device we will write our first lines of codes.
#include <iostream>
using namespace std;
int main(){
cout <<"Hello World";
return 0;
}
This code will should result in an output:”Hello World”
To run this code we will type the following command in our terminal :
g++ <file_name>.cpp
This will compile our code and creat an .exe file in the folder
To run this .exe file we can use the following command:
.\<exe_file_name>.exe
To create an .exe with a specific name we can use following command while compiling our code:
g++ <file_name>.cpp -o <name_you_want_to_give>
Code Explaination:
#include <iostream>
The above line adds a header file that allows input and output operations.
A header file in C++ is a file that contains declarations (such as functions, classes, and macros) that can be included in multiple programs. It typically has a .h
or .hpp
extension, though standard C++ headers (like <iostream>
) don’t require an extension.
Note: We will discuss more about header files in future.
using namespace std;
This allows us to use names from the standard library without prefixing them with “std::”. Without using the standard library to print “Hello World” we have to write the code like this:
std::cout << "Hello World";
More details regarding standard libraries will be discussed in the std libraries section.
int main() {
This is the main function where the execution of the program begins.We will discuss more about functions in Object Oriented Programming Section.
return 0;
return 0 indicates that our program has been successfully executed.