【发布时间】:2021-05-03 08:27:51
【问题描述】:
有父类Person,子类Education部分,以及Education部分的子类-Teacher
它们每个都有公共函数 void print();
我在教师中放了例如教育部分的打印功能。
void Teacher::print()
{
cout << subject << "\n" <<position << "\n"<<degree<<"\n"<< endl;
Education_part::print();
}
并且它不打印调试器写入错误读取字符串字符并给出 -88897459855 答案。
当我像这样cout<<"hello" 写在教育部分 print () 并且不使用我在课堂上的数据时 - 一切正常。
这里的类:
class Education_part : public Person
{
public:
Education_part();
Education_part(string phone,
int paymant,
string mAdress,
string m_email);
//getters/seters
public:
string get_Ph() const { return Phone; }
int get_Pay() const { return Payment; }
string get_Adress() const { return adress; }
string get_email() const { return email; }
void set_Ph(string phone) { Phone = phone; }
void set_Pay(int paymant) { Payment = paymant; }
void set_Adress(string mAdress) { adress = mAdress; }
void set_email(string m_email) { email = m_email; }
void print();
private:
string Phone;
int Payment;
string adress;
string email;
};
class Teacher : public Education_part
{
public:
Teacher();
Teacher(string Subject, string Position, string Degree);
public:
string get_Subject() const { return subject; }
string get_Position() const { return position; }
string get_Degree() const { return degree; }
void set_Subject(string Subject) { subject = Subject; }
void set_Position(string Position) { position = Position; }
void set_Degree(string Degree) { degree = Degree; }
void print();
private:
string subject;
string position;
string degree;
};
这里是主要的
int main()
{
Teacher::Education_part e2("805034521", 5988, "Kherson, Boguna 6, 122", "goward@gmail.com");
Teacher t("Criminal Law", "teacher", "doctor of philosofy");
t.print();
}
【问题讨论】:
-
为什么
Education_part是Person的一种? 见Liskov substitution principle