Class 10 Exam  >  Class 10 Notes  >  C++ Programming for Beginners  >  C++ Access Specifiers & C++ Encapsulation

C++ Access Specifiers & C++ Encapsulation | C++ Programming for Beginners - Class 10 PDF Download

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 | C++ Programming for Beginners - Class 10 is a part of the Class 10 Course C++ Programming for Beginners.
All you need of Class 10 at this link: Class 10
15 videos|20 docs|13 tests

Top Courses for Class 10

15 videos|20 docs|13 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Exam

,

study material

,

video lectures

,

Sample Paper

,

mock tests for examination

,

MCQs

,

Viva Questions

,

ppt

,

shortcuts and tricks

,

Objective type Questions

,

C++ Access Specifiers & C++ Encapsulation | C++ Programming for Beginners - Class 10

,

Extra Questions

,

Previous Year Questions with Solutions

,

Free

,

Semester Notes

,

C++ Access Specifiers & C++ Encapsulation | C++ Programming for Beginners - Class 10

,

Important questions

,

Summary

,

past year papers

,

pdf

,

C++ Access Specifiers & C++ Encapsulation | C++ Programming for Beginners - Class 10

;