首页 > 编程语言 > 详细

c++友元函数

时间:2020-05-12 20:32:37      阅读:73      评论:0      收藏:0      [点我收藏+]
#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;
}

技术分享图片技术分享图片技术分享图片

c++友元函数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!