Documenting my CPP Journey Part#1

Introduction

Welcome to my first series on #Hashnode where I will be documenting my journey of learning C++ from scratch to advance to develop my programing skills and later make some interesting projects. In this I will explain how to set up an environment for you to code in C++ through Visual Studio Code and also display my first program. But first lets understand what is programming and what is its use.

What Is Programming?

Programming can be understood as your instruction to the computer (machine) to solve real problems. The basic principle behind the development of machines was to make life easier, and machines that followed our instructions were able to do the same. But the distance between what we say and what the machine understands is reduced with a programming language. This shows the importance of learning a programming language.

A programming language helps us communicate with the computer.

Just like us humans who need a language, be it English, Hindi or Bengali, Marathi, Tamil etc, to talk to our people, computers also need a language to converse. To name a few there is C++, C, Python, Java etc.

Why C++ ?

Now, as we start listing the names of these programming languages, the question instinctively arises as to why C++. Despite being a programming language from the 80s, it has never lost its luster. C++ is an additional version of C developed by Bjarne Stroustrup. It is supposed to be very close to the hardware, making it relatively easy for programmers to issue instructions directly to the system without any middleman. Another reason why C++ is one of the most used languages ​​is its object-oriented nature. C++ is an object-oriented programming language that gives it the power to create real-world software systems. Beginners and #CodeNewbies don't need to focus too much on the language they choose to begin with as problem solving is more important than the programming language used(t&c).

Advantages Of Using C++

1. Portability

C++ provides this feature of portability allowing us to develop codes without caring about the hardware. This lets us move the development of a program from one platform to another.

2. Mid-level programming language

Being a mid-level programming language, we can treat it as both a low-level and high-level language. It is a mid-level language as we can do both systems-programming (drivers, kernels, networking etc.) and build large-scale user applications (Media Players, Photoshop, Game Engines etc.)

3. Object-Oriented

The main aim of OOP(Object Oriented Programming) is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. The OOP concepts like polymorphism, encapsulation, inheritance, and abstraction give C++ the biggest advantage over other programming languages. Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute

  • OOP provides a clear structure for the programs

  • OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug

  • OOP makes it possible to create full reusable applications with less code and shorter development time

OOPs have following features:

  1. Object - Instance of Class

  2. Class - Blue print of Object

  3. Encapsulation - Protecting our Data

  4. Polymorphism - Different behaviors at different instances

  5. Abstraction - Hiding our irrelevant Data

  6. Inheritence - One property of object is acquiring to another property of object

The Four major Pillars of OOPs are:

  1. Abstraction

  2. Encapsulation

  3. Inheritance

  4. Polymorphism

4. Multi-paradigm programming language

Paradigm refers to the planning involved in programming. It concerns the logic, the style, and the way how we proceed with the program. C++ is a multi-paradigm programming language as it follows three paradigms:

a. Generic – Using a single idea that serves multiple purposes.

b. Imperative – Using steps that change the state of the program.

c. Object-Oriented – Using methods and classes for reusability and modularity.

5. Standard Library

The Standard Library is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. C++ provides a good range of built-in libraries. They help in making the software development faster and allows the user to do more with less. The C++ Standard Library can be categorized into two parts −

  • The Standard Function Library − This library consists of general-purpose,stand-alone functions that are not part of any class. The function library is inherited from C.

  • The Object Oriented Class Library − This is a collection of classes and associated functions.

6. Memory Management

Reserving or providing space to a variable is called memory allocation. For storing the data, memory allocation can be done in two ways -

  • Static allocation or compile-time allocation - Static memory allocation means providing space for the variable. The size and data type of the variable is known, and it remains constant throughout the program.

  • Dynamic allocation or run-time allocation - The allocation in which memory is allocated dynamically. In this type of allocation, the exact size of the variable is not known in advance. Pointers play a major role in dynamic memory allocation.

C++ supports DMA (Dynamic Memory Allocation), which helps to free and allocate memory. Since there is no garbage collection, C++ gives the programmer total control over memory management. Dynamically we can allocate storage while the program is in a running state, but variables cannot be created "on the fly".

7. Fast and Powerful

As C++ is a compiler-based programming language; we do not require to install a special runtime while running the program. Hence, they are pre-interpreted and it makes the code faster and more powerful. C++ programs excel in execution speed. Since, it is a compiled language, and also hugely procedural. Newer languages have extra in-built default features such as garbage-collection, dynamic typing etc. which slow the execution of the program overall. Since there is no additional processing overhead like this in C++, it is blazing fast.

Even the compilation and execution are faster allowing it to create several kinds of programs from games to drivers to complicated GUIs.

8. Similar to other languages

C++ syntax is similar to C#, C, and Java. It makes learning C++ easier if you already know one of them. It also makes switching to and from other languages easier.

This can be treated as an added benefit that C++ is compatible with C programs i.e. every running C program can be run as a C++ program. Most of the time we just need to run the program on a file .cpp extension.

9. Wide Range of Applications

