In C++, access modifiers allow controlling access to class members, facilitating the management of data visibility and protection.
In C++, the access modifiers are public, protected, and private. Each of these modifiers defines a specific level of accessibility for class members.
Access modifiers implement the principle of Encapsulation, one of the pillars of object-oriented programming.
If you want to learn more, check out the Object-Oriented Programming Course.
Access Modifiers
Access modifiers determine the visibility of class members from outside the class.
| Modifier | Accessible from |
|---|---|
public | From anywhere. |
protected | From the base class and derived classes. |
private | Only within the class itself. |
Public Modifier
The public modifier allows class members to be accessible from any part of the code. This is useful for defining a class’s public interface (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 Brand and Model attributes and the Start method are public and can be accessed from outside the Vehicle class.
Private Modifier
The private modifier restricts access to class members to the class itself. This ensures that internal data cannot be modified directly from outside the class, promoting encapsulation and protecting data integrity.
#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 balance attribute is declared as private, meaning it can only be accessed within the BankAccount class.
Protected Modifier
The protected modifier allows class members to be accessible within the class itself and in derived classes. This is useful for allowing derived classes to access certain data or methods from 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 Eat method is protected, so 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, 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 ProtectedMethod method is protected in the base class and remains protected in the derived class, meaning it cannot be accessed directly from outside the inheritance hierarchy.
