Getting Started with C++ Language

Sriram Hariharan  Print 
16 Sep 2018
17 Sep 2018
Beginner
101

Welcome readers. If you are new to C++ or want to get started with C++, then you are in the right place. This article will give you a deep understanding of what is C++, its benefits and how you can get started with your first C++ program. Let’s get started!

What is C++

C++ is a general-purpose Object-Oriented Programming (OOP) language developed by Bjarne Stroustrup. C++ is a middle-level programming language and an extension of the C language. C++ runs on different operating platforms such as Windows, Macintosh OS, and different versions of UNIX.

History of C++

C++ programming language has an interesting history. In 1979, Bjarne Stroustrup used the Simula-67 programming language when working on a PhD Thesis project at Bell Laboratories. Simula-67 supported the object-oriented programming concepts and Stroustrup found it very easy and useful for software development. However, Simula was very slow when it came to practically implementing the programs.

Stroustrup then started to work on his new project “C with Classes”. He aimed to make use of the object-oriented programming concepts into C programming language (developed by Dennis Ritchie is 1972) without changing the C components. Later in 1983, the name C with Classes was changed to C++. The ++ operator is taken from C language which represents incrementing a variable. C++ is a superset of C programming language. This means that any “valid” C program is also a C++ program.

In 1985, the first edition of The C++ Programming Language was published. Later, in the same year, C++ was implemented as a commercial product. In 1989, C++ version 2.0 was released. In 1998, the C++ standards committee published the first ISO standard known as C++ 98 (C++ ISO/IEC 14882:1998). In 2003, the C++ standards committee responded to problems in the first ISO standard (1998) and made revisions which became C++ 03. The next official release of C++ happened in 2011, which was called C++11. This was a major revision release. Later in 2014, bug fixes and improvements (including new modules and regular expressions) were released on the C++11 version which was called C++14.

The below infographic shows the history of C++.

Features of C++

This section lists the salient features of C++ -

Platform Dependent

Since the program executes in the same operating system where it was developed and compiled, C++ is referred to as platform dependent. Once the code is compiled, it can run only in that environment. If you want to run it in another environment, you must recompile the code for the different target environment.

Rich library of inbuilt functions

The C++ Standard Library comprises of two parts – Standard Function Library and Object-Oriented Class Library. Standard Function Library consists of a collection of functions to perform mathematically, string, character, input/output operations. These functions are provided as a part of the programming environment. Object Oriented Class Library is a collection of classes and associated functions such as -

  • Standard C++ I/O Classes

  • String Class

  • Numeric Classes

  • Container Classes

  • Algorithms, Function Objects, and more

Compiler based programming language

C++ is a compiler based programming language. You cannot execute a C++ program without compiling it. You must compile the program and then execute it in the particular environment.

Classes and Objects

In object-oriented programming, objects are referred to as the basic run-time entities. An object can be anything – person, item, or any data that should be handled by the program. In simple terms, object is nothing but data and the function that operate on the data, bundled together as a single unit.Class is a collection of objects of the similar type. Objects are a variable of the type class. When you create a class, you can immediately create number of objects that belong to the class.

Inheritance

In general terms, inheritance means to acquire the properties of another person. The same concept applies in object-oriented programming. When the objects on one class acquire the properties of the objects of another class, it is referred to as inheritance. The class in which the members are inherited (by objects of another class) is called the Base class. The class that inherits the members is called the Derived class. In simple terms, Base class is the existing class and Derived class is the new (inherited) class. Inheritance helps to reduce the code size significantly as we can reuse the classes and their objects.

Encapsulation

The answer to this is in the question itself. Encapsulation means to “wrap up” the functions and data into a single class. To access the data, you must access it through the functions that are present in the class. The data cannot be accessed directly.

Abstraction

Data abstraction means to show only required information and hide the remaining information. To understand easily, to drive a motorcycle, you only need to know how to use the accelerator and clutch simultaneously. You need not know what happens internally when you turn the accelerator.

Polymorphism

Morphing refers to process of taking different forms. Polymorphism refers to the ability of the object, variable or the function to take multiple forms. To better understand the concept, let us consider an example. There is a base class “Animals” with sub-classes “Dog”, “Eagle”, and “Dolphin”. The base class has a function named “movement” that is inherited by all the 3 sub-classes. When you call the function in the object of the “dog” class, the response will be “run”. Similarly, if the function is called in the object of the “dolphin” class, the response will be “swim”.

Message Passing

Message passing is a communication format between objects, processes, or any other resources used in object-oriented programming. This process involves specifying the name of the object, name of the function and the message to be sent.

Dynamic Binding

