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

3.access Modifiers

Uploaded by

Sadi Rifat
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)
10 views

3.access Modifiers

Uploaded by

Sadi Rifat
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/ 13

Features of OOP

(Access Modifiers)
Access Modifiers in C++

 Access Modifiers or Access Specifiers in a class are used to set the accessibility
of the class members.
 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
 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.
Access Modifier :: Public

 All the class members declared under public will be avail.


 The data members and member functions declared public can be accessed able to
everyone 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.
 In the below program the data member radius is public so we are allowed to
access it outside the class.
Access Modifier :: Public
Access Modifier :: 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.
Access Modifier :: Private
Access Modifier :: Private

 The output of the below program will be a compile time error because we are not
allowed to access the private data members of a class directly outside the class.
 However, we can access the private data members of a class indirectly using the
public member functions of the class.
Access Modifier :: Private
Access Modifier :: 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.
Access Modifier :: Protected
Access Modifier
 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.
Access Modifier in a View
Thank You

You might also like