您的位置:首页 > 其它

类访问自身的私有成员

2014-04-10 22:13 211 查看
#include <iostream>
#include <string>
using namespace std;

class student
{
private:
char name[20];
public:
student(const char* str)
{
strcpy(name,str);
}
void get()
{
cout<<name<<endl;
}
void make(const student &pre)
{
cout<<pre.name<<endl;
}
};

int main()
{
student A("jianghuihong");
A.get();

student B("wangjun");
A.make(B);
/*输出
jianghuihong
wangjun
Press any key to continue

说明相同类型的对象是可以互相访问私有成员的

*/
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: