SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
C++ ACCESS SPECIFIERS
C++ ACCESS SPECIFIERS
 C++ access specifiers are used for determining or setting the boundary for the
availability of class members (data members and member functions) beyond that
class.
For example, the class members are grouped into sections, private protected and
public. These keywords are called access specifiers which define the accessibility
or visibility level of class members.
By default the class members are private. So if the visibility labels are missing then
by default all the class members are private.
In inheritance, it is important to know when a member function in the base class
can be used by the objects of the derived class. This is called accessibility and the
access specifiers are used to determine this.
C++ ACCESS SPECIFIERS
Access specifier can be either private or protected or public. In
general access specifiers are the access restriction imposed during the
derivation of different subclasses from the base class.
 private access specifier
 protected access specifier
 public access specifier
VISIBILITY MODES
VISIBILITY MODES
Public: When the member is declared as public, it is accessible to all
the functions of the program.
Private: When the member is declared as private, it is accessible
within the class only.
Protected: When the member is declared as protected, it is
accessible within its own class as well as the class immediately
derived from it.
PRIVATE ACCESS SPECIFIER
• If private access specifier is used while creating a class, then the
public and protected data members of the base class become the
private member of the derived class and private member of base
class remains private.
• In this case, the members of the base class can be used only within
the derived class and cannot be accessed through the object of
derived class whereas they can be accessed by creating a function
in the derived class.
PRIVATE ACCESS SPECIFIER
SAMPLE PROGRAM DEMONSTRATING PRIVATE
ACCESS SPECIFIER
• Note: Declaring data members with private access specifier is known as data hiding.
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
SAMPLE PROGRAM DEMONSTRATING PRIVATE
ACCESS SPECIFIER
public:
int z;
base() //constructor to initialize data
members
{
x = 1;
y = 2;
z = 3;
}
};
class derive: private base
{
//y and z becomes private members of class
derive and x remains private
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
SAMPLE PROGRAM DEMONSTRATING PRIVATE
ACCESS SPECIFIER
};
int main()
{
derive a; //object of derived class
a.showdata();
return 0;
} //end of program
Output
x is not accessible
value of y is 2
value of z is 3
EXPLANATION
• When a class is derived from the base class with private access specifier the
private members of the base class can’t be accessed. So in above program, the
derived class cannot access the
• So in above program, the derived class cannot access the member x which is
private in the base class, however, derive class has access to the protected and
public members of the base class. So the
• Hence the function showdata in derived class can access the public and
protected member of the base class.
PROTECTED ACCESS SPECIFIER
• If protected access specifier is used while deriving class then the
public and protected data members of the base class becomes the
protected member of the derived class and private member of the
base class are inaccessible.
• In this case, the members of the base class can be used only within
the derived class as protected members except for the private
members.
PROTECTED ACCESS SPECIFIER
SAMPLE PROGRAM DEMONSTRATING PROTECTED
ACCESS SPECIFIER
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data
members
{
x = 1;
y = 2;
z = 3;
}
};
SAMPLE PROGRAM DEMONSTRATING PROTECTED
ACCESS SPECIFIER
class derive: protected base
{
//y and z becomes protected members
of class derive
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
};
int main()
{
derive a; //object of derived class
a.showdata();
return 0; }
SAMPLE PROGRAM DEMONSTRATING PROTECTED
ACCESS SPECIFIER
Output
x is not accessible
value of y is 2
value of z is 3
PUBLIC ACCESS SPECIFIER
• If public access specifier is used while deriving class then
the public data members of the base class becomes the
public member of the derived class and protected
members becomes the protected in the derived class but
the private members of the base class are inaccessible.
PUBLIC ACCESS SPECIFIER
SAMPLE PROGRAM DEMONSTRATING PUBLIC
ACCESS SPECIFIER
#include <iostream>
using namespace std;
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data
members
{
x = 1;
y = 2;
z = 3;
}
};
SAMPLE PROGRAM DEMONSTRATING PUBLIC
ACCESS SPECIFIER
class derive: public base
{
//y becomes protected and z becomes
public members of class derive
public:
void showdata()
{
cout << "x is not accessible" << endl;
cout << "value of y is " << y << endl;
cout << "value of z is " << z << endl;
}
};
int main()
{
derive a; //object of derived class
a.showdata();
return 0;
} //end of program
SAMPLE PROGRAM DEMONSTRATING PUBLIC
ACCESS SPECIFIER
Output
x is not accessible
value of y is 2
value of z is 3

