已有教师类的部分代码如下:
/* 请在这里填写答案 */
class Teacher{
private:
string name; //姓名
string course; //所教课程
public:
Teacher(){};
Teacher(string name_,string course_);
string getName(){return name;}
string getCourse(){return course;}
};
/* 请在这里填写答案 */
int main(){
Class c1(2,1,"ZhangXiaowen","math");
Class c2(3,2,"LiSisi");
Class c3(1,1);
Class c4;
return 0;
}
输入样例:
本例没有输入。
输出样例:
在这里给出相应的输出。例如:
grade:2 class no:1,head teacher:ZhangXiaowen(math)
grade:3 class no:2,head teacher:LiSisi(chinese)
grade:1 class no:1,head teacher:unknow(chinese)
new class created.
Ans1:
#include <iostream>
#include <string>
using namespace std;
Ans2:
Teacher::Teacher(string name_, string course_) {
this->name=name_;
this->course=course_;
}
class Class{
public:
Class(int a, int b, const string& tch, const string& cor):grade(a),class_no(b),teacher(tch, cor){
cout<<"grade:"<<this->grade<<" class no:"<<this->class_no<<",head teacher:"<<tch<<"("<<cor<<")"<<endl;
}
Class(int a, int b, const string& tch):grade(a),class_no(b), teacher(tch, "chinese"){
cout<<"grade:"<<this->grade<<" class no:"<<this->class_no<<",head teacher:"<<tch<<"("<<teacher.getCourse()<<")"<<endl;
}
Class(int a, int b):grade(a),class_no(b), teacher("unknow","chinese"){
cout<<"grade:"<<this->grade<<" class no:"<<this->class_no<<",head teacher:"<<teacher.getName()<<"("<<teacher.getCourse()<<")"<<endl;
}
Class(){
cout<<"new class created."<<endl;
}
private:
int grade;
int class_no;
Teacher teacher;
};