这段代码哪错了 #include <iostream> #include <iomanip> using namespace std; class Staff { protected: int code; string name; static int count; public: Staff(string n) { name = n; code = count; count++; } void SetName(string s) { name = s; } string GetName() { return name; } int GetCode() { return code; } static int GetStaffCount() { return count; } }; class Teacher : virtual public Staff { protected: string subject; public: Teacher(string n, string m) : Staff(n) { subject = m; } void SetSubject(string s) { subject = s; } string GetSubject() { return subject; } }; class Officer : virtual public Staff { protected: string work; public: Officer(string n, string m) : Staff(n) { work = m; } void SetWork(string s) { work = s; } string GetWork() { return work; } }; class Teacher_Officer : public Teacher, public Officer { public: Teacher_Officer(string n, string m, string s) : Staff(n), Teacher(n, m), Officer(n, s) {} }; int main() { Teacher t1("孔子", "儒家文学"); // 创建教学类员工t1 Teacher t2("公输班", "建筑学"); Officer o1("端木赐", "财务科长"); // 创建教辅类员工o1 Teacher_Officer to3("颜回", "儒家文学", "副院长"); // 创建教师教辅类员工to3 cout << "共有教职工" << Staff::GetStaffCount() << "人" << endl; cout << t1.GetCode() << ", " << t1.GetName() << ", " << t1.GetSubject() << endl; cout << t2.GetCode() << ", " << t2.GetName() << ", " << t2.GetSubject() << endl; cout << o1.GetCode() << ", " << o1.GetName() << ", " << o1.GetWork() << endl; cout << to3.GetCode() << ", " << to3.GetName() << ", " << to3.GetSubject() << ", " << to3.GetWork() << endl; return 0; }
时间: 2023-06-20 22:09:19 浏览: 103
#include_iostream.h_
4星 · 用户满意度95%
这段代码有两个问题:
1.需要将 `count` 初始化为 0,因为静态成员变量不会自动初始化为 0。
2.在 `Teacher_Officer` 类的构造函数中,应该只给 `Teacher` 和 `Officer` 传递它们自己的参数,而不是将 `Staff` 类的参数也传递进去。
修改后的代码如下:
```
#include <iostream>
#include <iomanip>
using namespace std;
class Staff {
protected:
int code;
string name;
static int count;
public:
Staff(string n) {
name = n;
code = count;
count++;
}
void SetName(string s) {
name = s;
}
string GetName() {
return name;
}
int GetCode() {
return code;
}
static int GetStaffCount() {
return count;
}
};
int Staff::count = 0;
class Teacher : virtual public Staff {
protected:
string subject;
public:
Teacher(string n, string m) : Staff(n) {
subject = m;
}
void SetSubject(string s) {
subject = s;
}
string GetSubject() {
return subject;
}
};
class Officer : virtual public Staff {
protected:
string work;
public:
Officer(string n, string m) : Staff(n) {
work = m;
}
void SetWork(string s) {
work = s;
}
string GetWork() {
return work;
}
};
class Teacher_Officer : public Teacher, public Officer {
public:
Teacher_Officer(string n, string m, string s) : Teacher(n, m), Officer(n, s) {}
};
int main() {
Teacher t1("孔子", "儒家文学"); // 创建教学类员工t1
Teacher t2("公输班", "建筑学");
Officer o1("端木赐", "财务科长"); // 创建教辅类员工o1
Teacher_Officer to3("颜回", "儒家文学", "副院长"); // 创建教师教辅类员工to3
cout << "共有教职工" << Staff::GetStaffCount() << "人" << endl;
cout << t1.GetCode() << ", " << t1.GetName() << ", " << t1.GetSubject() << endl;
cout << t2.GetCode() << ", " << t2.GetName() << ", " << t2.GetSubject() << endl;
cout << o1.GetCode() << ", " << o1.GetName() << ", " << o1.GetWork() << endl;
cout << to3.GetCode() << ", " << to3.GetName() << ", " << to3.GetSubject() << ", " << to3.GetWork() << endl;
return 0;
}
```
阅读全文