Created by Turbolearn AI
Access Specifiers in C++
One of the main advantages of using object-oriented programming (OOP) is data
security. In C++, this is achieved through access specifiers, which control the
visibility and accessibility of class members. There are three types of access
specifiers:
private
public
protected
Public Access Specifier
Members declared as public are accessible from anywhere, both inside and outside
the class.
Public class members are accessible everywhere, both inside and outside
the class.
In previous examples, all class members (attributes and methods) were declared as
public, allowing direct access using the object of the class.
Private Access Specifier
Members declared as private are only accessible from within the class itself, and by
friends of the class.
Private class members are accessible only by the members of the class or
the friends of that class.
class Account {
private:
string name;
double balance;
public:
void deposit(double amount);
void withdraw(double amount);
void display();
};
Page 1
Created by Turbolearn AI
In the example above, name and balance are private, so they cannot be directly
accessed from outside the Account class. However, they can be accessed by the
public methods deposit, withdraw, and display.
Analogy: If your laptop is private, only you, your family, or your friends can access it.
Example Demonstrating Private Access Specifiers
#include <iostream>
#include <string>
class Account {
private:
std::string name;
double balance;
public:
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
balance -= amount;
}
void display() {
std::cout << "Updated balance is: " << balance << std::endl;
}
};
int main() {
Account JennyAccount;
// JennyAccount.name = "Jenny Katri"; // Error: name is private
// JennyAccount.balance = 1000.0; // Error: balance is private
JennyAccount.deposit(1000.0); // This works
JennyAccount.display(); // This works
return 0;
}
Accessing Private Members Through Public Methods
Private members can be accessed indirectly through public methods. These public
methods act as an interface to the private data.
Page 2
Created by Turbolearn AI
Analogy: Your phone number is private, but your friend's phone number is public.
Your friend can share your number if they want to.
Getters and Setters
To control access to private members, classes often use "getter" and "setter"
methods. Getter methods retrieve the value of a private member, while setter
methods allow modifying the value of a private member. These methods are typically
declared as public.
Protected Access Specifier
The protected access specifier is mainly used in the context of inheritance. Inheritance
is when a class inherits properties from another class.
Protected members of a base class can be accessed by the derived class,
but not from outside the class hierarchy.
If we have class A as a father and class B as a son, the protected members of A are
inheritable by B.
Summary of Access Specifiers
Access Specifier Accessibility
public Accessible from anywhere.
private Accessible only within the class and by friends.
protected Accessible within the class and by derived classes.
Benefits of Using Private Access Specifiers
Data Security: Making data private ensures that it cannot be directly accessed
or modified from outside the class.
Debugging: Private access specifiers make debugging manageable by
localizing the sources of error. You only need to check the public interfaces
through which private members are accessed.
Access Specifiers Assignment
Page 3
Created by Turbolearn AI
The assignment involves working with access specifiers, specifically private, public,
and introducing the concept of protected (though the latter will be explored more in
a later video on inheritance).
The task is to create a class with the following attributes and methods:
Attributes:
name (private)
balance (private)
Methods:
deposit()
withdraw()
display()
Implementing the Assignment
1. Setting up the Class:
Create a class with name and balance as private members. This means
they cannot be directly accessed or modified from outside the class.
2. Creating Methods:
Implement the deposit() and withdraw() methods to modify the balance.
Implement the display() method to show the name and balance.
3. Setting the Name:
Since name is private, you cannot directly set it using object.name = "J
Katri".
Create a public method, such as setName(), within the class. This method
should take a name as input and assign it to the private name attribute.
4. Displaying Information:
Modify the display() method to output both the name and balance.
Purpose
Page 4
Created by Turbolearn AI
The exercise aims to solidify understanding of access specifiers and how they control
the accessibility of class members. By making name and balance private, direct
modification from outside the class is prevented, enforcing encapsulation. You must
use a public method to indirectly modify the private name attribute, demonstrating
controlled access.
Page 5