SlideShare uma empresa Scribd logo
1 de 21
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

Mais conteúdo relacionado

Mais procurados

Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

Mais procurados (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
class and objects
class and objectsclass and objects
class and objects
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 

Semelhante a Access specifier

Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Class&objects
Class&objectsClass&objects
Class&objects
harivng
 

Semelhante a Access specifier (20)

Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Class&objects
Class&objectsClass&objects
Class&objects
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
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
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
OOP C++
OOP C++OOP C++
OOP C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
C++ classes
C++ classesC++ classes
C++ classes
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
inheritance
inheritanceinheritance
inheritance
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 

Mais de zindadili (19)

Namespaces
NamespacesNamespaces
Namespaces
 
Namespace1
Namespace1Namespace1
Namespace1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Templates2
Templates2Templates2
Templates2
 
Templates1
Templates1Templates1
Templates1
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Aggregation
AggregationAggregation
Aggregation
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Abstraction
AbstractionAbstraction
Abstraction
 
Inheritance
InheritanceInheritance
Inheritance
 
Enum
EnumEnum
Enum
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Último (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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