SlideShare a Scribd company logo
1 of 14
Access Modifiers in C++
Access modifiers are used to implement an important feature of Object Oriented Programming known
as Data Hiding.
Consider a real-life example: Consider Indian secret informatic system having 10 senior members, have some
top secret regarding national security. So we can think that 10 people as class data members or member
functions who can directly access secret information from each other but anyone can’t access this information
other than these 10 members i.e. outside people can’t access information directly without having any
privileges. This is what data hiding is.
Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it
sets some restrictions on the class members not to get directly accessed by the outside functions.Here are 3
types of access modifiers available in C++:
 Public
 Private
 Protected
Public
All the class members declared under public will be available to everyone. The data members and
member functions declared public can be accessed by other classes too. The public members of a
class can be accessed from anywhere in the program using the direct member access operator (.)
the object of that class.
Private
The class members declared as private can be accessed only by the functions inside the class. They
are not allowed to be accessed directly by any object or function outside the class. Only the
functions or the friend functions are allowed to access the private data members of a class.
Protected
Protected access modifier is similar to that of private access modifiers, the difference is that the
member declared as Protected are inaccessible outside the class but they can be accessed by any
subclass(derived class) of that class.
// C++ program to demonstrate public
// access modifier
#include<iostream>
using namespace std;
// class definition
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public data member outside class
obj.radius = 5.5;
cout << "Radius is:" << obj.radius << "n";
cout << "Area is:" << obj.compute_area();
return 0;
// C++ program to demonstrate private
// access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
double compute_area()
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
The output of above program will be a compile time error because we are not allowed to access
the private data members of a class directly outside the class.
we can access the private data members of a
class indirectly using the public member
functions of the class.
#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;
}
// C++ program to demonstrate
// protected access modifier
#include <iostrem>
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 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;
}
Access Control
A derived class can access all the non-private members of its base class. Thus base-class members
that should not be accessible to the member functions of derived classes should be declared
in the base class.
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
We can summarize the different access types according to - who can access them in the following way −
A derived class inherits all base class methods with the following exceptions
•Constructors, destructors and copy constructors of the base class.
•Overloaded operators of the base class.
•The friend functions of the base class.
Accessibility private variables protected variables public variables
Accessible from own
class?
yes yes yes
Accessible from derived
class?
no yes yes
Accessible from 2nd
derived class?
no yes yes
ccessibility private variables protected variables public variables
Accessible from own
class?
yes yes yes
Accessible from derived
class?
no yes
yes
(inherited as protected
variables)
Accessible from 2nd
derived class?
no yes yes
Accessibility private variables protected variables public variables
Accessible from own
class?
yes yes yes
Accessible from
derived class?
no
yes
(inherited as private
variables)
yes
(inherited as private
variables)
Accessible from 2nd
derived class?
no no no
Type of Inheritance
 When deriving a class from a base class, the base class may be inherited through public,
protected or private inheritance. The type of inheritance is specified by the access-
specifier as explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used.
While using different type of inheritance, following rules are applied −
 Public Inheritance − When deriving a class from a public base class, public members of
the base class become public members of the derived class and protected members of
base class become protected members of the derived class. A base class's private
are never accessible directly from a derived class, but can be accessed through calls to
the public and protected members of the base class.
 Protected Inheritance − When deriving from a protected base
class, public and protected members of the base class become protected members of the
derived class.
 Private Inheritance − When deriving from a private base
class, public and protected members of the base class become private members of the
derived class.
Scope resolution operator in C++
C++, scope resolution operator is ::. It is used for following purposes.
1) To access a global variable when there is a local variable with same name:
// C++ program to show that we can access a global variable
// using scope resolution operator :: when there is a local
// variable with same name
#include<iostream>
using namespace std;
int x; // Global x
int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "nValue of local x is " << x;
return 0;
} OUTPUT : global x is 0, local x is 10
2) To define a function outside a class.
// C++ program to show that scope resolution
operator :: is used
// to define a function outside a class
#include<iostream>
using namespace std;
class A
{
public:
// Only declaration
void fun();
};
// Definition outside class using ::
void A::fun()
{
cout << "fun() called";
}
int main()
{
A a;
a.fun();
return 0;
}
3) To access a class’s static variables.
// C++ program to show that :: can be used to access static
// members when there is a local variable with same name
#include<iostream>
using namespace std;
class Test
{
static int x;
public:
static int y;
// Local parameter 'a' hides class member
// 'a', but we can access it using ::
void func(int x)
{
// We can access class's static variable
// even if there is a local variable
cout << "Value of static x is " << Test::x;
cout << "nValue of local x is " << x;
}
};
// In C++, static members must be explicitly defined
// like this
int Test::x = 1;
int Test::y = 2;
int main()
{
Test obj;
int x = 3 ;
obj.func(x);
cout << "nTest::y = " << Test::y;
return 0;
}
4) In case of multiple Inheritance:
// Use of scope resolution operator in multiple
inheritance.
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
A() { x = 10; }
};
class B
{
protected:
int x;
public:
B() { x = 20; }
};
class C: public A, public B
{
public:
void fun()
{
cout << "A's x is " << A::x;
cout << "nB's x is " << B::x;
}
};
int main()
{
C c;
c.fun();
return 0;
}

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
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.Enam Khan
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 

