//下面是友元函数的定义和使用 学生姓名 成绩
#include<iostream.h>
#include<string.h>
class Student //学生类的定义
{
private:
char name[20],number[14];
public:
Student(char *s1,char *s2)
{
strcpy(name,s1);
strcpy(number,s2);
}
friend void show(Student st); //友元函数的声明
};
class Score //成绩类的定义
{
private:
unsigned int math,physics,english;
public:
Score(unsigned int i1,unsigned int i2,unsigned int i3)
{
math=i1;physics=i2; english=i3;
}
friend void show_all(Student st,Score sc); //友元函数的声明
};
void show(Student st) //友元函数的定义
{
cout<<"name:"<<st.name<<endl<<"number:"<<st.number<<endl;
}
void show_all(Student st,Score sc) //友元函数的定义
{
show(st);
cout<<"math:"<<sc.math<<endl<<"physics:"<<sc.physics<<endl;
cout<<"english:"<<sc.english<<endl;
}
void main()
{
Student wen("wenxianliang","3081817124");
Score score(85,95,89);
show_all(wen,score);
}
#include<iostream.h>
#include<string.h>
class Student //学生类的定义
{
private:
char name[20],number[14];
public:
Student(char *s1,char *s2)
{
strcpy(name,s1);
strcpy(number,s2);
}
friend void show(Student st); //友元函数的声明
};
class Score //成绩类的定义
{
private:
unsigned int math,physics,english;
public:
Score(unsigned int i1,unsigned int i2,unsigned int i3)
{
math=i1;physics=i2; english=i3;
}
friend void show_all(Student st,Score sc); //友元函数的声明
};
void show(Student st) //友元函数的定义
{
cout<<"name:"<<st.name<<endl<<"number:"<<st.number<<endl;
}
void show_all(Student st,Score sc) //友元函数的定义
{
show(st);
cout<<"math:"<<sc.math<<endl<<"physics:"<<sc.physics<<endl;
cout<<"english:"<<sc.english<<endl;
}
void main()
{
Student wen("wenxianliang","3081817124");
Score score(85,95,89);
show_all(wen,score);
}