#include<iostream>
#include<cstring>
using namespace std;
class CHuman{
private:
char* mName;
int mAge;
public:
CHuman(){
mAge = 0;
}
~CHuman(){
mAge = 0;
//delete[] mName; // 請問這個delete[] 是成功運作的嗎? //delete之後結果會出問題,程式會直接結束
}
void SetName(const char* name){
mName = new char[strlen(name)+1];
strcpy(mName, name);
}
char* GetName(){
return mName;
}
void SetAge(int age){
mAge = age;
}
int GetAge(){
return mAge;
}
};
class CEmployee: public CHuman{
private:
char* mId;
public:
CEmployee(){
}
~CEmployee(){
}
void SetId(const char* id){
mId = new char[strlen(id)+1];
strcpy(mId, id);
}
char* GetId(){
return mId;
}
};
class CEmployees{
private:
CEmployee* mEmployees;
int count;
public:
CEmployees(){
count = 0;
mEmployees = new CEmployee[count+1];//請問這行可以省略嗎 如果不在建構的時候就設置空間 是不是就不能成功Add第一筆
}
~CEmployees(){
delete[] mEmployees;
}
void Add(const CEmployee Employee){
CEmployee* NewEmployees;
NewEmployees = new CEmployee[count+1];
for(int index=0; index<count; ++index){
NewEmployees[index] = mEmployees[count];
}
delete[] mEmployees;
mEmployees = new CEmployee[count+1];
mEmployees = NewEmployees;
mEmployees[count] = Employee;
count++;
}
CEmployee Get(char* id){
for(int index=0; index<count; ++index){
if(strcmp(id, mEmployees[index].GetId()) == 0)
return mEmployees[index];
}
return CEmployee();
}
};
int main(){
CEmployee Employee;
CEmployees Employees;
int act;
while(1){
cout<<"1.新增員工\n2.查詢員工\n請輸入數字: ";
cin>>act;
if(act!=3){
switch(act){
case 1:{
char* name = new char[1];
char* id = new char[1];
int age = 0;
cout<<"請輸入資料!\n姓名: ";
cin>>name;
Employee.SetName(name);
cout<<"年齡: ";
cin>>age;
Employee.SetAge(age);
cout<<"員工ID: ";
cin>>id;
Employee.SetId(id);
Employees.Add(Employee);
cout<<"Finish !\n";
break;
}
case 2:{
char* id = new char[1];
cout<<"請輸入要查詢的員工ID: ";
cin>>id;
cout<<"姓名\t年齡\t員工ID\n";
cout<<Employees.Get(id).GetName()<<"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