如何通过对象调用类的私有函数成员

来源:百度知道 编辑:UC知道 时间:2024/09/23 07:30:29
具体:
class base
{private:fun1();};
int main()
{
base b;
……
}
一楼啊,你另我很失望,答非所问。但也感谢你。

其余的人也可以2选1回答这道类似的题:
基类b私有(private)派生出类d,如何用d的对象访问b的函数成员fun1(公有的)?

通过对象调用类的私有函数成员=>

class base
{
private:
fun1();

public:
pubFunc1() { func1();} //输出base私有函数
};

int main()
{
base b;
……
b.pubFunc1(); //间接调用base类私有函数func1
}

私有成员是封装的,只有通过调用类中的函数来访问私有成员。例:
#include<iostream>
using namespace std;
class stu{
public:
stu(int i=0){x=i;}
int getx()
{return x;}
private:
int x;
};
int main()
{
stu a(3);
cout<<a.getx()<<endl;
return 0;
}