Learn the basics of C++ the simple way

Why is C++ needed? And why is it considered so powerful?

Before we begin to understand anything about this programming language, it is necessary to understand the purpose of using or learning it in the first place.

C++ is a programming language that is well suited for applications that has critical constraints and tolerates no compromise on speed (high response time), energy consumption, high performance, scalable, easily portable, etc.

If these criteria are not of utmost importance to your application, then you can opt for other programming languages as well, and Python (used in almost all major machine learning applications) would be the easiest to start with.

The process of starting to code in C++ and then to deploy a program to get the desired result, involves the following steps:

  1. To write your code (a functionality to get your job done), called as the source code.
  2. To use a compiler to compile your source code.
  3. To use a linker to link your object code and other libraries and make an executable file.
  4. To run the executable file and test it to find errors.
  5. To debug it, and eliminate the errors.
  6. Et voilà! we are ready to deploy our code on to the real world.

Explanation: The program we write to accomplish a given task is the source code. Its a high level language compared to what a computer can understand. So to convert it to machine understandable format (object code), we compile the source code using a c++ compiler. This object code is then linked to all the libraries we used in our program using a linker. The output of which is an executable file which can be run or deployed in production. But, before deployment, we need to Test it to see if there is any error. There will be many (for even an experienced programmer), so we then eliminate these errors by debugging our code.

A C++ Program has two parts:

  • Preprocessor Directive
  • Main function

Preprocessor Directive

#include <iostream>

Anything starting with # is a preprocessor directive part of c++ program example: include, define , etc.

include helps to add the directives of the after mentioned libraries, in our example it is the iostream library. This particular object-oriented library provides us with input and output functionalities using streams.

< > is the brackets that encloses this iostream library. It essentially means, look for this in the directory where standard libraries are kept, since iostream is a standard library.

" " : you can also enclose a library inside the double quotes, which means, look in the current directory first, if not found there, then look in the directory where standard libraries are kept. You don’t use this for standard libraries.


Main Function

int main()
{
	// do some work
	// ...
	
	return 0;
}

The main function always returns something. Here int. The { } denotes the start and end of our main function. and the return 0 at the end is customary.

When we combine both the preprocessor directive and the main function together, along with our first hello world program inside the curly brackets of the main, we have our first C++ program!

#include <iostream>

int main()
{
	std::cout << "Hello world, I am trying C++ for the first time!";
	return 0;
}
  • std::cout : cout used to display the output (our message of hello world) to the screen or to the console. And since cout belongs to the standard library, we mention it as well.
  • << : This means, stream out. To understand in simple terms, consider it a way of saying, display my message "Hello world, I am trying C++ for the first time!" on to the console using cout from the standard library of iostream.
  • After every line to be executed, we put a semi colon ; at the end. It is crucial and cannot be skipped.

Comments

Comments are not executed and are just there in your code for you to label something with a short explanation (one line or in a paragraph style.) It makes your program more readable to others. There are other uses for comments as well, which we will learn later. To write a single line comment in your code do the following:

// To write a single line comment in your code

To write a block comment in your code that spans over multiple lines, do the following:

/* this is a

block 

comment */

For example:

#include <iostream> // including the iostream library

int main()
{
	std::cout << "Hello world, I am trying C++ for the first time!"; 
	// Displaying a message on to the console using cout

	return 0;
}

To compile

There are many compilers, g++ is one of them. The syntax to compile your source code is as follows:

g++ hello_world.cpp -o hello_world.out

//OR

g++ hello_world.cpp -o hello_world

The hello_world.cpp is your source code and has a .cpp extension, and hello_world is the name of your output file (executable file).

After you compile, a file names hello_world is created in the same directory. Run the newly created executable using the following commands in the terminal.

./hello_world

Namespace

Namespaces provide a method for preventing name conflicts in large projects. Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes. This is helpful in avoiding unwanted repeatability in our code.

So Namespace tells the compiler which library the functions we are using belongs to. The syntax is as following:

#include <iostream>

using namespace std;
int main()
{
cout << "Hello world";
//
}


sizeof( variable_type)

Helps to see how much memory a certain variable type uses. This is beneficial for proper memory management. There are many variable types, which will be covered in another blog.

sizeof(variable type)

For example:

#include <iostream>

using namespace std;

int main()
{
	cout << sizeof(int);
}

The output of this code will be 4 bytes. Follow up on the memory blog to know more.


Constant variable

A variable, the value of which cannot be changed in the lifetime of that particular program.

const int maxScore = 100;

In case you do want to change the value, edit your source code and recompile it.


Enumerated constants

Using this, you can create a new variable type and assign finite number of values to it.

enum MONTH {jan, feb, march, ...dec};

The entries jan, feb, march, … dec will all be assigned integer values starting from 0 to 11. Now you created MONTHS that can take 12 possible values.

And just like you can create an int variable like int num, you can create a MONTHS variable type as follows:

MONTHS best_month;
best_month = jan;

Here, we assign best_month one of the values in MONTHS


Escape statements

/n , /t , etc. are some of the escape statements, that are used to format the data.

#include <iomanip> // library for manipulation 
int main()
{
std::cout << "Dogs" << std::setw(10) << "Heavenly" << std::setw(10) << "\n";
}

Here, you are putting spaces in between the outputted strings. Since Dogs has 4 characters in it, and we have set the width to 10 using setw from the standard library, we are left with (10 - 4 = 6) spaces before the next word Heavenly is outputted. This created a space of 6 blanks in between Dogs and Heavenly.


Follow up on the next blog and happy learning!