学校人员信息的抽象与封装
一、实验目的:1、根据学生老师之间的关联关系,利用继承性和派生类,设计若干自定义类;
1、 掌握类的成员成员、静态成员和虚函数;
2、 定义和调用用户函数PrintInfo(Person &ob),体会虚函数的特性。
实现一个win32控制台的应用程序,可以对学校人员的信息输入和显示。
1 #include "stdafx.h" 2 #include "iostream" 3 #include "string" 4 using namespace std; 5 class TDate{ //定义TDate 6 protected: 7 int Year, Month, Day; 8 public: 9 TDate() 10 { 11 } 12 TDate(int year, int month, int day) 13 { 14 Year = year; 15 Month = month; 16 Day = day; 17 } 18 void Display() 19 { 20 cout << "出生年份:" << Year << endl; 21 cout << "出生月份:" << Month << endl; 22 cout << "出生日期:" << Day << endl; 23 } 24 25 }; 26 class Person :public TDate{ 27 protected: 28 int ID; 29 static int Count; 30 string Name, Sex; 31 public: 32 Person() 33 { 34 } 35 Person(int id, string name, string sex, int year, int month, int day) :TDate(year, month, day) 36 { 37 //ID=id; 38 Name = name; 39 Sex = sex; 40 } 41 virtual void Display() 42 { 43 cout << "姓名:" << Name << endl; 44 cout << "性别:" << Sex << endl; 45 TDate::Display(); 46 } 47 }; 48 int Person::Count = 0; 49 class Teacher :public Person{ 50 protected: 51 string Title; 52 string Dept; 53 public: 54 Teacher() 55 { 56 Count++; 57 ID = Count; 58 } 59 Teacher(string title, string dept, int id, string name, string sex, int year, int month, int day) :Person(id, name, sex, year, month, day) 60 { 61 Count++; 62 ID = Count; 63 Title = title; 64 Dept = dept; 65 } 66 virtual void Display() 67 { 68 cout << "----------老师信息----------" << endl; 69 cout << "编号:" << ID << endl; 70 cout << "姓名:" << Name << endl; 71 cout << "性别:" << Sex << endl; 72 cout << "出生日期:" << Year << "年" << Month << "月" << Day << "日" << endl; 73 cout << "职称:" << Title << endl; 74 cout << "所在系:" << Dept << endl; 75 } 76 77 }; 78 class Student :public Person{ 79 protected: 80 string Major, Grade; 81 public: 82 Student() //定义无参构造函数,因为主函数里要之声明一个类,但是在之后才赋值。 83 { 84 Count++; 85 ID = Count; 86 } 87 Student(int id, string name, string sex, int year, int month, int day, string major, string grade) :Person(id, name, sex, year, month, day) 88 { 89 Count++; 90 ID = Count; 91 Major = major; 92 Grade = grade; 93 } 94 virtual void Display() 95 { 96 cout << "----------学生信息----------" << endl; 97 cout << "编号:" << ID << endl; 98 cout << "姓名:" << Name << endl; 99 cout << "性别:" << Sex << endl; 100 cout << "出生日期:" << Year << "年" << Month << "月" << Day << "日" << endl; 101 cout << "专业:" << Major << endl; 102 cout << "年级:" << Grade << endl; 103 } 104 virtual void SetValue() 105 { 106 //cout<<"---------请输入学生信息---------"<<endl; 107 cout << "姓名:"; 108 cin >> Name; 109 cout << "性别:"; 110 cin >> Sex; 111 cout << "出生年份:"; 112 cin >> Year; 113 cout << "出生月份:"; 114 cin >> Month; 115 cout << "出生日期:"; 116 cin >> Day; 117 cout << "年级:"; 118 cin >> Grade; 119 cout << "专业:"; 120 cin >> Major; 121 cout << endl; 122 } 123 }; 124 int main() 125 { 126 cout << "----------输入学生信息----------" << endl; 127 Teacher teach("讲师", "计算机系", 1, "张山", "男", 1978, 11, 11); 128 Student stu; 129 stu.SetValue(); 130 teach.Display(); 131 stu.Display(); 132 int i; 133 cin >> i; 134 135 }