C++ Program of Access Specifiers or Access Modifiers, Accessing Data Members of Class in C++ | OOP Tutorial in C++
What is Access Specifiers or Modifiers of OOP in C++?
Access specifiers(or access modifiers) are used to set the accessibility of classes, methods and variables.
The keywords public, private, and protected are called access specifiers. A class can have multiple public, protected, or private labeled sections.
What is the use of access specifiers in C++?
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
What are the types of access specifiers?
In C++, there are three types of access specifiers:
1. public Access specifiers - members are accessible from outside the class
2. private Access specifiers - members cannot be accessed (or viewed) from outside the class.
3. protected Access specifiers - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Note: It is possible to access private members of a class using a public method inside the same class.
By default, all members of a class are private if you don't specify an access specifier.
Example:
class Blog {
public: // Public access specifier
int blogNo; // Public attribute
private: // Private access specifier
int postNo; // Private attribute
};
int main() {
Blog codeshikhi;
codeshikhi.blogNo = 4; // Allowed (public)
codeshikhi.postNo = 90; // Not allowed (private)
return 0;
}
If you try to access a private member, an error occurs:
error: postNo is private
Accessing Data Members of Class in C++ :
Accessing a data member depends solely on the access control of that data member. If its public, then the data member can be easily accessed using the direct member access (.) operator with the object of that class.
If, the data member is defined as private or protected, then we cannot access the data variables directly. Then we will have to create special public member functions to access, use or initialize the private and protected data members. These member functions are also called Accessors and Mutator methods or getter and setter functions.
Example-1: Write a program of public access specifier/modifier in C++
#include<iostream>
using namespace std;
class csecarnival
{
public:
int date;
void details(string name,int x)
{
date=x;
cout<<name<<" will be held on :"<<date<<" th Feb,2021"<<endl;
}
};
int main()
{
csecarnival event;
event.details("MEC CSECARNIVAL",24);
return 0;
}
#include<iostream> using namespace std; class csecarnival { public: int date; void details(string name,int x) { date=x; cout<<name<<" will be held on :"<<date<<" th Feb,2021"<<endl; } }; int main() { csecarnival event; event.details("MEC CSECARNIVAL",24); return 0; }
Output:
Example-2: Write a program of private access specifier in C++
#include<iostream>
using namespace std;
class Circle
{
private:
double radius;
public:
void compute_area(double r)
{
radius = r;
double area = 3.14*radius*radius;
cout << "Radius is:" << radius << endl;
cout << "Area is: " << area;
}
};
int main()
{
Circle obj;
obj.compute_area(1.5);
return 0;
}
#include<iostream> using namespace std; class Circle { private: double radius; public: void compute_area(double r) { radius = r; double area = 3.14*radius*radius; cout << "Radius is:" << radius << endl; cout << "Area is: " << area; } }; int main() { Circle obj; obj.compute_area(1.5); return 0; }
Output:
Example-3: Write a program of Protected Access specifier in C++ .
#include <bits/stdc++.h>
using namespace std;
class DU {
protected:
string dept_name;
};
class MEC : public DU {
public:
void setDept(string name) {
dept_name = name;
}
void displayDept() {
cout << "Welcome To " << dept_name << endl;
}
};
int main() {
MEC obj1;
obj1.setDept("CSE");
obj1.displayDept();
return 0;
}
#include <bits/stdc++.h> using namespace std; class DU { protected: string dept_name; }; class MEC : public DU { public: void setDept(string name) { dept_name = name; } void displayDept() { cout << "Welcome To " << dept_name << endl; } }; int main() { MEC obj1; obj1.setDept("CSE"); obj1.displayDept(); return 0; }
Output:
0 Response to C++ Program of Access Specifier or Modifier | OOP Tutorial in C++
Post a Comment