SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Access Specifiers
Each user has different access privileges to the object.
• private
• public : int a;
• protected
• These keywords are called access-control
specifiers.
• All the members that follow a keyword belong to
that type.
• If no keyword is specified, then the members are
assumed to have private privilege.
class PiggyBank
{
int Money; //Private by default
void Display () //Private by default
{
….
}
private: //Private by declaration
int AccNumber;
public:
int code; //public
void SetData(int a; int b) //public
{
….
}
protected:
int PolicyCode; //Protected by declaration
void GetPolicyCode() ,,
{
….
}
};
Member Money, AccNumber and Display() will be
the type private, the member code and SetDate()
will be of type public; and the members
PolicyCode and GetData() will be of type
protected.
Data Hiding is mainly designed to protect well-
intentioned programmers from honest mistakes.
There are mechanisms to access even private data
using friends, pointer to members, etc. from
outside the class.
Member Money, AccNumber and Display() will be
the type private, the member code and SetDate()
will be of type public; and the members
PolicyCode and GetData() will be of type
protected.
Data Hiding is mainly designed to protect well-
intentioned programmers from honest mistakes.
There are mechanisms to access even private data
using friends, pointer to members, etc. from
outside the class.
Private Members
• Only the member functions of the same class
can access these members.
• Access control in C++ has the objective of
reducing the likelihood of bugs and enhancing
consistency.
class Person
{
private: //access specifier
//private members
………..
int age; //private data
int getage(); //private function
……..
};
Person p1; //create object of class Person
a=p1.age; //cannot access private data
p1. getage(); // cannot access private function
Private members accessibility
Error : Invalid access
Protected Members
• The access control of the protected members
in similar to that of private members and has
more significance in inheritance.
class Person
{
protected: //access specifier
//protected members
………..
int age; // protected data
int getage(); // protected function
……..
};
Person p1; //create object of class Person
a=p1.age; //cannot access protected member
p1. getage(); (same as private)
Protected members accessibility
Public Members
• The members of a class, which are to be
visible (accessible) outside the class, should be
declared in public section.
• All data members and functions declared in
the public section of the class can be accessed
without any restriction from anywhere in the
program, either by functions that belong to
the class or by those external to the class.
class Person
{
public: //access specifier
//public members
………..
int age; // public data
int getage(); // public function
……..
};
Person p1; //p1 object of class Person
a=p1.age; //can access public data
p1. getage(); //can access public function
Public members accessibility
No Error :
Can access public data
Can access public function
Visibility of class members
Access Specifier Accessible to
Own Class Members Objects of a Class
private: Yes No
protected: Yes No
public: Yes Yes
Difference between Private and
Protected
Private members Protected members
Private members are declared with the
keyword private followed by a colon (:)
character.
Protected members are declared with the
keyword protected followed by a colon (:)
character.
Private members are accessible within the
same class in which they are declared.
Protected members are accessible within
the same class and within the
derived/sub/child class.
Private members can also be accessed
through the friend function.
Protected members cannot be accessed
through the friend function.
//protected
#include <iostream>
using namespace std;
// base class
class Parent {
// protected data members
protected:
int id_protected;
};
// sub class or derived class
class Child : public Parent {
public:
void setId(int id)
{
// Child class is able to access the inherited
// protected data members of the base class
id_protected = id;
}
void displayId()
{
cout << "id_protected is: "
<< id_protected << endl;
}
};
// main function
int main()
{
Child obj1;
// member function of the derived class can
// access the protected data members of the
base class
obj1.setId(81);
obj1.displayId();
return 0;
}
Output
id_protected is: 81
//Private
#include <iostream>
using namespace std;
class Circle {
// private data member
private:
double radius;
// public member function
public:
void compute_area(double r)
{
// member function can access private
// data member radius
radius = r;
double area = 3.14 * radius * radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Output
Radius is: 1.5
Area is: 7.065

Contenu connexe

Tendances

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

Tendances (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Structure in C
Structure in CStructure in C
Structure in C
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
class and objects
class and objectsclass and objects
class and objects
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Data types in python
Data types in pythonData types in python
Data types in python
 

Similaire à Access specifiers (Public Private Protected) C++ (20)

Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Python .pptx
Python .pptxPython .pptx
Python .pptx
 
Inheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybridInheritance_with_its_types_single_multi_hybrid
Inheritance_with_its_types_single_multi_hybrid
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Class&objects
Class&objectsClass&objects
Class&objects
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Friend function
Friend functionFriend function
Friend function
 
Inheritance
InheritanceInheritance
Inheritance
 
class c++
class c++class c++
class c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
27c
27c27c
27c
 

Dernier

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Dernier (20)

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

Access specifiers (Public Private Protected) C++

  • 2. Each user has different access privileges to the object. • private • public : int a; • protected • These keywords are called access-control specifiers. • All the members that follow a keyword belong to that type. • If no keyword is specified, then the members are assumed to have private privilege.
  • 3. class PiggyBank { int Money; //Private by default void Display () //Private by default { …. } private: //Private by declaration int AccNumber; public: int code; //public void SetData(int a; int b) //public { …. } protected: int PolicyCode; //Protected by declaration void GetPolicyCode() ,, { …. } };
  • 4. Member Money, AccNumber and Display() will be the type private, the member code and SetDate() will be of type public; and the members PolicyCode and GetData() will be of type protected. Data Hiding is mainly designed to protect well- intentioned programmers from honest mistakes. There are mechanisms to access even private data using friends, pointer to members, etc. from outside the class.
  • 5. Member Money, AccNumber and Display() will be the type private, the member code and SetDate() will be of type public; and the members PolicyCode and GetData() will be of type protected. Data Hiding is mainly designed to protect well- intentioned programmers from honest mistakes. There are mechanisms to access even private data using friends, pointer to members, etc. from outside the class.
  • 6. Private Members • Only the member functions of the same class can access these members. • Access control in C++ has the objective of reducing the likelihood of bugs and enhancing consistency.
  • 7. class Person { private: //access specifier //private members ……….. int age; //private data int getage(); //private function …….. }; Person p1; //create object of class Person a=p1.age; //cannot access private data p1. getage(); // cannot access private function Private members accessibility
  • 9. Protected Members • The access control of the protected members in similar to that of private members and has more significance in inheritance.
  • 10. class Person { protected: //access specifier //protected members ……….. int age; // protected data int getage(); // protected function …….. }; Person p1; //create object of class Person a=p1.age; //cannot access protected member p1. getage(); (same as private) Protected members accessibility
  • 11. Public Members • The members of a class, which are to be visible (accessible) outside the class, should be declared in public section. • All data members and functions declared in the public section of the class can be accessed without any restriction from anywhere in the program, either by functions that belong to the class or by those external to the class.
  • 12. class Person { public: //access specifier //public members ……….. int age; // public data int getage(); // public function …….. }; Person p1; //p1 object of class Person a=p1.age; //can access public data p1. getage(); //can access public function Public members accessibility
  • 13. No Error : Can access public data Can access public function
  • 14. Visibility of class members Access Specifier Accessible to Own Class Members Objects of a Class private: Yes No protected: Yes No public: Yes Yes
  • 15. Difference between Private and Protected Private members Protected members Private members are declared with the keyword private followed by a colon (:) character. Protected members are declared with the keyword protected followed by a colon (:) character. Private members are accessible within the same class in which they are declared. Protected members are accessible within the same class and within the derived/sub/child class. Private members can also be accessed through the friend function. Protected members cannot be accessed through the friend function.
  • 16. //protected #include <iostream> using namespace std; // base class class Parent { // protected data members protected: int id_protected; }; // sub class or derived class class Child : public Parent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of the base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { Child obj1; // member function of the derived class can // access the protected data members of the base class obj1.setId(81); obj1.displayId(); return 0; } Output id_protected is: 81
  • 17. //Private #include <iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function can access private // data member radius radius = r; double area = 3.14 * radius * radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.compute_area(1.5); return 0; } Output Radius is: 1.5 Area is: 7.065