0
点赞
收藏
分享

微信扫一扫

C++友元函数

#include <iostream>



#include <string>



using namespace std;






class Student



{



private:


int age;



public:


setage(int n);


friend int getage(Student &s);
//
定义友元函数



};






Student::setage(int n)



{


age=n;



}



int getage(Student &s)



{


return s.age;
//友元函数可以有访问类私有变量的权限



}






int main()



{


Student jack;


jack.setage(11);





int result=getage(jack);


cout<<result<<endl;





return 0;



}

举报

相关推荐

0 条评论