#include<iostream> #include<string.h> using namespace std; class stu{ private: string name; int num; int cla; public: stu(){} stu(string a,int b,int c){ name=a; num=b; cla=c; } void show1(){ cout<<num<<"->"<<name<<"->"<<cla<<endl; } }; class student:public stu{ private: string school; string teacher; string major; public: student(){} student(string d,string e,string f);stu(name,num,cla){ school=d; teacher=e; major=f; } void show2(){ cout<<school<<"->"<<teacher<<"->"<<major<<endl; } }; int main(){ string name,school,teacher,major; int num,cla; cin>>name>>num>>cla>>school>>teacher>>major; stu x1(name,num,cla); x1.show1(); student x2(school,teacher,major); x2.show2(); return 0; }
时间: 2023-06-15 09:04:44 浏览: 202
这段代码存在一些错误:
1. `student(string d,string e,string f);stu(name,num,cla)` 这一行应该改为 `student(string d,string e,string f,string a,int b,int c):stu(a,b,c)`,这里需要在构造函数中调用基类(即`stu`类)的构造函数来初始化基类的成员变量。
2. 在 `main` 函数中创建 `student` 对象时只传入了三个参数,而 `student` 类需要传入 6 个参数,应该改为 `student x2(school,teacher,major,name,num,cla);`。
修改后的代码如下:
```
#include<iostream>
#include<string.h>
using namespace std;
class stu{
private:
string name;
int num;
int cla;
public:
stu(){}
stu(string a,int b,int c){
name=a;
num=b;
cla=c;
}
void show1(){
cout<<num<<"->"<<name<<"->"<<cla<<endl;
}
};
class student:public stu{
private:
string school;
string teacher;
string major;
public:
student(){}
student(string d,string e,string f,string a,int b,int c):stu(a,b,c){
school=d;
teacher=e;
major=f;
}
void show2(){
cout<<school<<"->"<<teacher<<"->"<<major<<endl;
}
};
int main(){
string name,school,teacher,major;
int num,cla;
cin>>name>>num>>cla>>school>>teacher>>major;
stu x1(name,num,cla);
x1.show1();
student x2(school,teacher,major,name,num,cla);
x2.show2();
return 0;
}
```
阅读全文