TTMer 2020-05-09 11:40 采纳率: 100%
浏览 177
已采纳

子类与父类输出操作符重载出错

下面代码中两个输出运算符重载无法实现,应该怎么解决

#include<iostream>
#define defaultValue "0"
using namespace std;
class Person{
protected:
    string m_strName;
public:
    Person() { cout << "Person constructing" << endl; }
    ~Person() { cout << "Person destructing" << endl; }
    Person(const string&);
    friend ostream& operator<<(ostream&, const Person&);
    friend istream& operator>>(istream&, Person&);
};

class Student:public Person {
    static int m_nLastID;
public:
    Student() {
        cout << "Student constructing" << endl;
        m_strSID = ++m_nLastID; 
    }
    ~Student(){ cout << "Student destructing" << endl; }
    Student(const string&name);
    string m_strSID= defaultValue;
    friend ostream& operator<<(ostream&, const Student&);
    friend istream& operator>>(istream&, Student&);
};

#include "Person.h"

ostream & operator<<(ostream &o, const Person &p)
{
    o << p.m_strName;
    return o;
}

istream & operator>>(istream &i, Person &p)
{
    char a[256];
    i >> a;
    p.m_strName = a;
    return i;
}

ostream & operator<<(ostream &o,  const Student &s)
{
    o << s.Person::m_strName<<' '<<s.m_strSID;
    return o;
}

istream & operator>>(istream &i, Student &s)
{
    char a[256]{0};
    i >> a;
    s.Person::m_strName = a;
    return i;
}

Person::Person(const string &s)
{
    m_strName = s;
}

Student::Student(const string & name)
{
    m_strName = name;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-09 12:08
    关注

    问题解决的话,请点下采纳

    #include<iostream>
    #define defaultValue 0
    using namespace std;
    class Person{
    protected:
        string m_strName;
    public:
        Person() { cout << "Person constructing" << endl; }
        ~Person() { cout << "Person destructing" << endl; }
        Person(const string&);
        friend ostream& operator<<(ostream&, const Person&);
        friend istream& operator>>(istream&, Person&);
    };
    
    class Student:public Person {
    private:
        static int m_nLastID;
    public:
        int m_strSID;
        Student() {
            cout << "Student constructing" << endl;
            int m_strSID = defaultValue;
            m_strSID = ++m_nLastID; 
        }
        ~Student(){ cout << "Student destructing" << endl; }
        Student(const string&name);
        friend ostream& operator<<(ostream&, const Student&);
        friend istream& operator>>(istream&, Student&);
    };
    
    int Student::m_nLastID = 0;
    
    ostream & operator<<(ostream &o, const Person &p)
    {
        o << p.m_strName;
        return o;
    }
    
    istream & operator>>(istream &i, Person &p)
    {
        char a[256];
        i >> a;
        p.m_strName = a;
        return i;
    }
    
    ostream & operator<<(ostream &o,  const Student &s)
    {
        o << s.Person::m_strName<<' '<<s.m_strSID;
        return o;
    }
    
    istream & operator>>(istream &i, Student &s)
    {
        char a[256]={0};
        i >> a;
        s.Person::m_strName = a;
        return i;
    }
    
    Person::Person(const string &s)
    {
        m_strName = s;
    }
    
    Student::Student(const string & name)
    {
        m_strName = name;
    }
    
    int main() 
    {
        Student s;
        cout << s;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容