一、实验目的:
1、在实验2的基础上,在类Person中,以友元形式重载输入输出运算符〈〈、〉〉;
2、通过“cout<<teach”或“cout<<stud”,调用右值类的虚函数Display(),显示右值对象的信息。
3、通过“cin>>teach”或“cin>>stud”,调用右值类的虚函数SetValue(),通过键盘输入右值对象的信息。
1 #include2 #include 3 #include"stdafx.h" 4 #include 5 using namespace std; 6 7 class TDate{ 8 protected: 9 int Year,Month,Day; 10 11 public: 12 TDate() 13 { 14 } 15 TDate(int year,int month,int day){ 16 Year=year; 17 Month=month; 18 Day=day; 19 } 20 21 }; 22 23 class Person:public TDate{ 24 protected: 25 int ID; 26 static int Count; 27 string Name,Sex; 28 public: 29 Person() 30 { 31 32 } 33 Person(int id,string name,string sex,int year,int month,int day):TDate(year,month,day) 34 { 35 //ID=id; 36 Name=name; 37 Sex=sex; 38 } 39 virtual void Display() 40 { 41 cout<<"姓名:"< < >Name;110 cout<<"性别:";111 cin>>Sex;112 cout<<"出生年份:";113 cin>>Year;114 cout<<"出生月份:";115 cin>>Month;116 cout<<"出生日期:";117 cin>>Day;118 cout<<"年级:";119 cin>>Grade;120 cout<<"专业:";121 cin>>Major;122 cout< >(istream &in,Student &ob)131 {132 ob.SetValue();133 return in;134 }135 };136 int main()137 {138 Teacher teach("讲师","计算机系",1,"张山","男",1978,11,11);139 Student stud;140 cout<<"------------请输入学生信息---------------"< >stud;142 cout< >i;146 return 0;147 }