classes and objects 

Imagine that we need to write a program that allows us to display information about different animal species. One way would be to write code for every animal that we want to include in our program. Another way would be to use classes and objects.

class

A class is a blueprint. Instead of repeating ourselves with data that is the same for some species of animals, we could create classes for each animal and then create objects for each species. A class has attributes and behaviours. An attribute could be how many legs it has and if it is a land animal. A behaviour could be the animal moving. A class is a user-defined datatype. The attributes will be represented by variables.

class Cat {
  public:
    string name;
    double legs;
    string land;
    string fly;
};

object

An object is an instance of a class.

A class could be animal and an object could be a bird.

access modifiers

An important concept of OOP in C++ is data hiding. Data hiding allows access to data members to be restricted. This is done to prevent classes and functions from tampering with class data! However, it is also important that some member functions and member data can access the hidden data (this is done indirectly). To achieve these, we use access modifiers. These access modifiers allow us to decide which class members are accessible to other classes and functions, and which are not.

There are three access modifiers: public, private and protected.

public

The public keyword is used to create public members (data and functions). The public members are accessible from any part of the program.

private

The private keyword is used to create private members (data and functions). The private members can only be accessed from within the class.

protected

The protected keyword is used to create protected members (data and functions). The protected members can be accessed within the class and from the derived class.