You are on page 1of 10

MINHAJ UNIVERSITY LAHORE

Object Oriented
Programming (OOP)
BS Software engineering
2nd semester

Qasid Abbas

Sir Muhammad Akmal


Access modifiers
The technique used to prohibit the member of one class from accessing the data outside the class
or to hide a specific data in object orientated language program is known access modifier or
access specifier. It sets some limits to the member of the class from directly accessing data
outside the function.

It is the technique that implies the limits and restriction to the data member and the member
function for the access to the data outside their specific class. For example, the class members
are divided into different groups like private, protected and public. These groups are called
access specifiers or access modifiers and allow the availability or prohibit the data. In the default
program of object orientated language all the class members are private if the label of group is
not present then it will automatically make the data of the class member 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.

Real life example of Access modifiers


One of the basic and important feature of object-oriented programming known as data hiding is
applied and work with help of access modifiers. Here is a real life example of access modifiers

The Research and analysis wing (R&AW), having 10 core members has come in ownership of
complex confidential data concerning to national security and safety. Now we are associating
those core members to member function or data member of a class which in return can be
associated to the R&A wing. Only these 10 core members can access that confidential
information or any other information regarding to that information they can access it directly
from their wing (the class), but anyone else then these 10 core member can’t access that
confidential information directly, for example outside or external functions other than those
dominant members in class itself cannot access that information which is not allowed to them,
without having either assigned privileges such as those possessed by the friend class and
inherited class or having access through these 10 members who is allowed to have direct access
to that confidential information (similarly to how the private members of the class can be
accessed in the outside world through public member functions of the class that are directly
accessed to the private members).This is what called data hiding in practice.

Syntax
The basic syntax of the access modifiers public, private, and protected is given below:

class Base {
public:
// public members go here
protected:

// protected members go here


private:
// private members go here

};

Advantages of Access Modifiers in C++


The different advantages of Access Modifiers in C++ are given below:

 An access modifier provides us the full control to control our data depending upon the
situation in which we need our data. If a person is bank domain worker he needs to use
private data member to make its data hidden from the other users to make the data of
users safe, the full control of that data is in your hand from whom you want to hide that
data to whom you want to show that data you can make this data public but it won’t be a
good step because in that case anyone can access your data and change your data which
will be dangerous.
 If you make a base class of public members then the all derived class members also will
be public. In the same way, if you make a base class of protected members then the all
derived class member will also be derived which is very help full for user to manageing
the data as u can see it is very easy to access data in every aspect because as there is no
change in the members that user want to access if user is using public inheritance in his
programming language.
 If user is making a class using private inheritance according to the situation, all the public
members of the base calls becomes private member of the derived class of that base class.
In the same way, all the protected members of the base class will becomes private
member of the derived class of that base class whereas, in the scenario of protected
inheritance, all the public members of the base class becomes protected members of the
derived class and all protected members of the base class becomes protected member of
the derived class and all protected member of the base class will becomes protected
member of the derived class. Note that in C++ access specification works on the basis of
the per-class, not on per-object basis.

Types of Access Modifiers in C++


There are 3 types of access modifiers available in C++:

 Public
 Private
 Protected

Note: If a user does not specify any access modifiers for the members inside the class then by
default the access modifier for the members will be Private.

Public Access Specifier


If a user uses public access specifier while he is deriving the class then the all the public
members of that base class will become the public member of the derived class which is derived
from that base class and all the protected members become the protected in the derived class but
the private members of the base class are inaccessible in derived class.

The following block diagram of the public access modifiers explain how the dat members of the
base class are inherited when the derived class access mode used is public.
Example 1: C++ public Access Modifier

#include <iostream>
using namespace std;

// define a class
class Sample {

// public elements
public:
int age;

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

int 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();
return 0;
}
Output

Enter your age: 23


Age = 23

In this example program, we have created a class whom we have giving the name of sample
which has public variable used age and the public function named as displayage ().

In main(),we have created an object of the class sample named as obj1 to access the values from
the class in main function. Then we access all the public elements directly by using the code
obj.age and obj.displayAge

Example 2: C++ 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 datamember outside class
    obj.radius = 5.5;
     
    cout << "Radius is: " << obj.radius << "\n";
    cout << "Area is: " << obj.compute_area();
    return 0;
}
Output
Radius is: 5.5
Area is: 94.985

Private Access Specifier


If a user uses private access specifier when he is creating a class, then the public and protected
data members of the class become the private member of the derived class of that base class and
the private members of the base class will remain private.

In that case, the all 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.

The following block diagram explains how in the scenario of the private access specifier how
data members of base class are inherited when derived class access mode is private.

Example 1: C++ Private Access Specifier

#include <iostream>
using namespace std;
// define a class
class Sample {

// private elements
private:
int age;

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

int main() {

int ageInput;

// declare an object
Sample obj1;

cout << "Enter your age: ";


cin >> ageInput;

// call function and pass ageInput as argument


obj1.displayAge(ageInput);

return 0; }

Output

Enter your age: 28


Age = 28

In main(), the object obj.1 cannot be directly access the class variable age.


// error
cin >> obj1.age;

We can only indirectly manipulate or operate age through the public function displayAge(), since


this function allocates age to the argument approved into it i.e. the function parameter int a.

Example 2: C++ Private Access Modifiers

#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

You might also like