运用继承关系以及成员对象函数来编写一个有关健康信息的系统
代码如下:
#include <iostream> #include<cstring> using namespace std; class IDCard {//个人信息 private: string userName;//姓名 string cardNO;//身份证号码 double expirationDate;//证件的有效期 public: IDCard() {cout << "userName: "; cin >> userName; cout << "cardNO: "; cin >> cardNO; cout << "expirationDate: "; cin >> expirationDate;} void getname() { cout << userName; } }; class JianKangMa :public IDCard { private: string phoneNO;//电话号码 char healthInfo[30];//健康信息 public: JianKangMa() :IDCard() { cout << "phoneNO: "; cin >> phoneNO; cout << "healthInfo: "; cin >> healthInfo; } char* getHealthInfo() { return healthInfo; }//输出信息 void setHealthInfo(char* h) { strcpy_s(healthInfo, h); }//修改信息 int isWarning() { return (strcmp(healthInfo, "Warning") == 0); }//检查是否有异常 }; class JianKangMaManager {//管理健康信息 private: JianKangMa lb[2]; public: JianKangMaManager() :lb() {} void getWarningUsers() { bool st= 1; cout << "健康信息异常的人的姓名: " << endl; for (int i = 0; i < 2; i++) {//检查是否异常 if (lb[i].isWarning()) { lb[i].getname(); cout << endl; st = 0; } } if (st) { cout << "没有异常" << endl; } } }; int main() { JianKangMaManager person; person.getWarningUsers(); return 0; }