C++ is useful to make GUIs as well as games. C++ is also useful to develop graphics and real-time algebraic simulation. Hence, C++ is beneficial in every stream. C++ finds varied usage in applications such as:

  1. Operating Systems & Systems Programming. e.g. Linux-based OS (Ubuntu etc.)

  2. Browsers (Chrome & Firefox)

  3. Graphics & Game engines (Photoshop, Blender, Unreal-Engine)

  4. Database Engines (MySQL, MongoDB, Redis etc.)

  5. Cloud/Distributed Systems etc.

10. Huge Community

C++ has a vast community around it. Community size is very important if you want to get supported every now and then. The larger the community size, more the help you’ll get to solve your problems.

A huge number of paid/free online courses and lectures are available, which shows how community support works. Some of which I have credited below in this blog.

Setting Up Our C++ Environment

1. Installing Visual Studio Code

Visual Studio Code, also commonly referred to as VS Code, is a source-code editor made by Microsoft with the Electron Framework, for Windows, Linux and macOS. Click here and you will be redirected to the official download page of VS Code. Download the VS code according to your operating system in use.

2. Installation of g++

g++ command is a GNU C++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different “options” of g++ command allow us to stop this process at the intermediate stage. Simply, g++ is a compiler that helps us convert our source code into a .exe file. I recommend you to see this video on YouTube by Geeky Script titled How to install MinGW -w64 on Windows 10/11 [2022 Update] MinGW GNU Compiler for C & C++ Programming. This tutorial will explain how to add the compiler to our system and after doing so we are now ready to code.

Prerequisites For Writing Our First Program In C++

To write our first program, we need Visual Studio Code. Create a folder, then right click inside the folder and click "open with code". Following these steps will open your Visual Studio Code with this folder as context. After opening VS Code, you need to install some extensions. Go to the extensions menu and search for "C/C++" and it will show you that extension. The C/C++ provided by Microsoft, along with other extensions we add, will make our lives easier while learning C++. Click the install button and it will start installing the extension for you. Image below should help you

search-cpp-extension.png

Writing Our First C++ Program

It's time to write a C++ program now that you've installed a compiler. The "Hello World" program is the pinnacle of programming examples. Let's start there. One of the easiest programs you'll learn is "Hello World," which is the first step in learning any programming language. Simply displaying the phrase "Hello World" on the screen is all that is required. Allow us now to check the program out:

#include <iostream>
using namespace std;

int main()
{
    // prints hello world
    cout << "Hello World";

    return 0;
}

The Output of the program is : Hello World

Now, Let's break this program down.

Line 1: The first line #include is a header file that tells the compiler to copy and paste the code from the iostream file, which is used to manage input and output streams, into our source file. The header iostream makes it possible to carry out standard operations like writing the Hello World program's output to the screen. Lines starting with a hash sign (#) are directives, orders perused and deciphered by what is known as the preprocessor.

Line 2: One unfilled blank line. A program is not affected by empty lines.

Line 3: utilizing std namespace: This is used to import the entire standard namespace into the program's current namespace. Using namespace std in a statement is generally regarded as bad practice but beginners need not worry as we are still getting introduced to the world of C++. When we import a namespace, all type definitions are brought into the current scope. The standard namespace is enormous . The scope operator(::) can be used to specify the namespace to which the identifier belongs as an alternative to this statement, whenever we declare a type.

Line 4: int main(): This line is used to declare a function called "main" that returns data of type integer. A function is a group of statements designed to perform a specific task. Execution of a C++ program begins with the main() function, regardless of where the function is located in the program. Therefore, every C++ program should have a main() function.

Line 5: { and }: The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Everything between these two comprises the body of the main function.

Line 6: //prints hello world ---> A comment line .A comment provides additional program-related information. There is no programming logic in a comment. A compiler simply skips that line of code when it comes across a comment. In C++, a comment is any line beginning with // without quotes or in between /.../.

Line 7: The compiler is instructed to display the message "Hello World" on the screen by this line .In C++, this line is referred to as a statement. Every statement aims to accomplish something.";" is a semicolon and is used to close a sentence .Semi-colon character toward the finish of the assertion is utilized to show that the assertion is finishing there. The cout is used to identify the typical desktop screen, which serves as the standard character output device. The output device is shown everything that is followed by the character "<<"

Line 8: This statement is used to return a value from a function and indicates the end of a function. This statement is basically used in functions to return the results of operations performed by a function. Now just execute the program by opening a new terminal from the options given above

Note: Indentation: As displayed in the program above the cout and the return statement have been moved to the right, as you can see. This is done to make the code more meaningful. It doesn't matter much in a program like Hello World, but as programs get more complicated, it makes the code easier to read and less likely to make mistakes .In order to make the code easier to read, indentations and comments must always be used.

Congratulations!

Hurray!!! We just finished coding our first our first program in C++. This marks the beginning our journey where we will learn the basics of C++ and gain enough idea about it to make projects to show others and impress them.

Thank you for joining me in my quest to conquer C++.In the next blog, we’ll be talking more about the basic structures of a C++ program, see you there, till then keep coding.

Credit To The Following Resource I Referenced :

  1. VS Code

  2. CodeWithHarry

  3. GeeksForGeeks

  4. TechVidvan

Regards, Atharva Salitri !!!