本文主要是介绍一道关于OO面试题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
腾讯每天上班有员工和主管签到。主管有权限查看签到情况,可以获取的信息有:总共有多少人签到,按照员工的id或者签到顺序查看员工的签到情况,得到倒数n个签到的员工。
先写了下面这些,有时间再补充:
#include <iostream>
#include <string>
using namespace std;#define MAX 100
class Employee;
Employee *allEmployee[MAX]; //所有员工//class SignOperator;
class Singleton{
public:friend class Charge;static Singleton* GetInstance(){if(instance==NULL) instance=new Singleton();return instance;}void IncreaseNum(){++signNum;}void AddSignEmployee(int id){signEmployee[signNum]=allEmployee[signNum];IncreaseNum();}void Delete(){if(instance) delete instance;instance=NULL;}
private:static int signNum;Employee *signEmployee[MAX];Singleton(){}static Singleton *instance;int GetNum(){return signNum;}};Singleton *Singleton::instance=NULL;
int Singleton::signNum=0;class Employee{
protected:int ID;bool isSign;string signTime;Singleton *myInstance;
public:Employee(int id=0):ID(id){myInstance=Singleton::GetInstance();}~Employee(){}void SetID(int id){ID=id;}void Clear(){isSign=false;signTime="NULL";}int GetID(){return ID;}void Sign(string time){isSign=true;signTime=time;myInstance->AddSignEmployee(ID);}bool IsSign(){return isSign;}string GetSignTime(){return signTime;}virtual void GetSignNum(){cout<<"You have no right to get the message"<<endl;}
};class Charge:public Employee{
private:int ID;
public:Charge(int id=0):ID(id){}void GetSignNum(){cout<<"共有 "<<myInstance->GetNum()<<" 人签到"<<endl;}
};int main()
{for(int i=0;i<80;i++) allEmployee[i]=new Employee(i+1);for(int i=80;i<MAX;i++)allEmployee[i]=new Charge(i+1);allEmployee[1]->Sign("08:00");allEmployee[5]->Sign("08:05");allEmployee[5]->Sign("07:55");allEmployee[80]->Sign("08::00");allEmployee[80]->GetSignNum();return 0;
}
这篇关于一道关于OO面试题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!