
类的私有成员只能被类的成员访问。这样做是为了保持面向对象的封装原则,确保数据及其相关函数被保存在一个单元中,并且只能从该类的成员访问。C++有三种不同的访问控制符来指定类的成员的可见性。这三种访问控制符是−
Public − 如果一个类的成员具有public可见性,那么这些成员可以从任何其他类中访问。
Private − 具有私有可见性的类成员只能从类内部访问。
-
Protected − protected class members can be accessed from with9in the class or from its subclasses only.
立即学习“C++免费学习笔记(深入)”;
对于这篇文章,我们将仅关注访问类的私有成员。
使用getter和setter方法来访问数据成员
Getter和setter函数用于访问和修改类的私有成员。顾名思义,getter函数返回数据成员,而setter函数用于“设置”或修改数据成员。我们通过两个例子来进一步理解这个概念,但在此之前,给出基本语法如下。
语法
Getter/ Accessor functions −
1.修正BUG站用资源问题,优化程序2.增加关键词搜索3.修改报价4.修正BUG 水印问题5.修改上传方式6.彻底整合论坛,实现一站通7.彻底解决群发垃圾信息问题。注册会员等发垃圾邮件7.彻底解决数据库安全9.修改交易方式.增加网站担保,和直接交易两中10.全站可选生成html.和单独新闻生成html(需要装组建)11. 网站有10中颜色选择适合不同的行业不同的颜色12.修改竞价格排名方式13.修
private:value; public: getterFunction() { return this->value; }
Setter/Mutator函数−
private:value; public: void setterFunction( _value) { this->value = _value; }
Example
的中文翻译为:示例
#includeusing namespace std; class Test{ private: int value; public: //the getter function int getValue() { return this->value; } //the setter function void setValue(int _value) { this->value = _value; } }; int main(){ Test test; test.setValue(15); cout << "The value we set is: " << test.getValue() << endl; return 0; }
输出
The value we set is: 15
从另一个函数内部访问成员函数
当我们访问一个私有成员函数时,情况是一样的。我们必须以与访问数据成员相同的方式从类成员方法内部访问它。我们可以使用“this”指针来避免名称冲突。
语法
private:function_name(params) {}; public: another_function(params) { var = this->function_name(arguments); }
调用私有成员函数的函数应该声明为公共的。只有当从该类的对象调用公共函数时,该函数才会执行。
Example
的中文翻译为:示例
#includeusing namespace std; class Test{ private: int value; //multiplies the member value by 10 void multiplyValue() { this->value = this->value * 10; } public: //the getvalue function calls the multiply value function int multiplyAndGetValue() { this->multiplyValue(); return this->value; } //the setter function void setValue(int _value) { this->value = _value; } }; int main(){ Test test; test.setValue(15); cout << "The value after setting and multiplying is: " << test.multiplyAndGetValue() << endl; return 0; }
输出
The value after setting and multiplying is: 150
使用友元类
在C++中,友元类是一种可以访问其他类中不对其他类可见的私有和受保护成员的类。要将一个类声明为另一个类的友元类,需要使用关键字‘friend’。让我们看看它是如何工作的。
语法
class A{
private:
.....
friend class B;
};
class B{
//class body
};
Example
的中文翻译为:示例
#includeusing namespace std; class Test1{ private: int value; public: Test1(int _value) { this->value = _value; } //we declare another class as a friend friend class Test2; }; class Test2{ public: //displays the value of the other class object void display(Test1 &t) { cout << "The value of Test1 object is: " << t.value; } }; int main(){ //creating two class objects of the two classes Test1 test1(15); Test2 test2; //calling the friend class function test2.display(test1); return 0; }
输出
The value of Test1 object is: 15
使用友元函数
在C++中,友元函数类似于友元类。在这里,我们可以将一个不是类成员的特定函数声明为“友元”,并且它将获得访问类的私有成员的权限。让我们来看一下如何将一个函数定义为“友元”的语法。
语法
class A{
private:
.....
friend function_name(params);
};
function_name(params) {
//function body
}
Example
的中文翻译为:示例
#includeusing namespace std; class Test1{ private: int value; public: Test1(int _value) { this->value = _value; } //we declare a friend function friend void display(Test1); }; void display(Test1 t) { cout << "The value of Test1 object is: " << t.value; } int main(){ //creating two class objects of the two classes Test1 test1(55); //calling the friend class function display(test1); return 0; }
输出
The value of Test1 object is: 55
结论
当我们访问一个类的私有数据成员时,最好使用访问器/获取器和修改器/设置器函数。这是访问类的数据成员的最安全的方式。要始终记住的一件事是,访问私有成员的函数应该声明为公共的。友元函数在其他面向对象的语言中不可用,因为这并不总是保持面向对象封装的属性。友元关系是不对称的,如果类A声明类B为友元,那么类B将可以访问A的所有成员,但A将无法访问B的所有私有成员。










