Language: EN

cpp-modificadores-acceso

Access Modifiers in C++

In C++ access modifiers allow controlling access to the members of a class, facilitating the handling of visibility and data protection.

In C++, the access modifiers are public, protected, and private. Each of these modifiers defines a specific level of accessibility for the members of a class.

Access modifiers implement the principle of Encapsulation, one of the pillars of object-oriented programming.

Access Modifiers

Access modifiers determine the visibility of the members of a class from outside it.

ModifierAccessible from
publicFrom anywhere.
protectedFrom the base class and derived classes.
privateOnly within the class.

public Modifier

The public modifier allows the members of a class to be accessible from anywhere in the code. This is useful for defining the public interface of a class (i.e., the methods and attributes that other components can use to interact with it).

#include <iostream>
#include <string>

class Vehicle
{
public:
    std::string Brand;
    std::string Model;

    void Start()
    {
        std::cout << Brand << " " << Model << " has started." << std::endl;
    }
};

int main()
{
    Vehicle myVehicle;
    myVehicle.Brand = "Toyota";
    myVehicle.Model = "Corolla";
    myVehicle.Start(); // Access allowed

    return 0;
}

In this example, both the attributes Brand and Model as well as the method Start are public and can be accessed from outside the Vehicle class.

private Modifier

The private modifier restricts access to the members of the class to the class itself. This ensures that internal data cannot be modified directly from outside the class, promoting encapsulation and protecting the integrity of the data.

#include <iostream>

class BankAccount
{
private:
    double balance;

public:
    BankAccount(double initialBalance) : balance(initialBalance) {}

    void Deposit(double amount)
    {
        balance += amount;
    }

    double GetBalance() const
    {
        return balance;
    }
};

int main()
{
    BankAccount myAccount(1000.0);
    myAccount.Deposit(500.0);
    std::cout << "Balance: " << myAccount.GetBalance() << std::endl; // Access allowed

    // myAccount.balance = 2000.0; // Error: 'balance' is private

    return 0;
}

Here, the attribute balance is declared as private, which means it can only be accessed within the BankAccount class.

protected Modifier

The protected modifier allows the members of a class to be accessible within the class itself and in derived classes. This is useful for allowing derived classes to access certain data or methods of the base class while keeping these members hidden from other users of the class.

#include <iostream>

class Animal
{
protected:
    void Eat()
    {
        std::cout << "The animal is eating." << std::endl;
    }
};

class Dog : public Animal
{
public:
    void Bark()
    {
        Eat(); // Access allowed
        std::cout << "The dog is barking." << std::endl;
    }
};

int main()
{
    Dog myDog;
    myDog.Bark(); // Access allowed

    // myDog.Eat(); // Error: 'Eat' is protected

    return 0;
}

In this case, the method Eat is protected, meaning it can be called from the derived class Dog, but not from outside the inheritance hierarchy.

protected Modifier in Inheritance

When using protected in inheritance, the protected members of the base class remain protected in the derived class, while public members become protected.

#include <iostream>

class Base
{
protected:
    void ProtectedMethod()
    {
        std::cout << "Protected method in the base class." << std::endl;
    }
};

class Derived : protected Base
{
public:
    void CallMethod()
    {
        ProtectedMethod(); // Access allowed
    }
};

int main()
{
    Derived obj;
    obj.CallMethod(); // Access allowed

    // obj.ProtectedMethod(); // Error: 'ProtectedMethod' is protected

    return 0;
}

In this example, the method ProtectedMethod is protected in the base class and remains protected in the derived class, meaning it cannot be accessed directly from outside the inheritance hierarchy.