首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

写的C++程序出现小疑点,帮忙解决下…

2012-02-28 
写的C++程序出现小问题,帮忙解决下……这个是我写的一个小程序,就是分别输入不同的用户名和密码,进入,然后执

写的C++程序出现小问题,帮忙解决下……
这个是我写的一个小程序,就是分别输入不同的用户名和密码,进入,然后执行工作,吃饭,休息这些动作,但是老是报错,如果子类不实例化,子类里没有构造函数和析构函数的话可以运行,但我想用继承继续完善,在主函数中一实例化就报错了,不知道为什么,大家帮忙看看呀~~~谢啦

#include<iostream>
#include<windows.h>
#include<string>
using namespace std;

class Employee
{
public:
int energy;
int salary;
string workno;
string sex;
void enterface();
bool login(string id,string pwd);
void menu();
void work();
void sleep();
void eat();
void check();
void quit();
Employee(int initenergy,int initsalary):energy(initenergy),salary(initsalary){}
~Employee(){}
};

class Scientist:public Employee
{
public:
string workno;
Scientist(string initno):workno(initno){}
~Scientist(){}
};

class Doctor:public Employee
{
public:
string workno;
Doctor(string initno):workno(initno){}
~Doctor(){}
};

class Worker:public Employee
{
public:
string workno;
Worker(string initno):workno(initno){}
~Worker(){}

};

void Employee::enterface()
{
cout<<endl<<endl;
 cout<<"\t\t ____________________________"<<endl;
cout<<"\t\t|============================|"<<endl;
cout<<"\t\t|公司人力资源管理系统 v 1.0.1|"<<endl;
cout<<"\t\t|____________________________|"<<endl;
}

bool Employee::login(string id,string pwd) //***
{
bool judge=false;
system("cls");
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"\t\t+ 欢迎您进入公司人力资源管理系统! +"<<endl;
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<endl;
if(id=="scientist" && pwd=="000")
{
cout<<"\t\t欢迎您:"<<id<<"用户!您的编号:"<<workno<<endl;
judge=true;
}
else if(id=="doctor"&&pwd=="000")
{
cout<<"\t\t欢迎您:"<<id<<"用户!您的编号:"<<workno<<endl;
judge=true;
}
else if(id=="worker" && pwd=="000")
{
cout<<"\t\t欢迎您:"<<id<<"用户!您的编号:"<<workno<<endl;
judge=true;
}
else
{
cout<<"用户名或密码错误!"<<endl;
}
cout<<"\t\t您目前状态优秀(100),工资为无(0),您可以工作了!"<<endl;
return judge;
}

void Employee::menu()
{
cout<<endl<<"\t\t\t 【操作菜单,请选择】"<<endl<<endl;
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"\t\t+--我要工作(w)--我要睡觉(s)--我要吃饭(e)--+"<<endl;
cout<<"\t\t+ +"<<endl;
cout<<"\t\t+-------查询状态(c)------我要退出(q)------+"<<endl;
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++"<<endl;
}

void Employee::work()
{
cout<<"\t\t现在开始工作了!"<<endl;
cout<<"\t\t工作后您的精力将-10,工资将+50."<<endl;
energy-=10;
salary+=50;
if(energy<1)
{
cout<<"\t\t您的精力不足,不能工作!"<<endl;
energy+=10;
salary-=50;
}
cout<<"\t\t您当前的精力为:"<<energy<<" 您当前的薪水为:"<<salary<<endl;
}

void Employee::sleep()
{
cout<<"\t\t接下来开始睡觉咯!"<<endl;
cout<<"\t\t睡完觉,您的精力会+5!"<<endl;
  energy+=5;
if(energy>100)
{
energy=100;
cout<<"\t\t你精力过剩啦,赶紧起床干活吧!"<<endl;
}


cout<<"\t\t睡醒了,已恢复精力!"<<endl;
  cout<<"\t\t现在你的精力值是:"<<energy<<endl;
}

void Employee::eat()
{
cout<<"\t\t接下来开始吃饭咯!"<<endl;
cout<<"\t\t吃完饭,您的精力会+15,并且花去20元!"<<endl;
energy+=15;
salary-=20;
if(salary<0)
{
cout<<"\t\t你没钱啦,快去挣点钱吧!"<<endl;
energy-=15;
salary+=20;
}
if(energy>100)
{
energy=100;
cout<<"\t\t经历都满了还吃,会继续扣钱的哦!"<<endl;
}
cout<<"\t\t现在你的工资是:"<<salary<<" 现在你的精力是:"<<energy<<endl;
}

void Employee::check()
{
  cout<<"\t\t现在你的工资是:"<<salary<<" 现在你的精力是:"<<energy<<endl;
}

void Employee::quit()
{
cout<<"\t\t退出游戏!拜拜!"<<endl;
  exit(0);
}


int main()
{
Employee emp(100,0);
Scientist s("AC01001");
Doctor d("BC01002");
Worker w("DC01003");
emp.enterface();

cout<<endl<<endl<<"\t\t用户名:";
string id;
cin>>id;
cout<<endl<<"\t\t密 码:";
string pwd;
cin>>pwd;
emp.login(id,pwd);
emp.menu();
while(1)//永为真循环,如果不遇见结束语句一直循环
{
char ch;
cout<<"\t\t";
  cin>>ch;

if(ch=='w')//工作
{
emp.work();//调用work函数
}

else if(ch=='s')//睡觉
{
emp.sleep();
}

else if(ch=='e')//吃饭
{
emp.eat();
}

else if(ch=='c')//查询
{
emp.check();
}

else if(ch=='q')//退出
{
emp.quit();
}
}
return 0;

}

提示错误:error C2512: 'Employee' : no appropriate default constructor available

[解决办法]
为Employee增加一个不带参数的构造函数
[解决办法]
加一个Employee();试试
[解决办法]
为Employee提供默认构造函数
[解决办法]
加上默认构造函数

热点排行