結合C++特性(類模板/函數模板,STL的list)實現簡單員工表示例

個人cpp作業練習,綜合構造函數、析構函數、重載運算符等寫一個員工類和員工表類,可借鑑思路,禁止照搬

1.原始版本

#include <iostream>
#include <string>
using namespace std;

class Employee{
	public :
		Employee(){}
		//重載<<便於顯示輸出員工信息
        Employee(int id,string name,string sex,int age,string department):
		id(id),name(name),sex(sex),age(age),department(department){}
		Employee(const Employee& employee){
            this->id = employee.id;
            this->name = employee.name;
            this->sex = employee.sex;
            this->age = employee.age;
            this->sex = employee.sex;
            this->department = employee.department;
        }
		friend ostream & operator<<( ostream & os,const Employee & employee);
		void setName(string name){this->name = name;}
		string getName() {return this->name;}
		void setSex(string sex){this->sex = sex;}
		string getSex(){return this->sex;}
		void setDepartment(string department){this->department = department;}
		string getDepartment() {return this->department;}
		void setAge(int age){this->age = age;}
		int getAge() {return this->age;}
		void setId(int id){this->id = id;}
		int getId() {return this->id;}
private :
        int id;
		string name;
		string sex;
		int age;
		string department;
};

ostream & operator<<( ostream & os,const Employee & employee){
    os<<"id="<<employee.id<<" name="<<employee.name<<" sex="<<employee.sex<<" age="<<employee.age
	<<" department="<<employee.department<<endl;
    return os;
}

class EmployeeList{
public:
		//動態數組
		EmployeeList(){}
		EmployeeList(int size):size(size){list = new Employee[size];}
		~EmployeeList() {delete[] list;}
		void add(Employee employee) {//添加員工
			if (count>size) {cout<<"員工已滿"<<endl;return;}
			list[count] = employee; //調用Employee類拷貝構造
			count++;
		}
		int query_num(){//查詢員工人數
			return count;
		}
		void show_id(int id){ //通過id號查詢顯示員工信息
			if (id<=0||id>count) {cout<<"員工不存在!"<<endl;}
			else cout<<list[id-1];
		} 
		void dismiss(int id){ //通過id刪除某員工信息
			if(id<=0||id>count) {cout<<"員工不存在!"<<endl;}
			else {
				for(int i=id;i<count;i++){
					list[i-1]=list[i];
				}
				count--;
			}
		}
		void update(int id,Employee &ee){ //通過id更新員工信息
			list[id-1] = ee;
		}
private:
		Employee *list; //員工表 
		int count=0;//當前員工數量
		int size=0;//可容納員工數量
};
int main(){
	EmployeeList list(100);//創建一個最大容量爲100的員工表
	Employee employee1(1,"林浩","男",18,"開發部");
	Employee employee2(2,"文雅","女",18,"銷售部");
	Employee employee3(1,"李強","男",20,"開發部");
	Employee tempEmployee = employee3;//拷貝employee3員工信息
	list.add(employee1);
	list.add(employee2);
	list.add(employee3);
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(2);//查詢employee2員工信息
	list.dismiss(2);//解僱employee2員工
	cout<<"刪除後..."<<endl;
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(1);//查詢employee1員工信息
	list.update(1,tempEmployee);//更新employee1員工信息
	cout<<"更新後..."<<endl;
	list.show_id(1);//查詢employee1員工信息
	return 0;
}

程序輸出:
在這裏插入圖片描述
2.類模板/函數模板 (用T替換員工表類的員工類數組)

#include <iostream>
#include <string>
using namespace std;

class Employee{
	public :
		Employee(){}
		//重載<<便於顯示輸出員工信息
        Employee(int id,string name,string sex,int age,string department):
		id(id),name(name),sex(sex),age(age),department(department){}
		Employee(const Employee& employee){
            this->id = employee.id;
            this->name = employee.name;
            this->sex = employee.sex;
            this->age = employee.age;
            this->sex = employee.sex;
            this->department = employee.department;
        }
		friend ostream & operator<<( ostream & os,const Employee & employee);
		void setName(string name){this->name = name;}
		string getName() {return this->name;}
		void setSex(string sex){this->sex = sex;}
		string getSex(){return this->sex;}
		void setDepartment(string department){this->department = department;}
		string getDepartment() {return this->department;}
		void setAge(int age){this->age = age;}
		int getAge() {return this->age;}
		void setId(int id){this->id = id;}
		int getId() {return this->id;}
private :
        int id;
		string name;
		string sex;
		int age;
		string department;
};

ostream & operator<<( ostream & os,const Employee & employee){
    os<<"id="<<employee.id<<" name="<<employee.name<<" sex="<<employee.sex<<" age="<<employee.age
	<<" department="<<employee.department<<endl;
    return os;
}

template <class T>
class EmployeeList{
public:
		//動態數組
		EmployeeList(){}
		EmployeeList(int size):size(size){list = new T[size];}
		~EmployeeList() {delete[] list;}
		void add(T x);
        int query_num();
		void show_id(int id);
		void dismiss(int id); //通過id刪除某成員信息
		void update(int id,T& x);
private:
		T* list; //成員表 
		int count=0;//當前成員數量
		int size=0;//可容納成員數量
};

