Exploring C++ Access Specifiers - CSU1287 - Shoolini U

Access Specifiers

Access specifiers

Access specifiers are keywords in C++ that determine the visibility and accessibility of class members. They are an essential feature of object-oriented programming, as they allow you to control how data and behavior are exposed to other parts of the program. Access specifiers can be used to hide implementation details, enforce data integrity, and improve code readability and maintainability.

There are three access specifiers in C++: public, private, and protected. Each of these access specifiers defines a different level of access to class members.

Public access specifier:

The public access specifier allows class members to be accessed from anywhere in the program, including outside the class. Public members are used to provide the interface of the class, which is the set of functions and data members that other parts of the program can interact with. Public members are declared using the keyword public, followed by a colon (:), and then the list of member declarations.

Here's an example of a class with public members:

class Person {
public:
  void setName(const char* name) {
    this->name = name;
  }
  const char* getName() const {
    return name;
  }
private:
  const char* name;
};

In this example, the Person class has two public member functions: setName() and getName(). The setName() function takes a C-style string as an argument and sets the name data member to that value. The getName() function returns the value of the name data member. These functions provide the interface of the class, allowing other parts of the program to set and retrieve the person's name.

Private access specifier:

The private access specifier restricts access to class members to the class itself. Private members are used to implement the internal details of the class and hide them from other parts of the program. Private members are declared using the keyword private, followed by a colon (:), and then the list of member declarations.

Here's an example of a class with private members:

class BankAccount {
public:
  void deposit(double amount) {
    balance += amount;
  }
  void withdraw(double amount) {
    balance -= amount;
  }
  double getBalance() const {
    return balance;
  }
private:
  double balance = 0.0;
};

In this example, the BankAccount class has three public member functions: deposit(), withdraw(), and getBalance(). These functions allow other parts of the program to deposit, withdraw, and retrieve the account balance. The balance data member is declared as private, so it can only be accessed by the class itself. This ensures that the account balance is not modified by unauthorized code.

Protected access specifier:

The protected access specifier is similar to the private access specifier, but it allows derived classes to access the class members. Protected members are used to implement the common behavior of a set of related classes and share the implementation details with derived classes. Protected members are declared using the keyword protected, followed by a colon (:), and then the list of member declarations.

Here's an example of a class with protected members:

class Animal {
protected:
  void eat() {
    std::cout << "The animal is eating.\n";
  }
  void sleep() {
    std::cout << "The animal is sleeping.\n";
  }
};

class Dog : public Animal {
public:
  void bark() {
    std::cout << "The dog is barking.\n";
  }
};

In this example, the Animal class has two protected member functions: eat() and sleep(). These functions implement the common behavior of animals and are shared with derived classes. The Dog class is a derived class of the Animal class and has a public member function named bark(). The bark() function is specific to dogs and is not shared with other classes.

In this example, the protected access specifier allows the Dog class to access the eat() and sleep() member functions of the Animal class, which are used to implement the common behavior of animals.

Overall, access specifiers are a powerful tool for controlling the visibility and accessibility of class members in C++. By using public, private, and protected access specifiers, you can encapsulate data and behavior, hide implementation details, and enforce data integrity.