Class 8  >  C++ Programming for Beginners  >  C++ Access Specifiers & C++ Encapsulation

C++ Access Specifiers & C++ Encapsulation - Notes | Study C++ Programming for Beginners - Class 8

1 Crore+ students have signed up on EduRev. Have you?

Access Specifiers

By now, you are quite familiar with the public keyword that appears in all of our class examples:
Example

class MyClass {  // The class

  public:        // Access specifier

    // class members goes here

};

The public keyword is an access specifier. Access specifiers define how the members (attributes and methods) of a class can be accessed. In the example above, the members are public - which means that they can be accessed and modified from outside the code.
However, what if we want members to be private and hidden from the outside world?

In C++, there are three access specifiers:

  • public - members are accessible from outside the class
  • private - members cannot be accessed (or viewed) from outside the class
  • protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes. You will learn more about Inheritance later.

In the following example, we demonstrate the differences between public and private members:
Example

class MyClass {

  public:    // Public access specifier

    int x;   // Public attribute

  private:   // Private access specifier

    int y;   // Private attribute

};


int main() {

  MyClass myObj;

  myObj.x = 25;  // Allowed (public)

  myObj.y = 50;  // Not allowed (private)

  return 0;

}

If you try to access a private member, an error occurs:

error: y is private

Note: By default, all members of a class are private if you don't specify an access specifier:
Example

class MyClass {

  int x;   // Private attribute

  int y;   // Private attribute

};

C++ Encapsulation

Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.

Access Private Members

To access a private attribute, use public "get" and "set" methods:
Example

#include <iostream>

using namespace std;


class Employee {

  private:

    // Private attribute

    int salary;


  public:

    // Setter

    void setSalary(int s) {

      salary = s;

    }

    // Getter

    int getSalary() {

      return salary;

    }

};


int main() {

  Employee myObj;

  myObj.setSalary(50000);

  cout << myObj.getSalary();

  return 0;

}

Example Explained

The salary attribute is private, which have restricted access.
The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary = s).
The public getSalary() method returns the value of the private salary attribute.
Inside main(), we create an object of the Employee class. Now we can use the setSalary() method to set the value of the private attribute to 50000. Then we call the getSalary() method on the object to return the value.

Why Encapsulation?

  • It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts
  • Increased security of data
The document C++ Access Specifiers & C++ Encapsulation - Notes | Study C++ Programming for Beginners - Class 8 is a part of the Class 8 Course C++ Programming for Beginners.
All you need of Class 8 at this link: Class 8
15 videos|20 docs|13 tests
Download as PDF

Download free EduRev App

Track your progress, build streaks, highlight & save important lessons and more!

Related Searches

MCQs

,

practice quizzes

,

Exam

,

C++ Access Specifiers & C++ Encapsulation - Notes | Study C++ Programming for Beginners - Class 8

,

Summary

,

Objective type Questions

,

C++ Access Specifiers & C++ Encapsulation - Notes | Study C++ Programming for Beginners - Class 8

,

video lectures

,

Extra Questions

,

Free

,

shortcuts and tricks

,

mock tests for examination

,

past year papers

,

ppt

,

Previous Year Questions with Solutions

,

study material

,

Important questions

,

pdf

,

Viva Questions

,

C++ Access Specifiers & C++ Encapsulation - Notes | Study C++ Programming for Beginners - Class 8

,

Semester Notes

,

Sample Paper

;