赞
踩
注意:
//1、注意带参数的构造函数和带默认参数的构造函数的定义与声明
//2、定义employee类的成员变量时,应注意变量类型的声明
//3、在查询时,通过声明成emplyee的友元函数来访问类的成员变量
#include<iostream> #include<string.h> #define N 100 //总的职员个数 using namespace std; //Date类 class Date { public: Date(); Date(int year, int month, int day); ~Date(void); void SetYear(int year); void SetMonth(int month); void SetDay(int day); int GetYear(); int GetMonth(); int GetDay(); private: int year; int month; int day; }; //employee类 class Employee { public: Employee(); Employee( int ,int, int, string, string, char, string); ~Employee(void); void SetInfo(int year, int month, int day, string number, string name, char sex, string position); void Show(); friend int searchInfo(Employee* obj, string index) { if(index==obj->name||index==obj->number){ cout<<"查询到关于"<<index<<"的相关信息如下:"<<endl; cout<<"姓名:"<<obj->name<<endl; cout<<"编号:"<<obj->number<<endl; cout<<"性别:"<<obj->sex<<endl; cout<<"出生日期:"<<obj->birthDay.GetYear()<<"-"<<obj->birthDay.GetMonth()<<"-"<<obj->birthDay.GetDay()<<endl; cout<<"职位:"<<obj->position<<endl; return 1; } else return -1; } private: string number; string name; char sex; Date birthDay; string position; }; Employee::Employee() { cout<<"Employee object is being created by constructor without parameters!"<<endl; } Employee::Employee(int year, int month, int day, string number, string name, char sex, string position):birthDay(year,month,day),number(number),name(name),sex(sex),position(position) { cout<<"Employee object is being created by constructor with parameters!"<<endl; } Employee::~Employee(void) { cout<<"Employee object is being deleted!"<<endl; } void Employee::SetInfo(int year, int month, int day, string number, string name, char sex, string position) { this->birthDay.SetYear(year); this->birthDay.SetMonth(month); this->birthDay.SetDay(day); this->number=number; this->name=name; this->sex=sex; this->position=position; } void Employee::Show() { cout<<"职员"<<this->name<<"的基本信息如下:"<<endl; cout<<"\t编号:"<<this->number<<endl; cout<<"\t性别:"<<this->sex<<endl; cout<<"\t出生日期:"<<this->birthDay.GetYear()<<"-"<<this->birthDay.GetMonth()<<"-"<<this->birthDay.GetDay()<<endl; cout<<"\t职位:"<<this->position<<endl; } Date::Date(void) { cout<<"Date object is being created by constructor without parameters!"<<endl; } Date::Date(int year, int month, int day):year(year),month(month),day(day) { cout<<"Date object is being created by constructor with paramters!"<<endl; } Date::~Date(void) { cout<<"Date object is being deleted!"<<endl; } void Date::SetYear(int year) { this->year=year; } void Date::SetMonth(int month) { this->month=month; } void Date::SetDay(int day) { this->day=day; } int Date::GetYear() { return year; } int Date::GetMonth() { return month; } int Date::GetDay() { return day; } //程序的主函数 int main() { string number; string name; char sex; int year; int month; int day; string position; int input; //存放此次要录入信息的职员的个数 int total=0; //存放已录入信息的职员总数 int choice;//用户选择项 char flag; //判断释放需要修改信息 bool tag=true;//判断是否查询成功 string index; Employee *emp[N]; while(1){ cout<<"*****************************"<<endl; cout<<"* 1、职工信息录入 *"<<endl; cout<<"* 2、职工信息的显示 *"<<endl; cout<<"* 3、查询职工信息 *"<<endl; cout<<"* 0、退出 *"<<endl; cout<<"*****************************"<<endl; cout<<"请输入您的选项:"<<endl; cin>>choice; switch(choice){ case 1: cout<<"请输入您要录取信息的职工个数:"<<endl; cin>>input; for(int i=0;i<input;i++){ cout<<endl<<"请输入职工"<<i+1<<"的信息:"<<endl; cout<<"编号:"; cin>>number; cout<<flush<<"姓名:"; cin>>name; cout<<flush<<"性别(F/M):"; cin>>sex; cout<<flush<<"出生年月日:"; cin>>year>>month>>day; cout<<flush<<"职位:"; cin>>position; emp[total+i]=new Employee(year,month,day,number,name,sex,position); cout<<endl<<"是否需要对职工"<<name<<"的信息进行修改?Y/N"<<endl; cin>>flag; if(flag=='Y'||flag=='y'){ cout<<"请输入修改后职工"<<name<<"的信息:"<<endl; cout<<"编号:"; cin>>number; cout<<flush<<"姓名:"; cin>>name; cout<<flush<<"性别(F/M):"; cin>>sex; cout<<flush<<"出生年月日:"; cin>>year>>month>>day; cout<<flush<<"职位:"; cin>>position; emp[total+i]->SetInfo(year,month,day,number,name,sex,position); cout<<"信息修改成功!"<<endl; } } total=total+input; cout<<endl; break; case 2: cout<<"\n以下是已录入的所有职工的信息,共"<<total<<"个:\n"<<endl; for(int i=0;i<total;i++){ emp[i]->Show(); } cout<<endl; break; case 3: cout<<"\n请输入您要查询的职工名字或者编号:"<<endl; cin>>index; for(int i=0;i<total;i++){ if( searchInfo(emp[i], index)>0 ){ tag=false; continue; } } if(tag){ cout<<"很抱歉,没有查找到相关的信息,也许是信息还未录入!" <<endl; } tag=true; //标志型的数据,在用完之后一定要恢复原来的值! cout<<endl; break; case 0: for(int i=0;i<total;i++){ delete emp[i]; } exit(0); default: cout<<"输入不正确!"<<endl; } } return 0; }
c++构造函数的默认参数
c++动态对象数组的知识总结
c++对象数组中delete 和 delete[] 的区别
c++对象数组调用构造/析构函数的方法
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。