What's hot (20)

Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance
InheritanceInheritance
Inheritance
 
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.
 
Class and object
Class and objectClass and object
Class and object
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar to Access controlaspecifier and visibilty modes

Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
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_hybridVGaneshKarthikeyan
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Class&objects
Class&objectsClass&objects
Class&objectsharivng
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1Mirza Hussain
 

Similar to Access controlaspecifier and visibilty modes (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Access specifier
Access specifierAccess specifier
Access specifier
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
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
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP C++
OOP C++OOP C++
OOP C++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Class&objects
Class&objectsClass&objects
Class&objects
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance
InheritanceInheritance
Inheritance
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

Access controlaspecifier and visibilty modes

  • 1. Access Modifiers in C++ Access modifiers are used to implement an important feature of Object Oriented Programming known as Data Hiding. Consider a real-life example: Consider Indian secret informatic system having 10 senior members, have some top secret regarding national security. So we can think that 10 people as class data members or member functions who can directly access secret information from each other but anyone can’t access this information other than these 10 members i.e. outside people can’t access information directly without having any privileges. This is what data hiding is. Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.Here are 3 types of access modifiers available in C++:  Public  Private  Protected
  • 2. Public All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) the object of that class. Private The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the functions or the friend functions are allowed to access the private data members of a class. Protected Protected access modifier is similar to that of private access modifiers, the difference is that the member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
  • 3. // C++ program to demonstrate public // access modifier #include<iostream> using namespace std; // class definition class Circle { public: double radius; double compute_area() { return 3.14*radius*radius; } }; // main function int main() { Circle obj; // accessing public data member outside class obj.radius = 5.5; cout << "Radius is:" << obj.radius << "n"; cout << "Area is:" << obj.compute_area(); return 0;
  • 4. // C++ program to demonstrate private // access modifier #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: double compute_area() { // member function can access private // data member radius return 3.14*radius*radius; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.radius = 1.5; cout << "Area is:" << obj.compute_area(); return 0; }
  • 5. The output of above program will be a compile time error because we are not allowed to access the private data members of a class directly outside the class. we can access the private data members of a class indirectly using the public member functions of the class. #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; }
  • 6. // C++ program to demonstrate // protected access modifier #include <iostrem> 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 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; }
  • 7. Access Control A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared in the base class. Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no We can summarize the different access types according to - who can access them in the following way − A derived class inherits all base class methods with the following exceptions •Constructors, destructors and copy constructors of the base class. •Overloaded operators of the base class. •The friend functions of the base class.
  • 8. Accessibility private variables protected variables public variables Accessible from own class? yes yes yes Accessible from derived class? no yes yes Accessible from 2nd derived class? no yes yes ccessibility private variables protected variables public variables Accessible from own class? yes yes yes Accessible from derived class? no yes yes (inherited as protected variables) Accessible from 2nd derived class? no yes yes
  • 9. Accessibility private variables protected variables public variables Accessible from own class? yes yes yes Accessible from derived class? no yes (inherited as private variables) yes (inherited as private variables) Accessible from 2nd derived class? no no no
  • 10. Type of Inheritance  When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access- specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied −  Public Inheritance − When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of base class become protected members of the derived class. A base class's private are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.  Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.  Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • 11. Scope resolution operator in C++ C++, scope resolution operator is ::. It is used for following purposes. 1) To access a global variable when there is a local variable with same name: // C++ program to show that we can access a global variable // using scope resolution operator :: when there is a local // variable with same name #include<iostream> using namespace std; int x; // Global x int main() { int x = 10; // Local x cout << "Value of global x is " << ::x; cout << "nValue of local x is " << x; return 0; } OUTPUT : global x is 0, local x is 10
  • 12. 2) To define a function outside a class. // C++ program to show that scope resolution operator :: is used // to define a function outside a class #include<iostream> using namespace std; class A { public: // Only declaration void fun(); }; // Definition outside class using :: void A::fun() { cout << "fun() called"; } int main() { A a; a.fun(); return 0; }
  • 13. 3) To access a class’s static variables. // C++ program to show that :: can be used to access static // members when there is a local variable with same name #include<iostream> using namespace std; class Test { static int x; public: static int y; // Local parameter 'a' hides class member // 'a', but we can access it using :: void func(int x) { // We can access class's static variable // even if there is a local variable cout << "Value of static x is " << Test::x; cout << "nValue of local x is " << x; } }; // In C++, static members must be explicitly defined // like this int Test::x = 1; int Test::y = 2; int main() { Test obj; int x = 3 ; obj.func(x); cout << "nTest::y = " << Test::y; return 0; }
  • 14. 4) In case of multiple Inheritance: // Use of scope resolution operator in multiple inheritance. #include<iostream> using namespace std; class A { protected: int x; public: A() { x = 10; } }; class B { protected: int x; public: B() { x = 20; } }; class C: public A, public B { public: void fun() { cout << "A's x is " << A::x; cout << "nB's x is " << B::x; } }; int main() { C c; c.fun(); return 0; }