【问题标题】:Error reading characters of string Person Class读取字符串 Person 类的字符时出错
【发布时间】: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&lt;&lt;"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();
      

}

【问题讨论】:

标签: c++ string class


【解决方案1】:

当你创建 Teacher 对象时,他的父类 Person 使用默认构造函数 Education_part()。我认为您的默认构造函数不会初始化 Education_part 的成员,例如电子邮件、电话等。所以要使用另一个构造函数,您需要编写 Teacher(string Subject, string Position, string Degree): Education_part("805034521", 5988, "Kherson, Boguna 6, 122", "goward@gmail.com"); 而不是 Teacher(string Subject, string Position, string Degree);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多