设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”定义为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class Brithday 6 { 7 private: 8 int year; 9 int moon; 10 int day; 11 public: 12 Brithday(int year, int moon, int day) 13 { 14 this->year = year; 15 this->moon = moon; 16 this->day = day; 17 } 18 ~Brithday() 19 { 20 cout << "Brithday 析构函数输出" << endl; 21 } 22 int getYear() 23 { 24 return year; 25 } 26 int getMoon() 27 { 28 return moon; 29 } 30 int getDay() 31 { 32 return day; 33 } 34 }; 35 class People 36 { 37 private: 38 string number; 39 string sex; 40 Brithday brithday; 41 string id; 42 public: 43 People(string number, string sex, int year,int moon,int day, string id) 44 :brithday(year,moon,day) 45 { 46 this->number = number; 47 this->sex = sex; 48 this->id = id; 49 } 50 People(People & p1) 51 :brithday(p1.brithday.getYear(), p1.brithday.getMoon(), p1.brithday.getDay()) 52 { 53 this->number = p1.number; 54 this->sex = p1.sex; 55 this->id = p1.id; 56 } 57 ~People() 58 { 59 cout << "People 成员析构函数输出" << endl; 60 } 61 void display(); 62 }; 63 inline void People::display() 64 { 65 cout << "Peolpe 成员数据输出:" << endl; 66 cout << number << " " << sex << " " << brithday.getYear() << " 年" << brithday.getMoon()<< "月" << brithday.getDay() << "日 " << id << endl; 67 } 68 69 int main() 70 { 71 People p1("159074277", "男", 1997, 2, 4, "452316735216753251"); 72 p1.display(); 73 People p2(p1); 74 p2.display(); 75 system("pause"); 76 return 0; 77 }
实验中People(人员)类。具有的属性如下:姓名char name[11]、 编号char number[7]、性别 char sex[3]、生日 birthday、身份证号 char id[16]。其中“出生日期”定义为一个“日期”类内嵌对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函、拷贝构造函数、内联成员函数、聚集。在测试程序中定义 people类的对象数组,录入数据并显示。
//个人认为在C++中使用string比char方便,所以用的是string
people(人员)类派生出student(学生)类,添加属性:班号char classNo[7];从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。从student类派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate类和teacher类派出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class Brithday 6 { 7 public: 8 int year; 9 int moon; 10 int day; 11 public: 12 Brithday(int year, int moon, int day) 13 { 14 this->year = year; 15 this->moon = moon; 16 this->day = day; 17 } 18 ~Brithday() 19 { 20 cout << "Brithday 析构函数输出" << endl; 21 } 22 int getYear() 23 { 24 return year; 25 } 26 int getMoon() 27 { 28 return moon; 29 } 30 int getDay() 31 { 32 return day; 33 } 34 }; 35 36 class People 37 { 38 public: 39 string number; 40 string sex; 41 Brithday brithday; 42 string id; 43 public: 44 People(string number, string sex, int year, int moon, int day, string id) 45 :brithday(year, moon, day) 46 { 47 this->number = number; 48 this->sex = sex; 49 this->id = id; 50 cout << "People 构造函数调用成功" << endl; 51 } 52 People(People & p1) 53 :brithday(p1.brithday.getYear(), p1.brithday.getMoon(), p1.brithday.getDay()) 54 { 55 this->number = p1.number; 56 this->sex = p1.sex; 57 this->id = p1.id; 58 cout << "People 拷贝构造函数调用成功" << endl; 59 } 60 ~People() 61 { 62 cout << "People 成员析构函数输出" << endl; 63 } 64 void display(); 65 }; 66 inline void People::display() 67 { 68 cout << "成员数据输出:" << endl; 69 cout << " "<<number << " " << sex << " " << brithday.getYear() << "年" << brithday.getMoon() << "月" << brithday.getDay() << "日 " <<"id:"<< id << " "; 70 } 71 72 73 class Student :virtual public People 74 { 75 private: 76 string classNO; 77 public: 78 Student() :People("空", "空", 0000, 0, 0, "空") 79 { 80 classNO = "空"; 81 cout << "Student 缺省构造函数调用成功" << endl; 82 } 83 Student(string number, string sex, int year, int moon, int day, string id, string classno) : 84 People(number, sex, year, moon, day, id){ 85 classNO = classno; 86 cout << "Student 构造函数调用成功" << endl; 87 } 88 Student(People p, string classno) : 89 People(p){ 90 classNO = classno; 91 cout << "Student people类的拷贝构造函数调用成功" << endl; 92 } 93 ~Student() 94 { 95 cout << "Student 析构函数调用成功" << endl; 96 } 97 void display(); 98 }; 99 inline void Student::display() 100 { 101 People::display(); 102 cout << "classNO:" << classNO <<" "; 103 } 104 105 class Teacher :virtual public People 106 { 107 private: 108 string principalship; 109 string department; 110 public: 111 Teacher() :People("空", "空", 0000, 0, 0, "空") 112 { 113 principalship = "空"; 114 department = "空"; 115 cout << "Teacher 缺省构造函数调用成功" << endl; 116 } 117 Teacher(string number, string sex, int year, int moon, int day, string id, string principalship,string department) : 118 People(number, sex, year, moon, day, id){ 119 this->principalship = principalship; 120 this->department = department; 121 cout << "Teacher 构造函数调用成功" << endl; 122 } 123 Teacher(People p, string principalship, string department) : 124 People(p){ 125 this->principalship = principalship; 126 this->department = department; 127 cout << "Teacher people类的拷贝构造函数调用成功" << endl; 128 } 129 ~Teacher() 130 { 131 cout << "Teacher 析构函数调用成功" << endl; 132 } 133 void display(int i); 134 }; 135 inline void Teacher::display(int i = 0) 136 { 137 if (i != 1) 138 People::display(); 139 cout << "principalship:" << principalship << " department:" << department<<" "; 140 } 141 142 class Graduate :public Student 143 { 144 private: 145 string subject; 146 string teacher_adviser; 147 public: 148 Graduate() :Student("空", "空", 0000, 0, 0, "空","空"), 149 People("空", "空", 0000, 0, 0, "空") 150 { 151 subject = "空"; 152 teacher_adviser = "空"; 153 cout << "Graduate 缺省构造函数调用成功" << endl; 154 } 155 Graduate(string number, string sex, int year, int moon, int day, string id, string classno, string subject, string teacher_adviser) : 156 Student(number, sex, year, moon, day, id, classno), People(number, sex, year, moon, day, id) 157 { 158 this->subject = subject; 159 this->teacher_adviser = teacher_adviser; 160 cout << "Graduate 构造函数调用成功" << endl; 161 } 162 Graduate(Student p, string subject, string teacher_adviser) : 163 Student(p),People(p.number, p.sex, p.brithday.year, p.brithday.moon, p.brithday.day, p.id){ 164 this->subject = subject; 165 this->teacher_adviser = teacher_adviser; 166 cout << "Graduate Student 类的拷贝构造函数调用成功" << endl; 167 } 168 ~Graduate() 169 { 170 cout << "Graduate 析构函数调用成功" << endl; 171 } 172 void display(); 173 174 }; 175 inline void Graduate::display() 176 { 177 Student::display(); 178 cout << "subject:" << subject << " teacher_adviser:" << teacher_adviser<<" "; 179 } 180 181 class TA :public Teacher, public Graduate 182 { 183 public: 184 TA() :Graduate("空", "空", 0000, 0, 0, "空", "空","空","空"), 185 Teacher("空", "空", 0000, 0, 0, "空", "空", "空"), 186 People("空", "空", 0000, 0, 0, "空") 187 { 188 cout << "TA 缺省构造函数调用成功" << endl; 189 } 190 TA(string number, string sex, int year, int moon, int day, string id, string classno, string subject, string teacher_adviser, string principalship, string department) : 191 Graduate(number, sex, year, moon, day, id, classno, subject,teacher_adviser), 192 Teacher(number, sex, year, moon, day, id, principalship, department) 193 ,People(number, sex,year,brithday.moon, day, id) 194 { 195 cout << "TA 构造函数调用成功" << endl; 196 } 197 TA(Graduate G,Teacher T) : 198 Graduate(G), Teacher(T), People(G.number, G.sex, G.brithday.year, G.brithday.moon, G.brithday.day, G.id) 199 { 200 cout << "TA Graduate ,Teacher 类的拷贝构造函数调用成功" << endl; 201 } 202 ~TA() 203 { 204 cout << "TA 析构函数调用成功" << endl; 205 } 206 void display(); 207 }; 208 inline void TA::display() 209 { 210 Graduate::display(); 211 Teacher::display(1); 212 } 213 int main() 214 { 215 People p1("159074277", "男", 1997, 2, 4, "452316735216753251"); 216 cout << " People p1"; p1.display(); cout << " "; 217 People p2(p1); 218 cout << " People p2"; p2.display(); cout << " "; 219 Student S1(p1, "1213"); 220 cout << " Student S1"; S1.display(); cout << " "; 221 Teacher T1(p2, "English Teacher", " English"); 222 cout << " Teacher T1"; T1.display(); cout << " "; 223 Teacher T2; 224 Graduate G1(S1, "mach","zhangsan"); 225 cout << " Graduate G1"; G1.display(); cout << " "; 226 TA TA1(G1, T2); 227 cout << " TA TA1"; TA1.display(); cout << " "; 228 system("pause"); 229 return 0; 230 }
People类重载“= =”运算符和“=”运算符,“= =”运算符判断两个People类对象的id属性是否相等;“=”运算符实现People类对象的赋值操作。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class Brithday 6 { 7 private: 8 int year; 9 int moon; 10 int day; 11 public: 12 Brithday(int year, int moon, int day) 13 { 14 this->year = year; 15 this->moon = moon; 16 this->day = day; 17 } 18 ~Brithday() 19 { 20 cout << "Brithday 析构函数输出" << endl; 21 } 22 int getYear() 23 { 24 return year; 25 } 26 int getMoon() 27 { 28 return moon; 29 } 30 int getDay() 31 { 32 return day; 33 } 34 }; 35 class People 36 { 37 private: 38 string number; 39 string sex; 40 Brithday brithday; 41 string id; 42 public: 43 People() 44 :brithday(0000, 00, 00) 45 { 46 this->number = "空"; 47 this->sex = "空"; 48 this->id = "空"; 49 } 50 People(string number, string sex, int year, int moon, int day, string id) 51 :brithday(year, moon, day) 52 { 53 this->number = number; 54 this->sex = sex; 55 this->id = id; 56 } 57 People(People & p1) 58 :brithday(p1.brithday.getYear(), p1.brithday.getMoon(), p1.brithday.getDay()) 59 { 60 this->number = p1.number; 61 this->sex = p1.sex; 62 this->id = p1.id; 63 } 64 ~People() 65 { 66 cout << "People 成员析构函数输出" << endl; 67 } 68 void display(); 69 friend bool operator ==(People &p1, People &p2) 70 { 71 bool k; 72 if (p1.id == p2.id) 73 k = 1; 74 else 75 k = 0; 76 return k; 77 } 78 People& operator =(People &p) 79 { 80 this->brithday = p.brithday; 81 this->number = p.number; 82 this->sex = p.sex; 83 this->id = p.id; 84 return *this; 85 } 86 }; 87 inline void People::display() 88 { 89 cout << "Peolpe 成员数据输出:" << endl; 90 cout << number << " " << sex << " " << brithday.getYear() << " 年" << brithday.getMoon() << "月" << brithday.getDay() << "日 " << id << endl; 91 } 92 93 int main() 94 { 95 People p1("159074277", "男", 1997, 2, 4, "452316735216753251"); 96 p1.display(); 97 People p2("159074275", "女",1996, 12 ,5, "452316735216753251"); 98 p2.display(); 99 if (p1 == p2) 100 cout << "p1 = p2" << endl; 101 p1 = p2; 102 p1.display(); 103 system("pause"); 104 return 0; 105 106 }
*****************************************************************************************************************
编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车类(motorcar),从bicycle类和motorcar类派生出摩托车类(motorcycle),它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试看。程序名:lab8_2.cpp。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 5 class Vehicle 6 { 7 private: 8 double MaxSpeed; 9 double Weight; 10 public: 11 Vehicle(double maxspeed, double weight) 12 { 13 this->MaxSpeed = maxspeed; 14 this->Weight = weight; 15 cout << "Vehicle 构造函数调用成功" << endl; 16 } 17 ~Vehicle() 18 { 19 cout << "Vehicle 析构函数调用成功" << endl; 20 } 21 virtual void Run() 22 { 23 cout << "Vehicle Run() 函数调用成功" << endl; 24 } 25 virtual void Stop() 26 { 27 cout << "Vehicle Stop() 函数调用成功" << endl; 28 } 29 }; 30 class Bicycle : virtual public Vehicle 31 { 32 private: 33 double Height; 34 public: 35 Bicycle(double maxspeed, double weight, double height) 36 :Vehicle(maxspeed, weight) 37 { 38 this->Height = height; 39 cout << "Bicycle 构造函数输出" << endl; 40 } 41 ~Bicycle() 42 { 43 cout << "Bicycle 析构函数输出" << endl; 44 } 45 void Run() 46 { 47 cout << "Bicycle Run() 函数调用成功" << endl; 48 } 49 void Stop() 50 { 51 cout << "Bicycle Stop() 函数调用成功" << endl; 52 } 53 54 }; 55 class Motorcar : virtual public Vehicle 56 { 57 private: 58 int SeatNum; 59 public: 60 Motorcar(double maxspeed, double weight, double seatnum) 61 :Vehicle(maxspeed, weight) 62 { 63 this->SeatNum = seatnum; 64 cout << "Motorcar 构造函数输出" << endl; 65 } 66 ~Motorcar() 67 { 68 cout << "Motorcar 析构函数输出" << endl; 69 } 70 //other 函数,待定 71 void Run() 72 { 73 cout << "Motorcar Run() 函数调用成功" << endl; 74 } 75 void Stop() 76 { 77 cout << "Motorcar Stop() 函数调用成功" << endl; 78 } 79 }; 80 81 class Motorcycle :public Motorcar, public Bicycle 82 { 83 public: 84 Motorcycle(double maxspeed, double weight, double height, double seatnum) 85 :Vehicle(maxspeed, weight), Bicycle(maxspeed, weight, height), 86 Motorcar(maxspeed, weight, seatnum) 87 { 88 cout << "Motorcycle 构造函数输出" << endl; 89 } 90 ~Motorcycle() 91 { 92 cout << "Motorcycle 析构函数输出" << endl; 93 } 94 //other 函数,待定 95 void Run() 96 { 97 cout << "Motorcycle Run() 函数调用成功" << endl; 98 } 99 void Stop() 100 { 101 cout << "Motorcycle Stop() 函数调用成功" << endl; 102 } 103 }; 104 105 106 int main(void) 107 { 108 Motorcycle *a = new Motorcycle(1, 2, 3, 4); 109 Vehicle *v; 110 Bicycle *b; 111 Motorcar *m; 112 a->Run(),a->Stop(); 113 v = a; 114 v->Run(),v->Stop(); 115 b = a; 116 b->Run(),b->Stop(); 117 m = a; 118 m->Run(),m->Stop(); 119 delete(a); 120 a = NULL; 121 system("pause"); 122 return 0; 123 }