0

0

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

王林

王林

发布时间:2023-09-08 08:17:05

|

3529人浏览过

|

来源于tutorialspoint

转载

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 −

行业贸易网站管理系统 2007 Beta 1
行业贸易网站管理系统 2007 Beta 1

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

的中文翻译为:

示例

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

相关专题

更多
go语言 面向对象
go语言 面向对象

本专题整合了go语言面向对象相关内容,阅读专题下面的文章了解更多详细内容。

54

2025.09.05

java面向对象
java面向对象

本专题整合了java面向对象相关内容,阅读专题下面的文章了解更多详细内容。

46

2025.11.27

class在c语言中的意思
class在c语言中的意思

在C语言中,"class" 是一个关键字,用于定义一个类。想了解更多class的相关内容,可以阅读本专题下面的文章。

455

2024.01.03

python中class的含义
python中class的含义

本专题整合了python中class的相关内容,阅读专题下面的文章了解更多详细内容。

6

2025.12.06

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

1

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

5

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

6

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

30

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 5.7万人学习

C 教程
C 教程

共75课时 | 3.8万人学习

C++教程
C++教程

共115课时 | 10.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号