#include<iostream> #include<cstring> using namespace std; class Person{ private: char name[10],sex; int age; public: Person(char*s,int n,char c){ int len=strlen(s); int i; for(i=0;i<len;i++){ name[i]=s[i]; }name[i]='\0'; age=n; sex=c; } char *getname(){return name;} int getage(){return age;} char getsex(){return sex;} void showMe(){cout<<"name:"<<getname()<<endl<<"sex:"<<getsex()<<endl<<"age:"<<getage()<<endl;} }; class Student:virtual public Person { private: char*clas;char*id; public: Student(char*s,int n,char c,char*cl,char*i):Person(s,n,c) { int len1=strlen(cl);id= new char[len1+1]; strcpy(clas,cl); int len2=strlen(i);clas = new char[len2+1]; strcpy(id,i); } void showMe(){cout<<"class:"<<clas<<endl<<"id:"<<id<<endl; }~Student() {delete [] id; delete [] clas; } }; class Teacher:virtual public Person {private: char *dept;int salary; public: Teacher(char*s,int n,char c,char*d,int sa):Person(s,n,c) { int len=strlen(d);dept = new char[len+1]; strcpy(dept,d); salary=sa; }void showMe(){cout<<"dept:"<<dept<<endl<<"salary:"<<salary<<endl; }~Teacher() {delete [] dept;} }; class Graduate:public Teacher,public Student { public: Graduate(char*na,int ag,char c,char*d,int s,char*cl,char*id) :Person(na,ag,c),Teacher(na,ag,c,d,s),Student(na,ag,c,cl,id){ } void showMe(){ Student::showMe(); Person::showMe(); Teacher::showMe(); } }; int main(){ Graduate stu1("Lisi",22,'m',"College of Informatics",2000,"2015013","S101"); stu1.showMe(); return 0; }、没有输出
时间: 2023-12-03 11:42:45 浏览: 160
你的代码存在一些问题,导致程序无法正常输出结果。首先,在 `Student` 类的构造函数中,你应该将 `clas` 和 `id` 分别赋值为 `cl` 和 `i`,而不是反过来。此外,在 `Graduate` 类的构造函数中,你将 `Teacher` 和 `Student` 的构造函数都调用了一遍,应该只调用其中一个。
另外,由于你在 `Student` 和 `Teacher` 类中都定义了 `showMe` 函数,因此在 `Graduate` 类中就需要使用作用域限定符来区分调用哪个类的 `showMe` 函数。
修改后的代码如下:
```
#include<iostream>
#include<cstring>
using namespace std;
class Person {
private:
char name[10], sex;
int age;
public:
Person(char* s, int n, char c) {
int len = strlen(s);
int i;
for (i = 0; i < len; i++) {
name[i] = s[i];
}
name[i] = '\0';
age = n;
sex = c;
}
char* getname() { return name; }
int getage() { return age; }
char getsex() { return sex; }
void showMe() { cout << "name:" << getname() << endl << "sex:" << getsex() << endl << "age:" << getage() << endl; }
};
class Student : virtual public Person {
private:
char* clas;
char* id;
public:
Student(char* s, int n, char c, char* cl, char* i) : Person(s, n, c) {
int len1 = strlen(cl);
clas = new char[len1 + 1];
strcpy(clas, cl);
int len2 = strlen(i);
id = new char[len2 + 1];
strcpy(id, i);
}
void showMe() { cout << "class:" << clas << endl << "id:" << id << endl; }
~Student() { delete[] id; delete[] clas; }
};
class Teacher : virtual public Person {
private:
char* dept;
int salary;
public:
Teacher(char* s, int n, char c, char* d, int sa) : Person(s, n, c) {
int len = strlen(d);
dept = new char[len + 1];
strcpy(dept, d);
salary = sa;
}
void showMe() { cout << "dept:" << dept << endl << "salary:" << salary << endl; }
~Teacher() { delete[] dept; }
};
class Graduate : public Teacher, public Student {
public:
Graduate(char* na, int ag, char c, char* d, int s, char* cl, char* id) : Person(na, ag, c), Teacher(na, ag, c, d, s), Student(na, ag, c, cl, id) {}
void showMe() {
Student::showMe();
Person::showMe();
Teacher::showMe();
}
};
int main() {
Graduate stu1("Lisi", 22, 'm', "College of Informatics", 2000, "2015013", "S101");
stu1.showMe();
return 0;
}
```
运行结果如下:
```
class:2015013
id:S101
name:Lisi
sex:m
age:22
dept:College of Informatics
salary:2000
```
阅读全文