Access modifiers in C++ control the accessibility of class members, with three types: Public, Private, and Protected. Public members are accessible from anywhere, Private members can only be accessed within the class, and Protected members are accessible in subclasses. Encapsulation is emphasized as a good practice to protect sensitive data by declaring attributes as private and using public methods for access.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views18 pages
UNIT-1 Access Specifier
Access modifiers in C++ control the accessibility of class members, with three types: Public, Private, and Protected. Public members are accessible from anywhere, Private members can only be accessed within the class, and Protected members are accessible in subclasses. Encapsulation is emphasized as a good practice to protect sensitive data by declaring attributes as private and using public methods for access.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18
• Access Modifiers or Access Specifiers in a class are
used to set the accessibility of the class members.
That is, it sets some restrictions on the class members not to get directly accessed by the outside functions. • There are 3 types of access modifiers available in C++: • Public • Private • Protected • Note: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private. Public All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. Private • The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class. • It is possible to access private members of a class using a public method inside the same class. (Encapsulation) • Tip: It is considered good practice to declare your class attributes as private (as often as you can). This will reduce the possibility of yourself (or others) to mess up the code. This is also the main ingredient of the Encapsulation concept, • 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 }; • 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. • 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: 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 Protected • Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.