0% found this document useful (0 votes)
25 views11 pages

C++ Access Modifiers

Uploaded by

ketankumar2614
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)
25 views11 pages

C++ Access Modifiers

Uploaded by

ketankumar2614
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/ 11

C++ Access Modifiers

Raj Kumar
C++ Access Modifiers
 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.
Types of C++ Access Modifiers

1.Public
2.Private
3.Protected
Public, Private, and Protected
1. Public elements can be accessed by all other
classes and functions.
2. Private elements cannot be accessed outside
the class in which they are declared, except
by friend classes and functions.
3. Protected elements are just like the private,
except they can be accessed by derived
classes.
Public, Private, and Protected
Same Derived Outside
Specifiers
class class class

Public Yes Yes Yes

Private Yes No No

Protected Yes Yes No


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.
Public Access Modifier
#include <iostream>
using namespace std;

// define a class
class Sample {

// public elements
public:
int age;

void displayAge() {
cout << "Age = " << age << endl;
}
};
void main() {

// declare a class object


Sample obj1;
cout << "Enter your age: ";
// store input in age of the obj1 object
cin >> obj1.age;
// call class function
obj1.displayAge();
getch();
}
Private Access Modifier
 The private keyword is used to create private
members (data and functions).
 The private members can only be
accessed from within the class.
 However, friend classes and friend functions
can access private members.
#include <iostream>
// define a class
class Sample {

// private elements
private:
int age;

// public elements
public:
void displayAge(int a) {
age = a;
cout << "Age = " << age << endl;
}
};
Void main() {

int ageInput;

// declare an object
Sample obj1;

cout << "Enter your age: ";


cin >> ageInput;

// call function and pass ageInput as argument


obj1.displayAge(ageInput);
getch();
}
Protected Access Modifier
 Before we learn about the protected access
specifier, make sure you know about
inheritance in C++.
 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.
#include <iostream>
// declare parent class
class Sample {
// protected elements
protected:
int age;
};

// declare child class


class SampleChild : public Sample {

public:
void displayAge(int a) {
age = a;
cout << "Age = " << age << endl;
}

};
void main() {
int ageInput;

// declare object of child class


SampleChild child;

cout << "Enter your age: ";


cin >> ageInput;

// call child class function


// pass ageInput as argument
child.displayAge(ageInput);
}

You might also like