Heyy young programmer, Welcome to another insightful lesson from Noob Code Pro!!!!! Today I have got a complete guide for you to create your very first C++ program. Isn’t it exciting??? Executing your first C++ program is not only going to give you a good introduction to the basic C++ structure, but is also going to test if you installed C++ correctly in the last lesson.
If you come across any sort of error, you can drop a comment below, ask a question on our telegram community or simply google the error, and you will find the fix.
With that being said, let’s get into this lesson ☺
CREATING A CPP FILE:
The first step to creating any program is creating a file. So, in order to run C++ commands, we must create a ‘cpp’ file which stands for ‘C Plus Plus’.
You can do this by right-clicking anywhere on your Desktop, creating a text document and saving it with a .cpp extension.
Your cpp file is ready and now you can execute your C++ commands in it, but for that you need an IDE. In this lesson, we will use Sublime Text 3, which is a very famous IDE among programmers and my personal favourite, to run our very first C++ program. (If you want to learn how to install Sublime Text 3 in your computer follow the steps mentioned in the last section of this article)
Now, all you need to is open the cpp file we created in the previous step with Sublime Text 3.
WRITING A C++ PROGRAM:
Once you have opened the file then you can start typing your code in it. Don’t worry….I don’t expect you to create a whole video game. This is your first lesson, so I’ll be guiding you through creating your program. We will be creating a simple program that can print any sentence.
But before we start creating our program, a good habit to develop in programming is to add comments to your code. Comments can help you understand and debug (fixing the bugs in your program) the code better in the future. In C++ you can add a comment by using two forward slashes (//) followed by the comment. The forward slashes tell the system that the line is not part of the program and should be ignored during execution.
Since we have got that out of the way, here is the end result of the program we will be working on in this lesson:
Let us break the code down line-by-line:
Line 1: In the first line we will be importing a file called ‘iostream’ which expands to ‘Input Output Stream’. This file is imported whenever your program requires to take input or give an output (which is almost every program). By ‘importing a file’, I mean that we can use the code/functions written in that file in our program. So, in our case the file ‘iostream’ contains the code that enables a C++ program to take input and give output. We can import this file using:
#include <iostream>
The keyword ‘include’ tells the C++ compiler to include/import a file into our program. We specify the name of the file we want to import/include between the ‘<>’ signs.
Line 2: In the next line, we specify a ‘namespace’. Namespaces are like sub divisions in a C++ program. So, in the first line we specified a header file (iostream), which we will be using in our program and in this line we are specifying what part of the header file we are referring to. It’s like we are telling the computer to focus on a particular paragraph of a book, where book is the header file and paragraph is the namespace. There’s definitely more to namespaces but this should give you an idea of what we are doing here. You’ll learn more about them in the upcoming lessons. In this lesson, our main focus is to write a C++ program and make it work.
using namespace std;
Notice the semi–colon(;) at the end? Now there are two important notes to take from here.
Important Notes:
1) Every statement in a C++ program should end with a semi-colon
2) Not every line of code is a statement. (You’ll know what this means as we go through more of C++ programs, so don’t worry ;) )
Line 3-6: The last part of the code is defining a main function. This is the block of code that’ll execute the commands required to print a statement. The curly braces {} in Line 3 and Line 6 mark the beginning and end of the main function respectively. Between the braces, you write the line to print the statement and tell the computer if the commands were executed successfully or not.
cout << ‘Hello, World!’ << endl;
The above line prints the statement ‘Hello, World!’. ‘cout’ is the object/keyword that basically tells the computer that you want to print something. ‘<<’ is the insertion operator. Think of it this way, ‘cout’ is a window and you are pointing towards where you want your commands to be thrown out of (Outputting, in other words) using the insertion operator (<<). The ‘endl’ keyword depicts the end of the line.
return 0;
Our next statement simply tells our system that everything is fine. Notice how both these lines end with semi-colon.
Now, your program is ready to be built and executed. Since we are using Sublime Text 3, it will build and execute the program simultaneously. If you are using some other IDE, you might have to build the program and then execute it separately.
In sublime text 3, simply press Ctrl + Shift + B to open the ‘Build With…’ menu and select ‘C++ Single File – Run’ from the drop down list. This needs to be done because sublime text supports multiple languages and you need to tell Sublime Text which language to use when executing a specific program.
That’s it…your program should execute and print ‘Hello, World!’
CONCLUSION:
This should give you a good understanding on how a basic C++ program works! A quick recap, we learnt about the ‘iostream’ file, namespaces (which we will learn more about in the upcoming lessons), main function, ‘cout’ keyword, the insertion operator (<<) and the ‘endl’ keyword. Well that’s too much programming for a single day, isn’t it?
If you have any doubts, queries or questions, comment below or chat with me and other programmers directly on our telegram community, we would love to have you there ☺.
If you found this article helpful, give us a like and share it with your programming buddies who want to be game developers or app developers or just want to learn C++ to expand their horizons. I am sure they’ll love it too!!
I’ll see you next week with a new lesson to share with you, until then….
HAVE A GREAT DAY PEOPLE !!!
1 Comments
This brings back memories 😇
ReplyDeleteC++ was the first High Level language that I learned. Thanks for sharing 👍
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback