Getting Started with C++

G

In this article, you will learn about the basics of the C++ programming language.
Here’s what you’ll learn today.

  • What is C++ and why use it?
  • Getting Started
    • Installing GCC
  • C++: The Basics
    • Writing our first ‘Hello World’ program
    • Variables

What is C++ and why use it?

The C++ programming language is currently regarded as one of the most commonly used languages in the developer ecosystem. In fact, it can be found just about everywhere you look. It powers search engines, VR applications, air travel, movie production, and games.
Furthermore, C++ is most popularly used for building large software infrastructure and applications that run on power efficient machines. Because C++ can directly manipulate the hardware (or machine) that it runs on, programmers can fine-tune their code to run efficiently in any environment, even when there’s limited hardware space or energy available to power the application. This translates to applications that run quickly and reliably on a variety of devices, making C++ ideal for forming the base layer of many important applications.

Getting Started

Installing GCC
Before running C++ code, we need to first install a GCC compiler. This program will be responsible in running and executing our programs.
To do so, navigate to the Cygwin homepage and install the necessary binaries:

When that’s done, verify whether GCC has been correctly installed via the g++ command:

Since we are having a valid output, this means that our installation was valid! In the next section, you will get started with C++.

C++: The Basics

Writing our first ‘Hello World’ program
In a text editor of your choice, start by writing the following code:

#include <iostream>;
using namespace std;

int main()
{
   cout<<"Hello World!";
   return 0;
}
  • The include keyword tells C++ to use the iostream package. This is responsible for performing outputs to the console.
  • Later on, we used the main method. This function will run every time we compile the program.
  • The cout command tells the compiler to output the words Hello World to the console.

Let’s test it out! Save this file on your machine in your Downloads folder. Give it the name hello_world.cpp:

To run this program, run the following terminal command within the Downloads folder:

g++ hello_world.cpp #compile the program
./a.out #show the output

This will be the result:

As you can see, our code works! In the next section, we will learn about a concept called variables.
Variables
A variable is a name which is associated with a value that can be changed. For example, look at the following line:

int age = 20;

This means that the name of this variable is age and its associated value is 20. Furthermore, the int keyword indicates that this variable will be an integer.
To declare a variable, use the following syntax:

data_type variable_name = value;

There are many categories of variables. Some of them include:

  • int: These type of of variables holds integer value.
  • char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
  • bool: holds boolean value true or false.
  • double: double-precision floating point value.
  • float: Single-precision floating point value.

Here is a brief example of variable usage in action:

#include <iostream>;
using namespace std;
int main()
{
   int age = 20; //declare an int with a value of 20
   // displays age on screen
   cout<<age<< endl; //the endl keyword will move to the next line.
   return 0;
}

This will be the result:

Let’s play with this concept further. This piece of code declares a char and a bool variable and outputs it to the console:

#include<iostream>;
using namespace std;
int main()
{
   char alphabet = 'a'; //declare a char variable and assign it the value of 'a'
   bool isMale = true; //declare a boolean and assign it the value 'true'
   // displays isMale on screen:
   cout<<"Are you male? "<<isMale<<endl;
  //displays the value of alphabet on screen:
   cout<<"Your favorite letter: "<<alphabet<<endl;

   return 0;
}

This will be the outcome of the program:

And we’re done!

Conclusion

In this guide, you learned the building blocks of the C++ programming language. If you encountered any difficulty, I advise you to deconstruct and play with the code so that you can understand its inner workings.
Thank you so much for reading! Happy coding!

Add comment

Tutor Network