gpt4 book ai didi

c++ - 将类分离到它们自己的定义和实现文件中,并将 main 放在它自己的文件中

转载 作者:行者123 更新时间:2023-11-27 23:49:40 25 4
gpt4 key购买 nike

<分区>

#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个实现文件

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com