Dive into basics of C++

SG Blogs
6 min readApr 29, 2020

Hi there!!. In this post we are going to talk about C++ basic but most imp topics which we mostly ignore & when it appear in interviews or exams we get struck and end up loosing marks despite being good with other stuff and we will also going to talk about execution of programme through commands.As the commands are valuable when we talk about the industry standards as it saves lot of time and also it is addicting once you get used to it……

Perquisite for this language is not more but some basic knowledge of the C language is required. Now we will directly going to start from the basic concept of the C++ after taking the the quick and short tour(don’t want to waste time!! )of language.

What is C++?

C++ is a cross-platform language developed by the Bjarne Stroustrup that can be used to create high-performance applications.

Cross-platform means the program written in C++ language is able to run on the different machines and the software with some modifications if needed.

It is one of the world’s most popular languages.It is the Object Oriented language in which we can create the objects just like the real world and perform various operations on it (we will see objects and stuff in upcoming post ). As C++ is close to C# and Java , it makes it easy for programmers to switch to C++ or vice versa.

Software for the C++

You can use various software for C++ programs such as the Turbo C++ but it is always advised to use the IDE such as Visual Studio Code(Vs code) which is as per the industry standards. IDE it is not like the software like Turbo C++ that you will just run code after installing . You have to install the proper file for each language as it supports various languages and each language required different compilers.

For ex — For setting the IDE for the language of which we are talking about you have to download the file named mingw-w64 than you have to follow certain steps that will lead to proper installation of IDE for your preferred language. Other popular IDE includes Eclipse and the Code::Blocks. In case if you found some error while installing then let us know we will make special post on it.

How to execute the program of C++ in Vs Code

When we save the file in the Vs Code with the .cpp extension then we need to execute the file . Then we will execute the file using the commands. We can use the terminal for the execution of Vs Code but it sometimes gives error in the execution to the new learners so that’s why in this post I am going to execute the program using the Windows PowerShell.

Steps for creating the C++ file through command prompt

  • Open the windows command prompt
  • Then switch to the desktop from the windows C:\Users\user>
  • You should switch to desktop by running cd desktop command
  • After executing this command commands prompt will show now C:\Users\user\Desktop>
  • After that create the folder on the desktop by using the mkdir command(example- mkdir blogs)
  • But you are still on the desktop now you need to open the blogs folder and command for that is again cd(cd blogs).
  • Now you need to code in the folder created for coding the command is the code . (here the space between the code and dot(.) is necessary otherwise it will give error)

This command will open the visual studio code and then you should create the new file there in the folder you created

After that you should code in the file you have created and after that save the file by .cpp extension

After saving file then just open the powershell and follow the command which i am going to tell in next topic and your code will execute.

You can also create the file directly through options present inside the Vs Code but it is always good practice to create the file using the command prompt as it will make you handy with commands.

How to execute the file in the PowerShell

Now once we save the file than we need to execute the file . The steps for executing the file is as follows

  • get into the start menu and open the PowerShell
  • After that switch to desktop using same command cd desktop
  • After shifting to the desktop than shift to the folder where we are saving all our files(here it is Blogs)
  • After that for compiling our program run command g++ hello.cpp(here the hello is our file name which is stored at desktop and the .cpp is extension)
  • After that for running of our command run the command ./a.exe
  • a.exe is the file that will be created once we compile our program

After running the command we will get the output of our program.

Now as we are done with the commands we are going to take basic program of C++ and explain the basic topics which we were talking about

Program:
#include <iostream>
using namespace std;

int main() {
cout << “First program of tutorial !”;
return 0;
}

Above program is one of the basic program of the C++.It will give the output First program of tutorial!. It consist of the various parameters such as

  • #include<iostream>
  • using namespace std
  • int main()
  • cout<<
  • return 0

we will now study use of the each term we listed above

#include<iostream> is a header file that allow us to use the input (cin>>)and output(cout<<) objects. Header files add functionality to C++ programs.Now the question arises that what is the header files.So the files that tell the compiler how to call some functionality are called header files.Here it should be noted that the header file don’t know that how the functionality works .#include is use to include this file into our program. In this whole explanation functionality is the term which some of you will find hard to understand so let me explain for you – functionality means making our program functional ,when we execute the program containing the cout and cin objects then it should work properly and should gives us the proper results….

using namespace std – it allow us to differentiate between the function with the same name present in some other library. Consider the example of you writing the program which has some function in it. Now the function which you are using also exist in the other library with the same name but with the different functionality. So now the case will arise which will lead to the confusion for the compiler that which function to include . In that case the using namespace std will clear the confusion for the compiler and the function which we want will be included in the program.

int main() – it is the function inside our C++ program. The code inside this function which is written inside the curly braces{} will be executed when we run our program.

return 0 – it is basically the return statement which ends our main function.

difference between the <iostream> & <iostream.h> –

iostream is a standard header. iostream.h is a non-standard header that was very common in previous version C++, and is what iostream evolved from. It’s still common to have iostream.h around, presumably for use with older programs.

You can also write the C++ program without writing the using name space std but then you will have to replace it by the scope resolution operator (::) for some objects. Its example is given below
#include <iostream>
int main() {
std::cout << “we are learning C++!”;
return 0;

} but the better practice that is writing using namespace std once only at the top as it will save our time and also will make our coding easy as we don’t have to mention the std:: every time .
This post all about the basic information on the C++ and how to create and execute the file using the commands for next post you can suggest any topics related to programming of your interest.

Feel free to suggest any improvement if you want in this post…..

--

--

SG Blogs

Not a professional in writing but everything I write are my real experiences and everything I share is with great enthusiasm