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


类的私有成员只能被类本身的成员访问。这样做是为了保持面向对象范式中的封装性,确保数据及其相关函数保持在一个单元中,并且只能从该类的成员进行访问。C++ 有三种不同的访问说明符来指定类成员的可见性。这三种访问说明符分别是:

  • 公共 (Public) - 如果类成员的可见性为公共,则可以从任何其他类访问这些成员。

  • 私有 (Private) - 具有私有可见性的类成员只能从类内部访问。

  • 保护 (Protected) - 保护类成员只能从类内部或其子类访问。

本文将重点介绍如何仅访问类的私有成员。

使用 getter 和 setter 方法访问数据成员

getter 和 setter 函数用于访问和修改类的私有成员。顾名思义,getter 函数返回数据成员,而 setter 函数用于“设置”或修改数据成员。我们通过两个示例来进一步理解这个概念,但在此之前,先给出基本的语法。

语法

Getter/访问器函数 -

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

Setter/修改器函数 -

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

示例

#include <iostream>

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:
   <returntype> function_name(params) {};
public:
   <returntype> another_function(params) {
     <datatype> var = this->function_name(arguments);
   }

调用私有成员函数的函数应声明为公共。只有当从该类的对象调用公共函数时,该函数才会执行。

示例

#include <iostream>

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
};

示例

#include <iostream>

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++ 中的友元函数类似于友元类。在这里,我们可以将一个不属于类的特定函数声明为“友元”,它将获得访问该类私有成员的权限。让我们看看如何将函数定义为“friend”的语法。

语法

class A{
   private:
     .....
   friend <return_type> function_name(params);
};

<return_type> function_name(params) {
   //function body
}

示例

#include <iostream>

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

结论

当我们访问类的私有数据成员时,我们最好使用访问器/getter 和修改器/setter 函数。这是访问类数据成员最安全的方式。始终需要注意的是,访问私有成员的函数应声明为公共。其他面向对象语言中没有友元函数,因为这并不能始终保持面向对象的封装特性。友元不是相互的,如果类 A 将类 B 声明为友元,则类 B 将可以访问 A 的所有成员,但 A 无法访问 B 的所有私有成员。

更新于:2022-12-13

23K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.