结合C++特性(类模板/函数模板,STL的list)实现简单员工表示例

个人cpp作业练习,综合构造函数、析构函数、重载运算符等写一个员工类和员工表类,可借鉴思路,禁止照搬

1.原始版本

#include 
#include 
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;
}

程序输出:
结合C++特性(类模板/函数模板,STL的list)实现简单员工表示例_第1张图片
2.类模板/函数模板 (用T替换员工表类的员工类数组)

#include 
#include 
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 
#include 
#include 
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;
}

程序输出如上

你可能感兴趣的:(个人日记,c++,面向对象编程,stl,oop)