分别定义教师类Teacher和学生类Student,displayInfo(Student, Teacher)是教师类Teacher和学生类Student的友元函数,主函数调用displayInfo()完成教师和学生信息的输出。
Student类结构说明:
Student类的数据成员包括:
①私有数据成员:姓名name(string)。
②私有数据成员:成绩score(int)。
Student类的成员函数包括:
①有参构造函数Student(string,int),参数用于初始化学生姓名name和成绩score,其中int型参数默认值为0。
Teacher类结构说明:
Teacher类的数据成员包括:
①私有数据成员:姓名name(string)。
②私有数据成员:工资wages(double)。
Teacher类的成员函数包括:
①有参构造函数Teacher(string,double),参数用于初始化教师姓名name和工资wages,其中double型参数默认值为0.0。
裁判测试程序:
#include<iostream>
#include <string>
using namespace std;
class Teacher;//前向引用声明
class Student;//前向引用声明
/*请在这里填写答案*/
void displayInfo(Student s, Teacher t){
cout<<"学生姓名:"<<s.name<<",学生成绩:"<<s.score<<endl;
cout<<"教师姓名:"<<t.name<<",教师工资:"<<t.wages<<endl;
}
int main() {
string s1,s2;
int d1;
double d2;
cin>>s1>>s2;
cin>>d1>>d2;
Student student(s1,d1);
Teacher teacher(s2,d2);
displayInfo(student, teacher);
return 0;
}
输入样例:
Student Teacher
628 6037.59
输出样例:
学生姓名:Student,学生成绩:628
教师姓名:Teacher,教师工资:6037.59
class Student{
public:
string name;
int score;
Student(string a,double b=0.0)
{
name=a;
score=b;
}
};
class Teacher{
public:
string name;
double wages;
Teacher(string a,double b=0.0)
{
name=a;
wages=b;
}
};