Dynamic binding refers to the procedure to link a procedure call to the code that will be executed only during run-time. In simple terms, the compiler will match the function calls with correct definition only at run-time. Dynamic binding is associated with polymorphism and inheritance.

Advantages of C++

  1. C++ works based on Object-oriented Programming model. Hence the OOP concepts can be used with C++ programming.

  2. C++ is compatible with C language, and every valid C program is a C++ program

  3. C++ is a portable language and the preferred choice of developers for multiple-device and multiple-platform app development.

  4. C++ program compilation and execution is faster when compared to most other programming languages. C++ offers great concurrency support.

  5. C++ can be used to develop a wide range of applications such as simple GUI applications to games applications (graphics) and simulations.

  6. C++ follows a bottom up approach during program compilation

  7. C++ supports exception handling, inline functions, pointers and references

  8. From the community standpoint, C++ has a strong community from where you will get almost instant support.

Set up your machine for C++ development

C++ is a free programming language and is available for different platforms (Windows, MacOS, Linux). There are also online C++ compilers that you can use to compile and run a C++ program.In this section, we will see how to set up a C++ development environment on Windows and MacOS.

Setting up a C++ development IDE on Windows (7, 8, 10)

There are lots of free IDEs available that can be used to run and execute your C++ programs. For the purpose of this exercise, we will be using Dev-C++ as the IDE to build our C++ program.Follow the step by step guide to get Dev-C++ installed on your Windows OS.

Step 1 – Open a browser and type https://sourceforge.net/projects/orwelldevcpp/files/latest/download

Step 2 – Once the download is complete, navigate to the download folder and double click on “Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup”. If you see a prompt asking you to allow the app from an unknown publisher to make changes to the device, click Yes to continue.

Note : Make sure you run the setup with administrative privileges.

Step 3 – The installation procedure is self-explanatory. First, agree the terms and conditions, next choose the components that you want to install. I’d recommend you select the type as Full.

Step 4 – Click Next and select the folder where you want to install Dev-C++. As a final step, click Install. This will install Dev-C++ on your computer.

Note : If you see a message asking if you want to install Dev-C++ for all users of the computer. Click Yes.

Step 5 – Select the check-box “Run Dev-C++ 5.11”. Click Finish to complete the installation.

Step 6 – You will be prompted to make some default settings if this is your first time installing Dev-C++. Click Yes and this will launch the Dev-C++ first time configuration pane. Choose your preferred language from the list and the Dev-C++ theme from the drop down.

Step 7 – After completing all the installation configuration, you will see the Dev-C++ IDE. You can choose to disable the tips at start up.

With this, your C++ development environment is ready. It’s time to write our first C++ program and execute it.

Create your first C++ program

Let’s begin to create our first C++ program. We’ll take the ever simple “Hello World” program and explore the IDE to see how we can make the code work. The purpose of this program is to display the text “Hello World” as output on the screen.Let’s start writing the program in Dev-C++ editor. Follow the steps as shown below –

Step 1 – Go to File >New>Source File. The shortcut to open a new source file is Ctrl + N.

Step 2 – Type the Hello World program (code snippet given below).

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

Step 3 – Click File>Save (or) Ctrl+S to save the file as a C++ Source File type.

Step 4 – From the menu, click Execute > Compile. The shortcut key to compile the code is F9. You will see the compilation status in the Compile Log section at the bottom of the screen.

Step 5 – If there are no compile errors, click Execute > Run. The shortcut key to run the code is F10. You will see a new pop up window showing the result of the code “Hello World”.

That’s it! We have successfully executed our first C++ program!

Detailed explanation of the program

Now that we have executed the program, let’s take a detailed look into each statement in the code.

Command
Explanation
#include
Iostream is the header file. This command lets the compiler know that it must include the standard iostream file that contains all the declarations in the standard input/output library.
using namespace std;
This statement is used to make use the namespace std in the file. By using this command, it makes it easier to reference operations included within the namespace. Namespace is the area where the actual code resides.
Note : It’s important to end the statement with a ‘;’ (semicolon). Semicolon is like a line/statement terminator in the code. If you do not use the semicolon, you will see errors when you compile the program.
int main() { … }
This is the main function of your program. The code inside the { } will be executed when the program is compiled and executed.
cout
This statement is very similar to the “printf” in C programming. Any text after the << will be printed as the output of the program.
return 0;
This statement is referred to as return statement. Return 0 indicates the exit status of the program. It’s not mandatory to add the return statement from the main() function. The compiler will return a status.
Summary

C++ Is used by programmers working in almost every domain. It’s the easiest language to get started with and has the capability to take you a far distance as a developer.

Hands-on Learning
Free Interview Books
 
+