定义一个在校人员Person类包含成员姓名(字符串),性别(,女用字符w或W,男用字符m或M),年龄(大于0的整数)和构造函数及show成员函数;以Person作为基类派生出学生…
- User
定义一个在校人员Person类包含成员姓名(字符串),性别(,女用字符w或W,男用字符m或M),年龄(大于0的整数)和构造函数及show成员函数;以Person作为基类派生出学生Student类扩展成员学号(大于0的整数),成绩(实数)成员和构造函数及show成员函数;请实例化两个类的对象,然后再分别显示出成员信息。 参考主函数: int main() { string name; char sex; int age, num; float score; cin >> num >> name >> sex >> age >> score; Person p(name, sex, age); p.show(); cout << endl; Student s(num, name, sex, age, score); s.show(); return 0;
}C++编译
- Assistant
代码如下:
#include #include using namespace std;
class Person{ public: Person(string n, char s, int a){ name = n; sex = s; age = a; } void show(){ cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; cout << "Age: " << age << endl; } protected: string name; char sex; int age; };
class Student : public Person{ public: Student(int n, string na, char s, int a, float sc) : Person(na, s, a){ num = n; score = sc; } void show(){ Person::show(); cout << "Student ID: " << num << endl; cout << "Score: " << score << endl; } private: int num; float score; };
int main(){ string name; char sex; int age, num; float score; cin >> num >> name >> sex >> age >> score; Person p(name, sex, age); p.show(); cout << endl; Student s(num, name, sex, age, score); s.show(); return 0; }
- User
编译错误. Main.cpp:1:9: error: #include expects "FILENAME" or #include ^ Main.cpp:12:1: error: stray '\342' in program if(age<=0||(sex!=‘m’&&sex!=‘M’&&sex!=‘w’&&sex!=‘W’)){ ^ Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\230' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\231' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\230' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\231' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\230' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\231' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\230' in program Main.cpp:12:1: error: stray '\342' in program Main.cpp:12:1: error: stray '\200' in program Main.cpp:12:1: error: stray '\231' in program Main.cpp:13:1: error: stray '\342' in program cout<<“信息错误!”<<endl; ^ Main.cpp:13:1: error: stray '\200' in program Main.cpp:13:1: error: stray '\234' in program Main.cpp:13:1: error: stray '\344' in program Main.cpp:13:1: error: stray '\277' in program Main.cpp:13:1: error: stray '\241' in program /home/CourseGrading/coursework/workspace/judgesrc/exam/1367/2147483524/754
- Assistant
该代码出现了多个编译错误,具体如下:
-
第一行的 #include 语句应该包含头文件名或系统库名。
-
第12行的 if 语句中的单引号应该使用双引号。
-
在第13行中的“信息错误!”字符串中,引号应该使用英文半角符号。
-
每行结尾应该使用分号进行结束,第12和13行都没有分号。
可进行修正后重新编译调试。
参考修正后代码如下:
#include using namespace std;
int main(){ int age; char sex; cin >> age >> sex; if(age <= 0 || (sex != "m" && sex != "M" && sex != "w" && sex != "W")){ cout << "信息错误!" << endl; } else{ cout << "信息正确!" << endl; } return 0; }
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.