0% found this document useful (0 votes)
31 views

C++ Class Access Specifiers

C++ class access modifiers determine how class members can be accessed. The keywords public, private, and protected specify access levels, with private being the most restrictive and public the least. By default, class members are private and can only be accessed by member functions of the same class. Protected members can also be accessed by derived classes. Public members can be accessed anywhere once an object is created.

Uploaded by

rajatbisht9124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

C++ Class Access Specifiers

C++ class access modifiers determine how class members can be accessed. The keywords public, private, and protected specify access levels, with private being the most restrictive and public the least. By default, class members are private and can only be accessed by member functions of the same class. Protected members can also be accessed by derived classes. Public members can be accessed anywhere once an object is created.

Uploaded by

rajatbisht9124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Class Access Modifiers

• Data hiding is one of the important features of Object Oriented


Programming which allows preventing the functions of a
program to access directly the internal representation of a class
type.

• The access restriction to the class members is specified by the


labeled public, private, and protected sections within the class
body.

• The keywords public, private, and protected are called access


specifiers.

• 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.
private

• A private member variable or function cannot be accessed, or even


viewed from outside the class.

• Only the member functions and friend functions can access private
members.

• By default all the members of a class would be private

• Practically, we define data in private section and related functions in


public section so that they can be called from outside of the class using
public member functions.

• Note: Refer my lecture for demonstration


protected

• A protected member variable or function is


very similar to a private member but it provided
one additional benefit that they can be accessed
in child classes which are called derived
classes.

• Example is demonstrated in class lecture; refer


there.
public
• All the class members declared under the public specifier will be
available to everyone.

• The data members and member functions declared as public can be


accessed by other classes and functions 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.

• Example is demonstrated in class lecture; refer there.


• Q. Demonstrate private methods.

• In a class data members or member functions can be private.

• Similar to data members the private member functions can not be


accessed directly outside the class.

• You can call private member functions using public member


function.

• Note: Similar for protected functions also.

• Example is demonstrated in class lecture; refer there.

You might also like