• What is a class with an example?
  • What is an object?
  • What are the methods? how to create & access them using objects?
  • How to define a method outside of class?

What is a class?

A class is an object-oriented paradigm or model used to group both data & functionality together. Classes provide additional features like access restriction which we will discuss in detail while going further.

class keyword is used to create the class in C++. Following is an example of how to create a class in C++.

#include <iostream>

class Player {
	
 public:
    /* Data */
    int posX = 0;
    int posY = 0;
    int pScore = 0;
    
    /* Functionality that acts on the data */
    void movePlayer(int x, int y){
    	// Updates the player position
        posX = x;
        posY = y;
    }
    
    void updateScore(int score){
    	pScore = score;
    }
}

int main(){
	
    // Create an object or instance of a class
    Player p1;
    p1.movePlayer(3, 4);
    p1.updateScore(10); 
    
    Player p2;
    p1.movePlayer(434, 13434);
    p1.updateScore(100);
}

What are the methods?

A class can have member variables & member functions. Member functions are also known as methods.

In the above example, we have member variables posX, posY, pScore are also referred to as data of the player & the methods such as movePlayer, updateScore are player actions that act on the player data. We grouped both of them together using the classes.

What is an object?

An object is a named region of storage or memory.

When we declare a class, it’s like just telling the compiler about the model or blueprint of storage or memory that we need to use the class. Unless we create an object, no memory will be allocated to the class.

In the above example, P1 is an object of the Player class that we created to store and manage Player1’s data. Similarly, if we have N number of players, we can create N number of objects.

Player p1;

Accessing members of a class

Once we declare an object for a class, the required memory or storage space will be allocated for it. We can just use the <object name>.<member_name> to right away access the class members. As I told you, the object is just a name to the memory or storage location of these class members.

In the above example, you can see I accessed the movePlayer function using the object of a Player.

p1.movePlayer(3, 4);

Access modifiers

The accessibility of class members from outside of a class can be controlled by using three access modifiers. They are public, private & protected.

As of now just remember that the default access modifier of a class is private. In our further articles, we discuss how to use access modifiers in our program.

Scope resolution operator

C++ allows defining the class method outside of a class. Meaning the scope of a method is bound to that class even though it is declared outside. This can be done by using the scope resolution(::) operator.

It is always a good practice to define the methods outside of the class using the scope resolution operator if they contain complex logic. The methods defined inside the class are converted to the inline functions and the instructions are copied at the calling function stack.

Let’s see an example of defining a method outside of a class:

#include <iostream>

class Calculator{
	
    public:

    // Defined inside the class
    int add(int a, int b){
        return a + b;
     }
    
    // Just declared the prototype
    int sub(int a, int b);
 
};

int Calculator::sub(int a, int b){
    return a - b;
}

int main(){
    Calculator obj;
    std::cout << "Addition of 2 & 3 is : " << obj.add(2, 3);
    std::cout << "Subtraction of 2 & 3 is : " << obj.sub(2, 3);
    return 0;
}

In the above example, method sub is defined outside of a class and we are able to access it through the object of that class.

Note: To define the method outside of a class, we must declare its prototype inside the class

Categorized in:

Tagged in: