All posts

Introduction to C++

Tusar Palauri | 25th Feb, 2023

Creator

C++ is a programming language created by Bjarne Stroustrup and his team at Bell Laboratories in 1979. Forty years later, it is one of the most widely used languages in the world; we can find C++ applications everywhere, from the bottom of the oceans to the surface of Mars.

As the name implies, C++ was derived from the C language; Bjarne’s goal was to add object-oriented programming into C, a language well-respected for its portability and low-level functionality.

So why learn C++? Among many other things:

  • It is fast and flexible.
  • It is well-supported.
  • It forces you to think in new and creative ways.
Note: Don’t worry if some words don’t make much sense right now. We’ll learn about them in a bit!

Hello World!

Take a look at the hello.cpp file in the code editor that is placed in the middle of the screen. It’s a C++ program!

C++ programs are stored in files which usually have the file extension .cpp, which simply stands for “C Plus Plus”.

The code inside our C++ file is a classic first step all new programmers take — they greet the world through the terminal!

The terminal is the black panel on the right. It should be blank right now. The code in the text editor will print text out onto the terminal. More specifically, it will print the phrase Hello World!.

The code inside our C++ file is a classic first step all new programmers take — they greet the world through the terminal!

Output

High five! We just got your first program to run.

C++, like most programming languages, runs line by line, from top to bottom. Here is the structure of a C++ program:

In between the curly braces is what we are going to focus on for now.

1#include <bits/stdc++.h>
2using namespace std;
34int main()
5{
6    vector <int> vec = {1, 2, 3, 4, 5};
78    // Method 1: Using range for
9    for (auto element: vec)
10    {
11        cout << element << " ";
12    }
1314    return 0;
15}
  • std::cout is the “character output stream”. It is pronounced “see-out”.