#include<iostream>
using namespace std;
class Student
{
public:
Student(const char *name,int age,float score);
friend void show(Student *pstu);//声明友元函数
private:
const char* m_name;
int m_age;
float m_score;
};
Student::Student(const char* name, int age, float score)
{
m_name = name;
m_age = age;
m_score = score;
}
//友元函数定义
void show(Student* pstu)
{
cout<<pstu-> m_name << endl;
cout <<pstu-> m_age << endl;
cout <<pstu-> m_score << endl;
}
int main()
{
Student stu("张无忌",19,88.5);
show(&stu);//调用友元函数
Student* pstu = new Student("赵敏",19,55);
show(&stu);
delete pstu;
return 0;
}



原文:https://www.cnblogs.com/qq1480040000/p/12878251.html