先上代码
.h
#include <iostream>
using namespace std;
#ifndef PERSON_H
#define PERSON_H
//虚基类Person
class Person
{
protected:
string name, id, sex;
public:
Person(string _name, string _id, string _sex);
void print_info();
};
//派生类Student
class Student : virtual public Person
{
protected:
string major, grade;
public:
Student(string _name, string _id, string _sex, string _major, string _grade);
void print_info();
};
//派生类Assistant
class Assistant : virtual public Person
{
protected:
string school;
double salary;
public:
Assistant(string _name, string _id, string _sex, string _school, double _salary);
void print_info();
};
//最派生类Studen_assistant
class Student_assistant : public Student, public Assistant
{
public:
Student_assistant(string _name, string _id, string _sex,
string _major, string _grade, string _school, double _salary);
void print_info();
};
#endif
.cpp
#include "person.h"
Person::Person(string _name, string _id, string _sex)
{
name = _name;
id = _id;
sex = _sex;
};
void Person::print_info()
{
printf("name:%s id:%s sex:%s\n", name.c_str(), id.c_str(), sex.c_str());
};
Student::Student(string _name, string _id, string _sex, string _major, string _grade)
: Person(_name, _id, _sex)
{
major = _major;
grade = _grade;
};
void Student::print_info()
{
printf("name:%s id:%s sex:%s major:%s grade:%s\n",
name.c_str(), id.c_str(), sex.c_str(), major.c_str(), grade.c_str());
};
Assistant::Assistant(string _name, string _id, string _sex, string _school, double _salary)
: Person(_name, _id, _sex)
{
school = _school;
salary = _salary;
};
void Assistant::print_info()
{
printf("name:%s id:%s sex:%s school:%s salary:%.2f\n",
name.c_str(), id.c_str(), sex.c_str(), school.c_str(), salary);
};
Student_assistant::Student_assistant(string _name, string _id, string _sex,
string _major, string _grade, string _school, double _salary)
: Person(_name, _id, _sex), Student(_name, _id, _sex, _major, _grade), Assistant(_name, _id, _sex, _school, _salary)
{
//test
printf("name:%s id:%s sex:%s major:%s grade:%s school:%s salary:%.2f\n",
name.c_str(), id.c_str(), sex.c_str(), major.c_str(), grade.c_str(), school.c_str(), salary);
//test
}
void Student_assistant::print_info()
{
//test
cout << major << name << endl;
//test
printf("name:%s id:%s sex:%s major:%s grade:%s school:%s salary:%.2f\n",
name.c_str(), id.c_str(), sex.c_str(), major.c_str(), grade.c_str(), school.c_str(), salary);
}
main.cpp
#include "person.h"
inline int U(char *str)
{
return (str[0] + (str[1] ? U(str + 1) : 0));
};
constexpr inline int U(const char *str)
{
return (str[0] + (str[1] ? U(str + 1) : 0));
};
int main()
{
string name = "a", id = "12", sex = "male", major = "math", grade = "freshman", school = "huft";
double salary = 1000;
Person a(name, id, sex);
Student b(name, id, sex, major, grade);
Assistant c(name, id, sex, school, salary);
Student_assistant d(name, id, sex, major, grade, school, salary);
char str[2];
cout << "please operate\nP:person S:student A:assiatant SA:student-assistant E:exit" << endl;
for (bool bo = true; bo;)
{
cin >> str;
switch (U(str))
{
case U("P"):
a.print_info();
break;
case U("S"):
b.print_info();
break;
case U("A"):
c.print_info();
break;
case U("SA"):
d.print_info();
break;
case U("E"):
bo = false;
break;
default:
cout << "command wrong" << endl;
break;
}
}
return (0);
}
跑出来出错
此外我还试了这么写
...
void Student_assistant::print_info()
{
Student::print_info();
cout << "school:" << school << " salary:" << salary << endl;
}
...
结果也不行,报错位置转到了Student::print_info()
求解答或者线索
上网找了好久也找不到问题所在,我快怀疑是我的编译器出问题了,编辑器用的vscode,编译器用的c++11
另外如果代码中有值得优化的地方恳请指教
我正常运行,我只修改了这里
void Student_assistant::print_info()
{
//test
cout << major.c_str() << name.c_str() << endl;
//test
printf("name:%s id:%s sex:%s major:%s grade:%s school:%s salary:%.2f\n",
name.c_str(), id.c_str(), sex.c_str(), major.c_str(), grade.c_str(), school.c_str(), salary);
}
cout << major.c_str() << name.c_str() << endl;
string转了char