The concept of object oriented programming

What is object oriented programming?

In C, structures and functions are widely used. They are still used in C++, but usually not as much as classes. Object Oriented Programming (OOP) is one of the main features introduced by C++. OOP simply consist of programming using, mainly, class objects rather than functions and structure objects. Variables of structure type are also called objects, but the Object word from OOP stands for class object only.

Why using classes?

Structuring the code

Classes can be seen as improved structures. They help structuring the code by allowing functions (Called methods when inside a class) and variables (Called member variables when inside a class) that are related, to be grouped into classes.

We could, for example, create a class named Character that contains member variables to represent its life, energy, attack damage and target, and we could define, inside it, a method named attack, that would attack the object of type Character pointed by its member target, by dealing it the amount of damage defined in its member variable damage.

Methods, like functions, can receive arguments, but they also have access to the member variables of the objects they are called from.

Inheritance

Classes also introduce a concept called inheritance. By making a class inherit from an other class, it inherit all of the class methods and member variables. Let us say we define a class named Weapon with members to represent its damage, attack speed and price. We could define a class named Sword with a member named sharpness and make it inherit from Weapon. It would then contain all the members of Weapon, but also its member sharpness. We could also define a class named Mace, with a variable named weight, and make it inherit from Weapon. Both Sword and Mace would be special kinds of Weapon, but with additional features.

Virtual methods

Let us say we define an enumeration named DamageType and add to it the elements physical, pierce and none. Let us add a method which header is DamageType getDamageType(), that returns the element none, to the class Weapon and a member of type pointer to a Weapon object, to the class Character. Since both the class Sword and Mace inherit from Weapon, they both can be pointed to by pointers of type Weapon. That pointer could, however, only access their elements inherited from Weapon. Let us redefine the method DamageType getDamageType() in the class Sword, to make it return the enumeration element pierce, and in Mace to make it return physical. By making the method getDamageType what is called a virtual method, we could call it from a pointer of type Weapon and it would call the appropriate version of the method according to whether it points to an object of type Weapon, Sword or Mace. For example, if it points to an object of type Mace, calling the method getDamageType from the pointer would return the element physical of the enumeration DamageType. If it points to an object of type Weapon, it would be the element none that is returned, and pierce for an object of type Sword. We could then easily retrieve the type of a damage a Character deals, depending on the type of Weapon he is equipped with.

I hope the example above gave you a good idea of how classes help structure the code of a program. Now, let's start learning to use them!