Getting Started in C++

Introduction to C++

C++ is one of the most popular computer language today because it is a structured, high-level, machine independent language. It was developed by Bjarne Stroustrup in early 1980s. It is called C++ because it is an extension to ‘C’, another very popular high-level programming language. C++ is widely used in different sectors of development like

  • Games
  • Device driver development
  • Embedded Software, etc.

Compiling your first C++ Program

The best way to learn any new programming language is to start writing programs, so we’ll dive straight into writing programs and understand the key concepts. We’ll be working in Linux environment, but you can choose any OS of your choice or any IDEs This is the famous “Hello World” program. This is the first program programmers write in a new language

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World";
    return 0;
}

Just write this code in any text editor of your choice (gedit, vim, etc.) and save it like
filename.cpp (you can choose your own filename)
Every C++ program must be saved with .cpp extension, after saving this go to the terminal and type command
g++ filename.cpp

This command tells the OS to call the C++ compiler, and pass it the file filename.cpp to compile.

Note: g++ is part of GCC (GNU C/C++ Compiler) which is present in Linux OS.
After running this command we notice that a file named a.out is created in the same folder. Now to run our program, we’ll use this command in our terminal
./a.out
This command runs our program and this is the output that is displayed on our terminal
Hello World

Congratulations, we have written our first C++ program. Don’t worry if you don’t understand what all the different lines in the Hello World program do. We’ll look at and explain each line in detail in the upcoming lessons.

To summarize the steps

  1. Write your program in any editor, and save it with .cpp extension
  2. Give command, g++ filename.cpp to compile your program
  3. Give command, ./a.out to run your program

Basic definitions

These are some definitions just for better understanding of everything

Computer Program:

A computer program is a collection of instructions that performs a specific task when executed by a computer. This means that by our program we can tell computer to perform various tasks. They can be as simple as adding two numbers or can be as complex as finding number of faces in a picture.

Compiler:

A compiler is a computer software that transforms computer code written in one programming language (the source language, in our case: C++) into another programming language (the target language). So C++ compilers take our programs written in C++ and convert them into a language that computers can understand (machine code: 0s and 1s)

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?