Operator overloading in C++

Operators are a fundamental part of programming languages. They allow us to perform operations on operands by using a symbol. If you have ever written code, you probably know what this snippet does:

1
int a = 3 + 1;

Variable a will be initialized to 4. It is initialized to this value, because the + operator has been used to add the values of 3 and 1. The = sign, is also an operator that assigns the value of the addition to the variable a.

We use these operators without thinking too much about them, but they are just symbols that perform a certain action, like any function we could define.

It turns out we can override the behavior of an existing operator. That’s what this article will focus on.

Read More

Configuring gnome terminal programmatically

As part of getting a new computer, I want to be able to run a script to configure gnome terminal to my preferred setup. Doing this is not very hard, but finding how to do it took me some time.

The first thing we need to do is get the default terminal profile id:

1
2
$ gsettings get org.gnome.Terminal.ProfilesList default
'b1dcc9dd-5262-4d8d-a863-c897e6d979b9'
Read More

C++ value categories

In C++, all expressions have two properies: a type and a value category. Value categories are a little complicated to understand and have gone through a few changes with time.

lvalue and rvalue

Lvalue stands for locator value. An lvalue is something that occupies a memory location. But… doesn’t everything occupy a memory location? Not necessarily. Let’s look at a simple expression:

1
int number = 7;
Read More

Exception handling in C++

In this post I’m going to write about using exceptions for error handling in C++.

Exceptions

They are called exceptions, because they happen in exceptional scenarios. A program should in most cases run without exceptions, but sometimes unexpected things happen and we might want to do something in those scenarios.

Exceptions happen when there is either a developer error (Dereferencing a null pointer) or an environment error (Trying to write to a disk that is full). They should not happen for user triggered errors (incorrect input by user), if those errors are common.

When an error such as dereferencing a null pointer occurs, an Exception object is created. The Exception object contains information about the error, and the state of the program at the time this happened. The exception will then be thrown and left for the runtime to handle.

Read More

Configuring gnome properties programmatically

After installing a new distribution in my computer, I usually want to make some tweaks to gnome. In this post I’m going to explain how to do these tweaks from the command line, so they can be scripted.

gsettings

Gsettings is a command-line tools that allows us to modify gnome settings. To modify a setting we can use the set command. The format is like the following:

1
gsettings set SCHEMA[:PATH] KEY VALUE
Read More

Hide snap folder in Ubuntu

Ubuntu recently introduced snaps. Snaps are a new way of packaging applications in a way that there will be no dependency conflicts (because all dependencies are included). The only problem is that Ubuntu will create a snap folder in your home folder that you will most likely never need to access.

If you, like me, find this folder annoying, you can hide it from Nautilus:

1
2
cd ~
echo snap >> ~/.hidden

This makes the folder invisible in Nautilus, but it will still be visible in other places (The terminal, for example).

Read More

How to configure Firefox programmatically

I reinstalled Ubuntu on my personal computer recently and I noticed that there are some things I don’t like about the default distribution. One of the things I noticed is that Firefox keeps asking me if I want it to remember my passwords, which I don’t.

I know that I can go to settings and disable this feature, but I wanted to learn how to do it programmatically, so in the future I can just run a script and have Firefox work the way I want.

Preferences

There is documentation explaining how preferences work for mozilla projects, but it’s a little hard to understand how to exactly do what I wanted to do.

Read More

Virtual functions in C++

If you are not familiar with Inheritance, I recommend you read my short article about inheritance first.

One of the features of Object Oriented Programming is Polymorphism. Virtual functions in C++ allow developers to achieve run-time polymorphism by overwriting methods of a base class.

In my article about inheritance, I showed how we can create classes that inherit from a base (or parent) class. Something similar to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Greeter {
 public:
  void talk() {
    std::cout << "hello" << std::endl;
  }
};

class SpanishGreeter : public Greeter {
 public:
  void talk() {
    std::cout << "hola" << std::endl;
  }
};
Read More

Inheritance in C++

Inheritance is a feature of Object Oriented Programming that allows programmers to create classes based on other classes. A class that inherits from another class will have the same behavior as the parent class unless it is overwritten.

Let’s say we have a class:

1
2
3
4
5
6
7
8
9
class Greeter {
 public:
  void talk() {
    std::cout << greeting_ << std::endl;
  }

 protected:
  std::string greeting_ = "hello";
};
Read More

Enums in C++

Enumerations are useful when we want to have a custom type, that can only have a limited number of values. For example:

1
2
3
4
5
enum Mode {
  EASY,
  MEDIUM,
  HARD
};

We can create variables of type enum that can only have one of the previously defined values:

1
Mode userMode = EASY;
Read More