一、实验目的:
1、在实验2的基础上,在类Person中,以友元形式重载输入输出运算符〈〈、〉〉;
2、通过“cout<<teach”或“cout<<stud”,调用右值类的虚函数Display(),显示右值对象的信息。
3、通过“cin>>teach”或“cin>>stud”,调用右值类的虚函数SetValue(),通过键盘输入右值对象的信息。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include"stdafx.h" 4 #include<string> 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<<endl; 42 cout<<"性别:"<<Sex<<endl; 43 } 44 }; 45 int Person::Count=0; 46 47 class Teacher:public Person{ 48 protected: 49 string Title; 50 string Dept; 51 public: 52 Teacher(){ 53 Count++; 54 ID=Count; 55 } 56 Teacher(string title,string dept,int id,string name,string sex,int year,int month,int day):Person(id,name,sex,year,month,day) 57 { 58 Count++; 59 ID=Count; 60 Title=title; 61 Dept=dept; 62 } 63 virtual void Display() 64 { 65 cout<<"-----老师信息-----"<<endl; 66 cout<<"编号:"<<ID<<endl; 67 cout<<"姓名:"<<Name<<endl; 68 cout<<"性别:"<<Sex<<endl; 69 cout<<"出生日期:"<<Year<<"年"<<Month<<"月"<<Day<<"日"<<endl; 70 cout<<"职称:"<<Title<<endl; 71 cout<<"所在系:"<<Dept<<endl; 72 } 73 friend ostream &operator<<(ostream &out,Teacher &ob) 74 { 75 ob.Display(); 76 out<<endl; 77 return out; 78 } 79 }; 80 class Student:public Person{ 81 protected: 82 string Major,Grade; 83 public: 84 Student() 85 { 86 Count++; 87 ID=Count; 88 } 89 Student(int id,string name,string sex,int year,int month,int day,string major,string grade):Person(id,name,sex,year,month,day) 90 { 91 Count++; 92 ID=Count; 93 Major=major; 94 Grade=grade; 95 } 96 virtual void Display() 97 { 98 cout<<"----------学生信息----------"<<endl; 99 cout<<"编号:"<<ID<<endl; 100 cout<<"姓名:"<<Name<<endl; 101 cout<<"性别:"<<Sex<<endl; 102 cout<<"出生日期:"<<Year<<"年"<<Month<<"月"<<Day<<"日"<<endl; 103 cout<<"专业:"<<Major<<endl; 104 cout<<"年级:"<<Grade<<endl; 105 } 106 virtual void SetValue() 107 { 108 cout<<"姓名:"; 109 cin>>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<<endl; 123 } 124 friend ostream &operator<<(ostream &out,Student &ob) 125 { 126 ob.Display(); 127 out<<endl; 128 return out; 129 } 130 friend istream &operator>>(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<<"------------请输入学生信息---------------"<<endl; 141 cin>>stud; 142 cout<<teach; 143 cout<<stud; 144 int i; 145 cin>>i; 146 return 0; 147 }