C++程序访问类的私有成员

c++程序访问类的私有成员

类的私有成员只能被类的成员访问。这样做是为了保持面向对象的封装原则,确保数据及其相关函数被保存在一个单元中,并且只能从该类的成员访问。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 −

private:    value;public:    getterFunction() {     return  this->value;   }

Setter/Mutator函数−

private:    value;public:   void setterFunction( _value) {     this->value = _value;   }

Example

的中文翻译为:

示例

#include using 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

的中文翻译为:

示例

#include using 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

的中文翻译为:

示例

#include using 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

的中文翻译为:

示例

#include using 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的所有私有成员。

以上就是C++程序访问类的私有成员的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444637.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:44:58
下一篇 2025年12月17日 21:45:12

相关推荐

发表回复

登录后才能评论
关注微信