检查代码#include<iostream> #include<string.h> using namespace std; class Teacher{ public: string teacher_name; int teacher_age; string teacher_duty; teacher(string teacher_name,int teacher_age){ this->teacher_name=teacher_name; this->teacher_age=teacher_age; teacher_duty="教学"; } }; class Student{ public: string student_name; double student_id; string student_duty; student(string student_name,double student_id){ this->student_name=student_name; this->student_id=student_id; student_duty="学习"; } }; class Graduate{ public: string graduate_name; int graduate_age; double graduate_id; string graduate_duty; graduate(string graduate_name,int graduate_age,double graduate_id){ this->graduate_name=graduate_name; this->graduate_age=graduate_age; this->graduate_id=graduate_id; graduate_duty="教学和学习"; } }; int main(){ Teacher teacher("Tom", 35); Student student("John", "20210001"); Graduate graduate("Mary", 25, "20210002"); cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl; cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl; cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl; return 0; }
时间: 2024-03-17 19:47:14 浏览: 106
这段代码存在一些问题:
1. 在类的声明中,应该添加访问修饰符,比如public、protected、private等,否则默认为private。
2. 在Student类中,student_id应该是int类型而不是double类型。
3. 在Graduate类中,graduate_id应该是int类型而不是double类型。
4. 在main函数中,创建Student和Graduate对象时,传入的参数类型不正确,应该是整型而不是字符串类型。
5. 在Graduate类中,graduate_duty的赋值应该为"教学和学术",而不是"教学和学习"。
修复后的代码如下:
```
#include<iostream>
#include<string.h>
using namespace std;
class Teacher{
public:
string teacher_name;
int teacher_age;
string teacher_duty;
Teacher(string teacher_name,int teacher_age){
this->teacher_name=teacher_name;
this->teacher_age=teacher_age;
teacher_duty="教学";
}
};
class Student{
public:
string student_name;
int student_id;
string student_duty;
Student(string student_name,int student_id){
this->student_name=student_name;
this->student_id=student_id;
student_duty="学习";
}
};
class Graduate{
public:
string graduate_name;
int graduate_age;
int graduate_id;
string graduate_duty;
Graduate(string graduate_name,int graduate_age,int graduate_id){
this->graduate_name=graduate_name;
this->graduate_age=graduate_age;
this->graduate_id=graduate_id;
graduate_duty="教学和学术";
}
};
int main(){
Teacher teacher("Tom", 35);
Student student("John", 20210001);
Graduate graduate("Mary", 25, 20210002);
cout << teacher.teacher_name << " " << teacher.teacher_age<< " " << teacher.teacher_duty<< endl;
cout << student.student_name<< " " << student.student_id << " " << student.student_duty<< endl;
cout << graduate.graduate_name<< " " << graduate.graduate_age << " " << graduate.graduate_id<< " " << graduate.graduate_duty<< endl;
return 0;
}
```
阅读全文