C++实现简单的链表管理员工
文章开始把我喜欢的这句话送个大家:这个世界上还有什么比自己写的代码运行在十万人的电脑上更酷的事情吗,如果有那就是让这个数字再扩大十倍。
简单的链表增删改查
这里是staff.h
#ifndef STAFF_H_INCLUDED
#define STAFF_H_INCLUDED
using namespace std;
class staff
{
protected:
int id;
char *name;
public:
staff();
staff(int ,char *);
virtual void show(){cout<<"编号:"<<id<<" 姓名:"<<name;}
};
class manager:public staff
{
protected:
double salary;
public:
manager(int id,char *name):staff(id,name){};
double getsalary(){salary=8000;return salary;};
void show(){cout<<"经理 "<<endl;
staff::show();
cout<<" 月薪:"<<getsalary()<<endl;};
};
class technician:public staff
{
private:
int hour;
double salary;
public:
technician(int id,char *name,int hour):staff(id,name)
{this->hour=hour;};
double getsalary()
{return 100*hour;};
void show(){cout<<"兼职技术人员 "<<endl;
staff::show();
cout<<" 工作时长:"<<hour<<" 月薪:"<<getsalary()<<endl;};
};
class salesman:public staff
{
private:
double amount;
double salary;
public:
salesman(int id,char *name,double amount):staff(id,name)
{this->amount=amount;};
double getsalary()
{return amount*0.04;};
void show(){cout<<"销售员 "<<endl;
staff::show();
cout<<" 当月销售额:"<<amount<<" 月薪"<<getsalary()<<endl;};
};
class sales_manager:public staff
{
private:
double amount;
double salary;
public:
sales_manager(int id,char *name,double amount):staff(id,name)
{this->amount=amount;};
double getsalary()
{return 5000+amount*0.005;};
void show(){cout<<"销售经理 "<<endl;
staff::show();
cout<<" 管辖部门销售额:"<<amount<<" 月薪"<<getsalary()<<endl;};
};
//typedef staff* Item;
class yuangong
{
private:
struct node
{
staff * item;//staff 类型的指针
struct node *next;
};
static int num;
node * head;
node *rear;
public:
yuangong(){head=NULL;}///必须自己定义不能用系统默认的因为需要head=NULL;
void add(staff* );
//void delet(Item );
void print();
};
下面是staff.cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include"staff.h"
using namespace std;
staff::staff()
{
this->id=0;
name=new char[4];
name="员工";
}
staff::staff(int id,char *name)
{
this->id=id;
if(name)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}
}
int yuangong::num=0;
void yuangong::add(staff* it)
{
node *add=new node;
add->item=it;
add->next=NULL;
num++;
if(head==NULL)
head=add;
else rear->next=add;
rear=add;
}
/*void delet(Item it)
{
node *temp;
temp->next=it->next;
}*/
void yuangong::print()
{
while(head!=NULL)
{
head->item->show();
head=head->next;
}
}
这里就是main.cpp 了
#include <iostream>
#include <cstring>
#include <cstdlib>
#include"staff.h"
using namespace std;
int main()
{
manager p(1,"靳东男神");
staff *p1=&p;
technician a(2,"老胡",50);
staff *p2=&a;
salesman t(3,"帅雷雷",80000);
staff *p3=&t;
sales_manager x(4,"华仔",800000);
staff *p4=&x;
yuangong yuan;
yuan.add(p1);
yuan.add(p2);
yuan.add(p3);
yuan.add(p4);
yuan.print();
return 0;
}
加油吧,程序员!!!