on Leave a Comment

Access Modifiers in C++

Access modifiers is necessary in object-oriented programming for data hiding. Access modifiers specifies who can access data and who can't access data. In class, we can set accessibility of instance member and static members by access modifiers.  

C++ has three access modifiers:

(1)  Public
(2)  Private
(3)  Protected

Public Access Modifier in C++

Public is keyword is c++ programming language. The variables and functions declared under public access modifier is accessible to everyone inside or outside of that class. Other class members can also access public members of another class. 

Public members can accessed by using (.) dot operator.

During inheritance, public members of base class accessible to derived class.

Programming example of public access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
public:
    double l, b;
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5

Private Access Modifier in C++

Private is keyword is c++ programming language. The variables and functions declared under private access modifier can be only accessible inside the class. Other class members can not access private members of another class. 

Objects of class can't access private members directly. But object can access private members by public functions of that class.

During inheritance, private members do not inherits.

Friend functions can access private members indirectly.

Programming example of private access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
private:
    double l, b;
public:
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5

Protected Access Modifier in C++

Protected is keyword is c++ programming language. The variables and functions declared under protected access modifier can be only accessible inside the class. Other class members can not access protected members of another class. 

Objects of class can't access protected members directly. But object can access protected members by public functions of that class.

During inheritance, protected member of base class accessible to derived class.

Friend functions can access protected members indirectly.

Programming example of protected access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
protected:
    double l, b;
public:
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5


0 comments:

Post a Comment