0% found this document useful (0 votes)
9 views15 pages

C++ Access Modifiers

C++
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)
9 views15 pages

C++ Access Modifiers

C++
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/ 15

C++ Access Modifiers

Introduction
• One of the main features of object-oriented
programming languages such as C++ is data hiding.
• Data hiding refers to restricting access to data members
of a class.
• This is to prevent other functions and classes from
tampering with the class data.
• However, it is also important to make some member
functions and member data accessible so that the
hidden data can be manipulated indirectly.
• The access modifiers of C++ allows us to determine
which class members are accessible to other classes
and functions, and which are not.
Example
class Patient {
private:
int patientNumber;
string diagnosis;
public:
void billing() {
// code
}
void makeAppointment() {
// code
}
};
• Here, the variables patientNumber and diagnosis of the
Patient class are hidden using the private keyword,
while the member functions are made accessible using
the public keyword.
C++ Access Modifiers
• There are three access modifiers:
i. public
ii. private
iii. Protected
Public Access Modifier
• The public keyword is used to create public members
(data and functions).
• The public members are accessible from any part of the
program.
Example
• A program
• In this program, we have created a class named
Sample, which contains a public variable age and a
public function displayAge().
• In the main(), we have created an object of the Sample
class named obj1.
• We then access the public elements directly by using
the codes obj1.age and obj1.displayAge().
• Notice that the public elements are accessible from
main(). This is because public elements are accessible
from all parts of the program.
Private Access Modifier
• The private keyword is used to create private members
(data and functions).
• The private members can only be accessed within the
class.
• However, friend classes and friend functions can access
private members.
Example
• Program
• In main(), the object obj1 cannot directly access the
class variable age.
// error
cin >> obj1.age;
• We can only indirectly manipulate age through the
public function displayAge(), since this function
initializes age with the value of the argument passed to
it i.e. the function parameter int a.
Protected Access Modifier
• The protected keyword is used to create protected
members (data and function).
• The protected members can be accessed within the
class and from the derived class.
• Program
• Here, SampleChild is an inherited class that is derived
from Sample.
• The variable age is declared in Sample with the
protected keyword.
• This means that SampleChild can access age since
Sample is its parent class.
• We see this as we have assigned the value of age in
SampleChild even though age is declared in the Sample
class.
• Note: By default, class members in C++ are private,
unless specified otherwise.

You might also like