访问修饰符用于实现称为数据隐藏的面向对象编程的重要方面。考虑一个真实的例子:
拥有10名核心成员的研究与分析部门(R&AW)拥有有关国家安全的敏感机密信息。现在,我们可以将这些核心成员与某个类的数据成员或成员函数相关联,而这些成员又可以与R&A部门相关联。这10个成员可以直接从其所属部门(班级)访问机密信息,但是除这10个成员之外,任何人都不能直接访问此信息,即,除班级中普遍存在的那些外部功能外,其他人不能访问未授权的信息。给他们,而没有分配特权(例如,前面将在本文中看到的朋友类和继承的类所拥有的特权)或访问被允许直接访问机密信息的这10个成员之一(类似于私有的特权)可以通过可直接访问私有成员的类的公共成员函数在外部访问类的成员)。这就是实践中的数据隐藏。
类中的访问修饰符或访问说明符用于将可访问性分配给类成员。也就是说,它对类成员设置了一些限制,使其不能被外部函数直接访问。
C++中提供了3种类型的访问修饰符:
- 上市
- 私人的
- 受保护的
注意:如果我们没有为类内的成员指定任何访问修饰符,则默认情况下,成员的访问修饰符将为Private 。
现在让我们详细了解这些访问修饰符:
1.公众:在公共说明符下声明的所有班级成员将对所有人开放。声明为public的数据成员和成员函数也可以由其他类和函数访问。可以使用具有该类对象的直接成员访问运算符(。)从程序中的任何位置访问该类的公共成员。
例子:
CPP
// C++ program to demonstrate public
// access modifier
#include
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;
}
CPP
// C++ program to demonstrate private
// access modifier
#include
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;
}
CPP
// C++ program to demonstrate private
// access modifier
#include
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;
}
CPP
// C++ program to demonstrate
// protected access modifier
#include
using namespace std;
// base class
class Parent
{
// protected data members
protected:
int id_protected;
};
// sub class or derived class from public base 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;
}
输出:
Radius is: 5.5
Area is: 94.985
在上面的程序中,数据成员半径声明为public,因此可以在类外部访问它,因此可以从main()内部访问它。
2.私有:声明为私有的类成员只能由该类内部的成员函数访问。类之外的任何对象或函数都不允许直接访问它们。只允许成员函数或朋友函数访问类的私有数据成员。
例子:
CPP
// C++ program to demonstrate private
// access modifier
#include
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;
}
输出:
In function 'int main()':
11:16: error: 'double Circle::radius' is private
double radius;
^
31:9: error: within this context
obj.radius = 1.5;
^
上面程序的输出是编译时错误,因为不允许我们直接在类外部访问类的私有数据成员。但是尝试访问obj.radius,radius是私有数据成员,我们得到了编译错误。
但是,我们可以使用类的公共成员函数间接访问类的私有数据成员。
例子:
CPP
// C++ program to demonstrate private
// access modifier
#include
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;
}
输出:
Radius is: 1.5
Area is: 7.065
3.受保护的:受保护的访问修饰符与私有访问修饰符类似,除非不能在朋友类的帮助下,否则不能在其类之外进行访问,区别在于声明为受保护的类成员可以由任何人访问该类的subclass(派生类)。
注意:这种通过继承进行的访问可以根据继承的模式来更改派生类中基类元素的访问修饰符。
例子:
CPP
// C++ program to demonstrate
// protected access modifier
#include
using namespace std;
// base class
class Parent
{
// protected data members
protected:
int id_protected;
};
// sub class or derived class from public base 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;
}
输出:
id_protected is: 81