求解一个C++问题

2025-03-03 06:00:41
推荐回答(2个)
回答1:

因为不知道程序的本意,所以这个程序有很多种方法可以修改为正确的,且修改完都不一样。这个只是按照我想得修改的。忘能采纳,有不懂的在问

#include
#include
using namespace std;
class Teacher
{
protected: //此处要声明为protected或public,否则Preteacher子类无权限访问成员
char name[20];
int age;
char title[20];
char sex[20];
char tele[20];
public:
Teacher(char nam[],int a,char ses[],char tel[],char t[])
{
age=a;
strcpy(name,nam);
strcpy(sex,ses);
strcpy(tele,tel);
strcpy(title,t);
}
void display();
};
void Teacher::display()
{
cout<<"name:"< cout<<"age"< cout<<"sex"< cout<<"tele"< cout<<"title"<}

class Student
{
protected: //此处要声明为protected或public,否则Preteacher子类无权限访问成员
char name[20];
int age;
char tele[20];
char sex[20];
char classes[20];
public:
Student (char nam[],int a,char ses[],char tel[],char c[])
{
age=a;
strcpy(name,nam);
strcpy(sex,ses);
strcpy(tele,tel);
strcpy(classes,c);
}
void display();
};

void Student::display()
{
cout<<"name:"< cout<<"age"< cout<<"sex"< cout<<"tele"< cout<<"classes"<};

class Preteacher:public Teacher,public Student
{
public:
Preteacher(char* nam,int a,char* ses,char* tel,char* t,char* c,int time)://不能用string类型初始化char数组
Teacher(nam,a,ses,tel,t),Student(nam,a,ses,tel,c),pretime(time)
{}

void preshow()
{
cout<<"name:"< cout<<"age:"< cout<<"sex:"< cout<<"tele:"< cout<<"title:"< cout<<"classs:"< cout<<"pretime:"<}
private:
int pretime;
};

int main()
{
Preteacher liu("xiaoming",25,"a","12345678910","assistant","maths",2);//倒数第二个参数应该是char*
liu.preshow();
return 0;
}

回答2:

好多问题,先将这个改过来吧:
srting nam改成string nam