In C++ access modifiers allow you to control access to the members of a class, facilitating the management 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.
If you want to learn more about Encapsulation
check the Object-Oriented Programming Course read more ⯈
Access Modifiers
Access modifiers determine the visibility of the members of a class from outside of it.
Modifier | Accessible from |
---|---|
public | From anywhere. |
protected | From the base class and derived classes. |
private | Only 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 directly modified 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 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
, 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, 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, which means it cannot be accessed directly from outside the inheritance hierarchy.