C++ Programming Series: Pointers (Part 1)

In this post, we are going to discuss pointers. But before proceeding, I just want to say that if you really followed all my posts till now then, Congratulations! You learned the basics of programming in general!

But there are still a lot of basics in C++! C++ is a very old programming language but we still use it because of the fact that C++ is the only language to have a very high performance with object orientation. The term, ‘object orientation’ is a very broad term which we will not discuss in this series.

C++ is powerful because it is a very low level language. And when something is low level, it will be way more flexible but hard to work with. With the help of C++, we can access every bit of our device memory directly. And so, this direct access needs us to be cautious of how it works and what are we really doing with it.

A pointer is the memory address of a variable. I don’t want to say that but learning pointers is a hard thing! Although the definition may looks so simple, to work with pointers isn’t that simple. However, if we understand the way pointer works and all such concepts, it will all be a piece of cake!

Getting the memory address of a variable:

int main()
{
  //As we all know, a basic variable of type 'int'
  int value = 20;
  
  //To get the memory address of any variable,
  //put ampersand(&) at the back of a variable's name.

  //This prints a hexadecimal value which
  //represents the memory address of the variable
  cout << &value << endl;

  return 0;
}

It looks useless but it is very useful! Memory address of a variable is that address inside our device memory where that particular variable is stored. It is a hexadecimal value which is something that is not important to know about. Here is a link explaining what is hexadecimal.

POINTER CONCEPT:

If two variables have same memory addresses, they will act like one variable. Or in other words, a variable can not have two different memory addresses. If we think that a variable have two different memory addresses, they will act like two variables.

Filling a pointer with the memory address of a variable:

//A basic variable of type 'int'
int value = 20;

//Pointer declaration is just like references
//except that it has asterisk(*) instead of ampersand(&)

//A pointer to type 'int'
int* pValue = &value;

//'pValue' is a pointer to 'value'
//or 'pValue' contains the memory location of 'value'

cout << pValue << endl;
cout << &value << endl;
//Both gives us the same memory address

Yup! There is a bit confusion of where to use ampersands, asterisks and other symbols while working with pointers and references. But once we got used to it, it will get easy!

That’s it for now! We will discuss it more later. For now, it is quite much to get used to. No, pointers are not useless! Keep proceeding. Pointers are not a hard subject! I was joking!

Learning OpenGL

The start of this month proved to be very busy for me and my new challenges! These days, I am learning OpenGL (or you may call it, Open Graphics Library) which is something next level in terms of graphics! It is pretty hard and requires solid concepts of using vectors, geometry, matrices and just mathematics! It requires to be more creative and more patient.

OpenGL Logo

To be honest, I really, don’t want to do that at first after knowing the fact that how calculative one must have to, to just begin with it. But after realizing the facts that general graphics SDK’s (SDK stands for Software Development Kit) like SDL2 and SFML don’t give many of the features that OpenGL gives and also, are built on top of OpenGL, I started to learn it.

For just proving my point, I have made a video which shows shining and rippling water which of course, is not possible with high-level SDK’s like SDL2 and SFML, is made using OpenGL in C++.

YouTube: BAC C++ – OpenGL Water

Well, I am not going to give the source code here because it is not the right time for now. But you can ask for the concept, the idea and maybe some little bits of codes behind it

C++ Programming Series: Functions/Subroutines (Part 1)

In this post, we are going to discuss something called as “Functions”. They are also called as “Subroutines”. Like arrays, functions or subroutines are very significant, awesome and helpful. These are some of the basic things which we will be keep using throughout our entire programming sessions.

The first thing to figure out is the definition of a function and a subroutine. Actually, both have slightly different meanings but are almost synonyms. Function is a group of steps performed on an input data and then, is received as an output (which is actually, the data formed by the modification of the input data). Below is an abstract visual of a function.

Function

Subroutine is a group of commands carried out in an order. Subroutine may or may not have an input or an output.

Let us keep the definitions brief and step forward into the programming!

Every code that we type in, is some sort of command which then, runs or executes. In programming languages, a function or a subroutine is a block of codes which can have inputs and outputs.

Below are many lines of code which you may wanted to copy and will not bother typing it all by yourself.

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string name;
  cout << "Enter your name: ";
  getline(cin, name);
  cout << "Your name is " << name << "." << endl;

  int age;
  cout << "Enter your age: "; cin >> age;
  if (age >= 18)
  {
    cout << name << ", you are eligible for registration." << endl;
    cout << "In other words, you can go to next section for registration!" << endl;
    cout << endl << "***Thank you for visiting us!***" << endl;
  }
  else
  {
    cout << name << ", we are sorry that you are not eligible for registration!" << endl;
    cout << "You can register after " << 18 - age << " years." << endl;
    cout << endl << "***Thank you for visiting us!***" << endl;
  }

  //For the sake of simplicity, just imagine that there are next sections for registration in the program...

  return 0;
}

This is a program which registers the person in some sort of a company or an institution. We have hard-coded the first part only. This whole procedure has to be done for many people. We can loop through it to recall all the codes. But it is still a mess in the main block.

I can’t type in all the code for the registration because it will make this post a mess. Just imagine, what if it is more than 1000 lines of codes, will you still prefer to read this post rather than going somewhere else for learning? This is pretty low-level.

From low-level codes, we mean that we are working with such codes which are extremely customizable yet, at the same time, extremely uncompressed and powerless! From powerless, I mean that you have to type three lines of code to just get one input from the user. And you repeat it for all the inputs you want from the user. And what I mean by power, is to have the whole codes that you see in the program above, to be executed by just one line. Compressing that much lines into a line sounds crazy! This is what we can call as high-level.

From high-level codes, we mean that we are working with such codes which are barely customizable yet, at the same time, extremely compressed and powerful! Of course, by power, I mean something like replacing the set of commands,

“Hey, computer! Make a variable of window and make another variable of size then, make that window size equal to that size then, stretch the window gradually to that size then, put the window in the center and at last, force the window to be shown.”

with

“Computer, open up the window!”

It looks like I changed the direction of the topic to an entirely different dimension. Actually, this is a very important concept about the functions or subroutines.

The purpose of a function or a subroutine is to simplify our programming by converting our low-level codes into high-level codes. This can never be achieved through looping because there is much more to functions and subroutines beside just repeating the same codes.

Above the int main() or main function,

void checkRegistrationEligibility() //make sure that the spellings are right
{
  string name;
  cout << "Enter your name: ";
  getline(cin, name);
  cout << "Your name is " << name << "." << endl;

  int age;
  cout << "Enter your age: "; cin >> age;
  if (age >= 18)
  {
    cout << name << ", you are eligible for registration." << endl;
    cout << "In other words, you can go to next section for registration!" << endl;
    cout << endl << "***Thank you for visiting us!***" << endl;
  }
  else
  {
    cout << name << ", we are sorry that you are not eligible for registration!" << endl;
    cout << "You can register after " << 18 - age << " years." << endl;
    cout << endl << "***Thank you for visiting us!***" << endl;
  }
}

Now, we can have only one line, to have our eligibility registration program running fine!

int main()
{
  checkRegistrationEligibility(); //make sure that the spellings are right
  return 0;
}

We will discuss the function/subroutine syntax in the upcoming post! For now, just understand the concept of function, subroutines, low-level codes and high-level codes. My definitions are mine and of course, may be inaccurate but they are the easiest definitions according to me. If you are interested in getting deeper concepts, here are some of the pages from Wikipedia:

Function (in terms of mathematics)
Subroutine
Low-level programming
High-level programming