设为首页 加入收藏

TOP

C++友元函数访问类的私有成员(代码实例)
2018-03-06 09:03:07 】 浏览:311
Tags:函数 访问 私有 成员 代码 实例

在一些工程中我们经常需要访问某些类的静态成员,添加一个友元函数可以达到我们的目的。 

#include <iostream>
#include<cstring>
using namespace std ;


class Student{
private:
string  name; int age; int sore;
public:
Student(string name,int age,int sore)
{
this->name = name;
this->age = age;
this->sore = sore;
} 
friend void Display(Student student);

} ; 


void Display(Student student)
{
cout<<student.name<<"\t"<<student.age<<"\t"<<student.sore<<endl;

}


int main(void)
{
Student a("TOM",13,96);

Display(a);

return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在c++中,冒号 : 和双冒号 :: 的.. 下一篇C++之暴力算法突破二进制枚举子集..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目