Building Blocks of C++

C++ is between 10 and 100 times faster than Python when doing any serious number crunching.

Photo by Dawid Zawiła on Unsplash

Most modern programming languages have a set up similar building blocks, for example

  1. Receiving input from the user and Showing output to the user
  2. Ability to store values in variables (usually of different kinds such as integers, floating points or character)
  3. A string of characters where you can store names, addresses, or any other kind of text
  4. Some advance data types such as arrays which can store a series of regular variables (such as a series of integers)
  5. Ability to loop your code in the sense that you want to receive 10 names from a user, you will write the code for that 10 times, but just once and tell the computer to loop through it 10 times
  6. Ability to execute statements of code conditionally, for example if marks are more than 40 then the student passes else fails
  7. Put your code in functions
  8. Advanced data types that are formed through a combination of one or more types of basic data types such as structures or classes
  9. Read file from a disk and save file to a disk
  10. Ability to comment your code so you can understand it when you revisit it some time later

Let’s dive right in and see how we can do these things in C++.

Before we can begin to write a program in C++, we need to install Dev-C++. Once done, go ahead and open the IDE and try out the following code to see if everything is in order.

As you noticed, unlike languages such as Python, R or Ruby, it takes more than a few statements just to display basic text to the user in C++. In the next section we will try to dismantle this code and understand the various components. Lets however cover a few important points:

  • In C++ we need to end each line of code with a semi-colon ;
  • The scope of statements is defined using curly brackets {}, unlike Python where the scope is defined through indentation
  • All statements need to be within a function. Here we have included the statements in the main() function which is the first function that is executed during a compiler call. All other functions will be called from within this funciton.

1. Receiving input from the user and Showing output to the user

Following program shows output to the user. The include statement is used to call the iostream header file which is same as a python library. This header file provides information on basic programming routines including input and output constructs. The next is int main() which says that the main function will return an integer after execution. Within the main function we use cout« to show the text to the user. The text is enclosed in double quotes “text”. endl after the text tells the compiler to insert a new line in the output window. Finally we return 0 as the main function is supposed to return an integer. 0 signifies that everything was in order during the execution of the funciton.

We can modify this program to accept input form the user. The cin» statement allows us to receive input. The variable in which we store the received input needs to be defined beforehand.

2. Ability to store values in variables (usually of different kinds such as integers, floating points or character)

C++ is not dynamically typed — you need to type out the variable’s name and data type before using it.

Basic data types: In C++ we have several types of variables, lets take a look at the important ones:

3. A string of characters where you can store names, addresses, or any other kind of text

A string in C++ can be defined using the string keyword. It can be assigned usign the input from user or it can be assigned by providing text within double quotes “text”.

4. Some advance data types such as arrays which can store a series of regular variables (such as a series of integers)

Arrays are a series of variables stored together in one variable. Arrays can be one-dimentional or multi-dimentional. One-dimentional arrays:

Multi-dimentional arrays:

Loops can be used to iterate over one-dimentional or multi-dimentional arrays. We will take a closer look at this in the next section.

5. Ability to loop your code in the sense that you want to receive 10 names from a user, you will write the code for that 10 times, but just once and tell the computer to loop through it 10 times

C++ has several looping options such as ‘for’, ‘while’ and ‘do while’. There are also options of nesting (single, double, triple, ..) loops. a. The for loop

b. The While loop executes the same code again and again until a stop condition is met:

c. The Do-While loop executes the same code again and again until a stop condition is met. The difference from while loop is that in do-while loop atleast the content of the loop is executed once before checking the condition.

C++ also provides the break and continue statements that allow us to alter the loops further. Following is their use: break jumps immidiately out of the loop. mostly used in while loops but can also be used in for loops

continue is similar to break, but just breaks out of the current iteration, but still continues running the next iterations

6. Ability to execute statements of code conditionally, for example if marks are more than 40 then the student passes else fails

C++ provides if.., if..else.., and switch statements to apply conditional logic. Lets take a look at them: a. The basic syntax for creating an if statement is:

b. The basic syntax for creating an if…else statement is:

c. The basic syntax for creating a switch statement is:

7. Put your code in functions

Functions allows us to create a block of code that can be executed many times without needing to it write it again.

We can call the function by its name:

8. Advanced data types that are formed through a combinaiton of one or more types of basic data types such as structures or classes

C++ allows user to create classes. These can be a combination of variables and functions that operate on those variables. Lets take a look at how we can define and use them.

9. Read file from a disk and save file to a disk

Lets see how to read and write a text file in an organized way. We use the fstream header file for importing the functions necessary to read/write files.

10. Ability to comment your code so you can understand it when you revisit it some time later

We can tell C++ that a line of code is a comment as follows.

We can tell that a multi-line block of text as follows.

To close I will emphasize the importance of practicing in learning anything new. Persistence and trying out different combinations of these building blocks for solving easier problems first and more complex ones later on is the only way to become fluent.

Comments welcome!

Experienced in synthesizing data to identify trends, deliver insights and recommendations. Focused on customer lifecycle, cross-sell, and employee performance.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store