More Related Content

PPTX
[OOP - Lec 07] Access Specifiers
PPTX
Power Point Presentation on Artificial Intelligence
PPTX
Switch case in C++
PPTX
PPTX
Error detection and correction
PPT
National science day
PPTX
Laws of boolean algebra
PPT
Class and object in C++
[OOP - Lec 07] Access Specifiers
Power Point Presentation on Artificial Intelligence
Switch case in C++
Error detection and correction
National science day
Laws of boolean algebra
Class and object in C++

What's hot (20)

PPTX
Member Function in C++
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Inheritance in c++
PPT
friend function(c++)
PPT
Function overloading(c++)
PPTX
Abstract class in c++
PPTX
Friend function & friend class
PPT
08 c++ Operator Overloading.ppt
PPTX
Polymorphism In c++
PPTX
Data members and member functions
PPTX
Inline function
PDF
Operator overloading
PPTX
Function overloading
PPTX
classes and objects in C++
PPTX
Constructor and Types of Constructors
PPTX
Static Data Members and Member Functions
PPTX
Friend function
PPTX
PPTX
Function overloading and overriding
PPTX
Method overloading
Member Function in C++
[OOP - Lec 19] Static Member Functions
Inheritance in c++
friend function(c++)
Function overloading(c++)
Abstract class in c++
Friend function & friend class
08 c++ Operator Overloading.ppt
Polymorphism In c++
Data members and member functions
Inline function
Operator overloading
Function overloading
classes and objects in C++
Constructor and Types of Constructors
Static Data Members and Member Functions
Friend function
Function overloading and overriding
Method overloading
Ad

Similar to Access specifier (20)

PPTX
Access controlaspecifier and visibilty modes
ODP
Class&objects
PPTX
00ps inheritace using c++
PPTX
C++ presentation
PPTX
Opp concept in c++
PDF
Access specifiers (Public Private Protected) C++
PPT
classes data type for Btech students.ppt
PPT
PPTX
Inheritance_with_its_types_single_multi_hybrid
PPTX
Presentation on class and object in Object Oriented programming.
PPT
11 Inheritance.ppt
PPTX
OOP C++
PPT
Inheritance in C++
PPT
C++Presentation 2.PPT
PPTX
OOPs & C++ UNIT 3
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PDF
PPT
Object and class presentation
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
oop lecture 3
Access controlaspecifier and visibilty modes
Class&objects
00ps inheritace using c++
C++ presentation
Opp concept in c++
Access specifiers (Public Private Protected) C++
classes data type for Btech students.ppt
Inheritance_with_its_types_single_multi_hybrid
Presentation on class and object in Object Oriented programming.
11 Inheritance.ppt
OOP C++
Inheritance in C++
C++Presentation 2.PPT
OOPs & C++ UNIT 3
Arre tari mano bhosko ka pikina tari ma no piko
Object and class presentation
Chapter 6 and inheritance OOP C++ tu ioe
oop lecture 3
Ad

More from zindadili (19)

PPTX
Namespaces
PPTX
Namespace1
PPTX
Exception handling
PPT
Exception handling
PPTX
Templates2
PPTX
Templates1
PPTX
Virtual function
PPTX
Operator overloaing
PPTX
Operator overloading2
PPTX
Polymorphism
PPTX
Function overloading
PPTX
Aggregation
PPTX
Hierarchical inheritance
PPTX
Hybrid inheritance
PPTX
Multiple inheritance
PPTX
Abstraction1
PPTX
Abstraction
PPTX
Inheritance
PPTX
Enum
Namespaces
Namespace1
Exception handling
Exception handling
Templates2
Templates1
Virtual function
Operator overloaing
Operator overloading2
Polymorphism
Function overloading
Aggregation
Hierarchical inheritance
Hybrid inheritance
Multiple inheritance
Abstraction1
Abstraction
Inheritance
Enum

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
RMMM.pdf make it easy to upload and study
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPH.pptx obstetrics and gynecology in nursing
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Module 4: Burden of Disease Tutorial Slides S2 2025
O7-L3 Supply Chain Operations - ICLT Program
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
RMMM.pdf make it easy to upload and study
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
master seminar digital applications in india

