Go on marching

With the last homework shown below of this chapter done, I went into a new chapter, which is inheritance. Hope it won't puzzle me so much like operator overloading.
#include <iostream>
#include <string>

using namespace std;

class Student;
class Teacher
{
    public:
        Teacher()
        {
            id=0000;
            name="None";
            sex="None";
            hobby="None";
        }
        Teacher(int i,string n,string s):id(i),name(n),sex(s){}
        friend void change (Teacher &,Student &);
        friend ostream& operator<< (ostream &,Teacher &);
    private:
        int id;
        string name;
        string sex;
        string hobby;
};

class Student
{
    public:

        Student()
        {
            id=0000;
            name="None";
            sex="None";
            hobby="None";
        }
        Student(int i,string n,string s):id(i),name(n),sex(s),hobby("None"){}
        friend void change (Teacher &,Student &);
        friend ostream& operator<< (ostream &,Student &);
    private:
        int id;
        string name;
        string sex;
        string hobby;
};

void change (Teacher &t,Student &s)
{
    t.id=s.id;
    t.name="Mr. "+s.name;
    t.sex=s.sex;
}

ostream& operator<< (ostream &output,Student &i)
{
    output << i.id << " " << i.name << " " << i.sex << " " << i.hobby << endl;
    return output;
}

ostream& operator<< (ostream &output,Teacher &i)
{
    output << i.id << " " << i.name << " " << i.sex << " " << i.hobby << endl;
    return output;
}

int main(void)
{
    Student s(1001,"Makui","Male");
    cout << s;
    Teacher t;
    change(t,s);
    cout << t;
    return 0;
}

评论

此博客中的热门博文

开通此博客的缘由

链表的应用