R6-2 【2020CPP27】学生及教师信息输出

分别定义教师类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;

    }

};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值