
类的私有成员只能被类的成员访问。这样做是为了保持面向对象的封装原则,确保数据及其相关函数被保存在一个单元中,并且只能从该类的成员访问。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/ Accessor functions −
private:
<datatype> value;
public:
<datatype> getterFunction() {
return <datatype> this->value;
}
Setter/Mutator函数−
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++中,友元函数类似于友元类。在这里,我们可以将一个不是类成员的特定函数声明为“友元”,并且它将获得访问类的私有成员的权限。让我们来看一下如何将一个函数定义为“友元”的语法。
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
当我们访问一个类的私有数据成员时,最好使用访问器/获取器和修改器/设置器函数。这是访问类的数据成员的最安全的方式。要始终记住的一件事是,访问私有成员的函数应该声明为公共的。友元函数在其他面向对象的语言中不可用,因为这并不总是保持面向对象封装的属性。友元关系是不对称的,如果类A声明类B为友元,那么类B将可以访问A的所有成员,但A将无法访问B的所有私有成员。
以上就是C++程序访问类的私有成员的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号