template <class T>
void EmployeeList<T>::add(T x) {
	if (count>size) {cout<<"成員已滿"<<endl;return;}
	list[count] = x; //如果T是類,則調用拷貝構造
	count++;
}

template<class T>
int EmployeeList<T> ::query_num(){//查詢數量
	return count;
}

template <class T>
void EmployeeList<T>::show_id(int id){
    //通過id號查詢顯示成員信息
	if (id<=0||id>count) {cout<<"員工不存在!"<<endl;}
	else cout<<list[id-1];		
}

template <class T>
void EmployeeList<T>::dismiss(int id){
if(id<=0||id>count) {cout<<"員工不存在!"<<endl;}
	else {
		for(int i=id;i<count;i++){
			list[i-1]=list[i];
		}
		count--;
	}
}

template <class T>
void EmployeeList<T>::update(int id,T& x){
//通過id更新成員信息
	list[id-1] = x;
}

int main(){
	EmployeeList<Employee> list(100);//創建一個最大容量爲100的員工表
	Employee employee1(1,"林浩","男",18,"開發部");
	Employee employee2(2,"文雅","女",18,"銷售部");
	Employee employee3(1,"李強","男",20,"開發部");
	Employee tempEmployee = employee3;//拷貝employee3員工信息
	list.add(employee1);
	list.add(employee2);
	list.add(employee3);
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(2);//查詢employee2員工信息
	list.dismiss(2);//解僱employee2員工
	cout<<"刪除後..."<<endl;
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(1);//查詢employee1員工信息
	list.update(1,tempEmployee);//更新employee1員工信息
	cout<<"更新後..."<<endl;
	list.show_id(1);//查詢employee1員工信息
	return 0;
}

程序輸出如上

3.STL list<> (替換員工表類的員工類數組)

#include <iostream>
#include <string>
#include <list>
using namespace std;

class Employee{
	public :
		Employee(){}
		//重載<<便於顯示輸出員工信息
        Employee(int id,string name,string sex,int age,string department):
		id(id),name(name),sex(sex),age(age),department(department){}
		Employee(const Employee& employee){
            this->id = employee.id;
            this->name = employee.name;
            this->sex = employee.sex;
            this->age = employee.age;
            this->sex = employee.sex;
            this->department = employee.department;
        }
		friend ostream & operator<<( ostream & os,const Employee & employee);
		void setName(string name){this->name = name;}
		string getName() {return this->name;}
		void setSex(string sex){this->sex = sex;}
		string getSex(){return this->sex;}
		void setDepartment(string department){this->department = department;}
		string getDepartment() {return this->department;}
		void setAge(int age){this->age = age;}
		int getAge() {return this->age;}
		void setId(int id){this->id = id;}
		int getId() {return this->id;}
private :
        int id;
		string name;
		string sex;
		int age;
		string department;
};

ostream & operator<<( ostream & os,const Employee & employee){
    os<<"id="<<employee.id<<" name="<<employee.name<<" sex="<<employee.sex<<" age="<<employee.age
	<<" department="<<employee.department<<endl;
    return os;
}

class EmployeeList{
public:
		//動態數組
		EmployeeList(){}
		EmployeeList(int size){eList = list<Employee>(size);}
		~EmployeeList() {}
		void add(Employee employee) {//添加員工
			eList.push_back(employee);
		}
		int query_num(){//查詢員工人數
			return eList.size();
		}
		void show_id(int id){ //通過id號查詢顯示員工信息
			if (id<=0||id>eList.size()) {cout<<"員工不存在!"<<endl;}
			else {
				int i=0;
				list<Employee>::iterator it=eList.begin();
				for(it;i<id;it++,i++){
					if(i==id-1)
					cout<<*(it); //重載Employee的<<進行輸出
				}	
			}
				
		} 
		void dismiss(int id){ //通過id刪除某員工信息
			if(id<=0||id>eList.size()) {cout<<"員工不存在!"<<endl;}
			else {
				int i = 0;
				list<Employee>::iterator it=eList.begin();
				for(it;i<id;it++,i++){
					if(i==id-1)
					eList.erase(it);
				}
			}
		}
		void update(int id,Employee &ee){ //通過id更新員工信息
				int i=0;
				list<Employee>::iterator it=eList.begin();
				for(it;i<id;it++,i++){
					if(i == id-1){
					eList.insert(it,ee);
					eList.erase(it);
					}
				}
		}
private:
		list<Employee> eList; //員工表 
};
int main(){
	EmployeeList list(0);//創建空員工表
	Employee employee1(1,"林浩","男",18,"開發部");
	Employee employee2(2,"文雅","女",18,"銷售部");
	Employee employee3(1,"李強","男",20,"開發部");
	Employee tempEmployee = employee3;//拷貝employee3員工信息
	list.add(employee1);
	list.add(employee2);
	list.add(employee3);
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(2);//查詢employee2員工信息
	list.dismiss(2);//解僱employee2員工
	cout<<"刪除後..."<<endl;
	cout<<"現有員工數量:"<<list.query_num()<<endl;
	list.show_id(1);//查詢employee1員工信息
	list.update(1,tempEmployee);//更新employee1員工信息
	cout<<"更新後..."<<endl;
	list.show_id(1);//查詢employee1員工信息
	return 0;
}

程序輸出如上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章