Access specifier

  • 2. C++ ACCESS SPECIFIERS  C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class. For example, the class members are grouped into sections, private protected and public. These keywords are called access specifiers which define the accessibility or visibility level of class members. By default the class members are private. So if the visibility labels are missing then by default all the class members are private. In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class. This is called accessibility and the access specifiers are used to determine this.
  • 3. C++ ACCESS SPECIFIERS Access specifier can be either private or protected or public. In general access specifiers are the access restriction imposed during the derivation of different subclasses from the base class.  private access specifier  protected access specifier  public access specifier
  • 5. VISIBILITY MODES Public: When the member is declared as public, it is accessible to all the functions of the program. Private: When the member is declared as private, it is accessible within the class only. Protected: When the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.
  • 6. PRIVATE ACCESS SPECIFIER • If private access specifier is used while creating a class, then the public and protected data members of the base class become the private member of the derived class and private member of base class remains private. • In this case, the members of the base class can be used only within the derived class and cannot be accessed through the object of derived class whereas they can be accessed by creating a function in the derived class.
  • 8. SAMPLE PROGRAM DEMONSTRATING PRIVATE ACCESS SPECIFIER • Note: Declaring data members with private access specifier is known as data hiding. #include <iostream> using namespace std; class base { private: int x; protected: int y;
  • 9. SAMPLE PROGRAM DEMONSTRATING PRIVATE ACCESS SPECIFIER public: int z; base() //constructor to initialize data members { x = 1; y = 2; z = 3; } }; class derive: private base { //y and z becomes private members of class derive and x remains private public: void showdata() { cout << "x is not accessible" << endl; cout << "value of y is " << y << endl; cout << "value of z is " << z << endl; }
  • 10. SAMPLE PROGRAM DEMONSTRATING PRIVATE ACCESS SPECIFIER }; int main() { derive a; //object of derived class a.showdata(); return 0; } //end of program Output x is not accessible value of y is 2 value of z is 3
  • 11. EXPLANATION • When a class is derived from the base class with private access specifier the private members of the base class can’t be accessed. So in above program, the derived class cannot access the • So in above program, the derived class cannot access the member x which is private in the base class, however, derive class has access to the protected and public members of the base class. So the • Hence the function showdata in derived class can access the public and protected member of the base class.
  • 12. PROTECTED ACCESS SPECIFIER • If protected access specifier is used while deriving class then the public and protected data members of the base class becomes the protected member of the derived class and private member of the base class are inaccessible. • In this case, the members of the base class can be used only within the derived class as protected members except for the private members.
  • 14. SAMPLE PROGRAM DEMONSTRATING PROTECTED ACCESS SPECIFIER #include <iostream> using namespace std; class base { private: int x; protected: int y; public: int z; base() //constructor to initialize data members { x = 1; y = 2; z = 3; } };
  • 15. SAMPLE PROGRAM DEMONSTRATING PROTECTED ACCESS SPECIFIER class derive: protected base { //y and z becomes protected members of class derive public: void showdata() { cout << "x is not accessible" << endl; cout << "value of y is " << y << endl; cout << "value of z is " << z << endl; } }; int main() { derive a; //object of derived class a.showdata(); return 0; }
  • 16. SAMPLE PROGRAM DEMONSTRATING PROTECTED ACCESS SPECIFIER Output x is not accessible value of y is 2 value of z is 3
  • 17. PUBLIC ACCESS SPECIFIER • If public access specifier is used while deriving class then the public data members of the base class becomes the public member of the derived class and protected members becomes the protected in the derived class but the private members of the base class are inaccessible.
  • 19. SAMPLE PROGRAM DEMONSTRATING PUBLIC ACCESS SPECIFIER #include <iostream> using namespace std; class base { private: int x; protected: int y; public: int z; base() //constructor to initialize data members { x = 1; y = 2; z = 3; } };
  • 20. SAMPLE PROGRAM DEMONSTRATING PUBLIC ACCESS SPECIFIER class derive: public base { //y becomes protected and z becomes public members of class derive public: void showdata() { cout << "x is not accessible" << endl; cout << "value of y is " << y << endl; cout << "value of z is " << z << endl; } }; int main() { derive a; //object of derived class a.showdata(); return 0; } //end of program
  • 21. SAMPLE PROGRAM DEMONSTRATING PUBLIC ACCESS SPECIFIER Output x is not accessible value of y is 2 value of z is 3