To write your first C++ program, please open the Notepad application(which comes preinstalled in Windows Operating System) and
type in your first C++ program explained in this article.
Although you may also use Integrated Development Environment(IDE) application like Eclipse, NetBeans to create your first C++ program but for a beginner in C++, we would recommend using Notepad because you learn so much when you work to find your own mistakes to fix them
rather than an IDE finding them for you.
Important points about a C++ program
Every C++ program starts with including header files containing library of functions to be used in the program.
Every C++ program starts with a main() function.
Every statement in C++ program must end with a semicolon ;(unless it is a comment)
A main function defines a single or multiple set of statements to execute within opening and closing braces {}.
How to name a C++ program?
The name of a C++ program may start with any alphabet or even a digit but it must ends with a .cpp extension, where cpp stands for c plus plus.
Creating our first C++ program
We are creating a C++ program which is saved in a file named First.cpp.
First.cpp
#include<iostream>
using namespace std;
int main()
{
cout<< "Hello World!";
return 0; //returning an int value before the main() function ends
}
Advertisement
Understanding your first C++ program
Let's see what we actually did in our first C++ program by dividing it into a few segments -
#include<iostream>
Every C++ program starts by including header files containing the definitions of the library of functions to be used in the program.
In this program we are going to include header file iostream, which contains definition of ostream class, which is a class of the cout object,
using which we could perform the output operation.
Note : The iostream also contains definition of istream class used to perform input operation using its cin object.
using namespace std;
Every C++ program that requires to perform input and output operations, must use an already defined namespace, stdi.e. an abbreviation of standard,
which tells the compiler to look in the std namespace(namespace can be understood as a container for a set of classes), in order to use cin or cout variables and perform input-output operations.
Note : If we don't mention the statement using namespace std and still want to use cout and cin variable then we have to directly call these variables by
using the std namespace.
std::cout or by std::cin.
where :: is a scope resolution operator(advanced topic).
int main()
A C++ program begins with a call to the main() function, because it is an entry point to our program.
This main() function is always defined with an int return type,
which means that it is going to return an int value at the end of its execution.
cout<< "Hello World!";
This statement simply outputs the string Hello World! in the output on the command prompt, where cout is a predefined object of ostream class defined in the header file iostream,
hence, we have included iostream header file at the top of our program.
Note : The cout object prints the string of characters(within double quotes) at the command prompt in the output of the program.
return 0; //returning an int value before the main() function ends
This statement returns an int value zero(0), at the end of execution of main() function and ends with a semicolon(;)
Next, we have added a comment at the end of this return statement.
A single line comment in C++ is defined within the opening element //.
How to compile a C++ program?
In order to compile our program, we need to call the C++ compiler. As we have installed and configured MinGW GCC C++ Compiler,
hence we are going to call this compiler
with a command g++ with the name of our file.
We have saved our C++ program in a file First.cpp in the D: Drive location. Hence, in order to compile our First.cpp file from command prompt, first we need to reach the
location where this C++ file is saved and type in the command -
D:\>g++ First.cpp -o First.exe
This compiles the file First.cpp and creates its .exe file with the name - First.exe.
This .exe file is a compiled form of our program, which is going to be required and read in order to execute this C++ program.
How to run a C++ program?
D:\>First.exe
Hello World!
In order to run our program, we need to execute the .exe file with the name of our C++ program.
And, at the end of this command, we press Enter to get the output of our program.