定義一個Teacher(教師)類,和一個Student(學生)類,兩者有一部分數據成員是相同的,例如num(號碼),name(姓名),sex(性別)。編寫程序,將一個Student對象ios
轉換爲Teacher類,只將以上3個相同的數據成員移植過去,ide
- #include<iostream>
- #include<string>
- using namespace std;
- class Student;
- class Teacher
- {
- public:
- Teacher(){name="";num=0;sex="";}
- Teacher(string a,int b,string c){name=a;num=b;sex=c;}
- Teacher operator =(Student &b);
- void display();
- string name;
- int num;
- string sex;
- };
- void Teacher::display()
- {
- cout<<name<<" "<<num<<" "<<sex;
- }
- class Student
- {
- public:
- Student(){name="";num=0;sex="";}
- Student(string a,int b,string c){name=a;num=b;sex=c;}
- void display();
- string name;
- int num;
- string sex;
- };
- void Student::display()
- {
- cout<<name<<" "<<num<<" "<<sex;
- }
- Teacher Teacher::operator =(Student &b)
- {
- name=b.name;
- num=b.num;
- sex=b.sex;
- return *this;
- }
- int main()
- {
- Teacher t1("張三",101,"女"),t2;
- t1.display();
- cout<<endl;
- Student s1("李四",168,"男");
- s1.display();
- cout<<endl;
- t2=s1;
- t2.display();
- }