Access
Modifiers in C++: Controlling Access to a Class
In C++, access modifiers are keywords that determine the accessibility
or visibility of data members and member functions within a class. They control
how the members of a class can be accessed from outside the class and play a
crucial role in encapsulation, one of the key principles of object-oriented
programming (OOP).
Types of Access Modifiers
C++ provides three primary access modifiers:
Public: Members declared as `public` are accessible from
anywhere in the program. There are no restrictions on their access.
Private: Members declared as `private` are accessible only
within the class or by friends of the class. They are not accessible from
outside the class.
Protected: Members declared as `protected` are similar to
`private` members, but they can also be accessed in derived classes
(subclasses).
1. Public Access Modifier:
When a class member is declared `public`, it can be
accessed from any part of the program. This means that the member functions and
data members marked as `public` can be used directly by creating objects of the
class or by using pointers and references to objects.
Syntax:
class
ClassName {
public:
// Public members
};
Example:
class
Employee {
public:
string name;
int id;
void display()
{
cout << "Name: "
<< name << ", ID: " << id << endl;
}
};
int
main() {
Employee emp;
emp.name = "Alice"; // Accessing public member directly
emp.id = 101;
emp.display(); // Calling public member function
return 0;
}
In this example, `name` and `id` are public data
members and can be accessed directly using the object `emp`.
2. Private Access Modifier
When a class member is declared `private`, it can only
be accessed from within the class itself. Private members are not accessible
directly from outside the class. This is useful for hiding the internal details
and implementation of the class, which is a key aspect of encapsulation.
Syntax:
class
ClassName {
private:
// Private members
};
Example:
class
BankAccount {
private:
double balance;
public:
void setBalance(double amount) {
balance = amount;
}
double getBalance() {
return balance;
}};
int
main() {
BankAccount account;
account.setBalance(5000.50); // Setting balance via public function
cout << "Balance: $"
<< account.getBalance() << endl;
// Accessing balance via public function
// account.balance = 1000; // Error: Cannot access private member
directly
return 0;
}
In this example, `balance` is a private data member.
It can only be accessed or modified using the public member functions
`setBalance()` and `getBalance()`.
3. Protected Access Modifier
When a class member is declared `protected`, it
behaves like a private member but with an additional feature: it can be
accessed in derived classes. Protected members are used in inheritance, where
the base class members need to be accessible in the derived class.
Syntax:
class
ClassName {
protected:
// Protected members
};
Example:
class
Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
};
class
Student : public Person {
public:
void display() {
cout << "Name: "
<< name << endl; //
Accessing protected member in derived class
}
};
int
main() {
Student student;
student.setName("Bob");
student.display();
return 0;
}
In this example, `name` is a protected member of the
`Person` class. It is accessed in the `Student` class, which is derived from
`Person`.
4. Default Access Modifier
In C++, if no access modifier is specified, the
default access level depends on the context:
- For class members, the default access level is
`private`.
- For struct members, the default access level is
`public`.
Example:
class Example {
int value; // Default is private
};
struct ExampleStruct {
int value; // Default is public
};
Summary:
Public: Members are accessible from any part of the
program.
Private: Members are accessible only within the class
and not from outside.
Protected: Members are accessible within the class and
in derived classes.
Access Modifier |
Scope |
Direct Access |
Accessed From |
Usage Example |
Public |
Accessible everywhere |
Yes |
Anywhere in the program |
public int id; |
Private |
Accessible only within the class |
No |
Only within the class or by friend functions |
private double balance; |
Protected |
Accessible within the class and derived classes |
No |
Within the class and its derived classes |
protected string name; |
Access modifiers are essential in implementing data
hiding and encapsulation, which help in protecting the integrity of data and
defining clear interfaces for the use of a class.
No comments:
Post a Comment