#include <string>
#include <iostream>
using namespace std;
//-----------------------------------------------------------------//
//-------------------------class employee--------------------------//
//-----------------------------------------------------------------//
class Employee
{
private: string empName;
int empNum;
string hireDate;
public:
Employee():empName(""),empNum(0), hireDate("") //default ctor
{}
Employee(string name, int num, string date)
{
empName = name;
empNum = num;
hireDate = date;
}
void setempName(string n);
void setempNum(int nm);
void setHiredate(string d);
string getName();
int getNum();
string getDate();
void print();
};
void Employee::setempName(string n)
{empName = n ;}
void Employee::setempNum(int nm)
{empNum = nm;}
void Employee::setHiredate(string d)
{hireDate = d;}
string Employee::getName()
{return empName;}
int Employee::getNum()
{return empNum;}
string Employee::getDate()
{return hireDate;}
//-----------------------------------------------------------------//
//--------------------class production worker----------------------//
//-----------------------------------------------------------------//
class ProductionWorker : public Employee
{
private:
int shift;
double hrlyPay;
public:
ProductionWorker():shift(0) , hrlyPay(0.0)
{}
ProductionWorker(int sh , double pay)
{
shift = sh;
hrlyPay = pay;
}
void setshift(int s);
void setPay(double p);
int getshift();
double getPay();
void print();
};
void ProductionWorker::print()
{
cout << "Employee Name: " << getName() << endl;
cout << "Employee Number: " << getNum() << endl;
cout << "Hire Date: " << getDate() << endl;
cout << "Shift: " << getshift();
if(shift == 1)
{
cout << "(Day Shift)" << endl;}
else
cout << "(Night Shift)" << endl;
cout << "Pay Rate: $" << getPay()<< endl;
}
void ProductionWorker::setshift(int sh) //
{sh = shift;}
void ProductionWorker::setPay(double p)
{p = hrlyPay;}
int ProductionWorker::getshift()
{return shift;}
double ProductionWorker::getPay()
{return hrlyPay;}
//-----------------------------------------------------------------//
//-------------------------Main------------------------------------//
//-----------------------------------------------------------------//
int main()
{
int Shift;
double pay;
cout << "Enter 1 for Day Shift or 2 for Night Shift: "<<endl;
cout<< "Any deviation will default to Night Shift ";
cin >> Shift;
cout << "Enter hourly pay: $";
cin >> pay;
ProductionWorker emp1(Shift, pay);
emp1.setempName("Pedro, Colon");
emp1.setempNum(8675309);
emp1.setHiredate("1-1-2000");
emp1.print();
return 0;
}
当我把所有东西都放在一个主函数中时,我的代码就可以了。但是,当我尝试将类分成自己的定义和实现文件并将main分隔到自己的文件中时。我的代码不起作用。我的代码有什么问题吗?请帮助我,我只是c ++的初学者
问题: 我需要将它们分成1个主要功能,2个定义和2个实现文件
答案 0 :(得分:3)
您的主要包含using namespace std;
。这使得std
命名空间的元素(例如std::string
)可以不受限制地使用(即string
)。
您的标头文件不包含此内容,因此您必须对该类型进行限定。请不要将using指令添加到头文件中;这样做被认为是错误的样式,因为您强制标题的用户在全局名称空间中包含所有std
符号。
对于评论中提到的tobi,您还应该在需要的地方添加所需的标题文件,例如:在employee.h
中,您有一个std::string
类型的字段,因此您应该在该文件中#include <string>
。