注册 登录
编程论坛 C++教室

求做一道C++题目,请大家帮忙

Clytie 发布于 2015-12-10 20:18, 2252 次点击
设计一个雇员类Employee,存储雇员的编号,姓名和生日等信息,要求该类使用日期类作为成员对象,雇员类的使用如下:
//定义一个雇员,其雇员号为10,生日为1980年11月20日
Employee Tom("Tom", 10, 1980, 11, 20);
Date today(1980, 11, 20);
if(Tom.IsBirthday(today))    //判断今天是不是Tom的生日
5 回复
#2
TonyDeng2015-12-10 22:18
你遇到什么问题了?
#3
阿文fire2015-12-13 11:20
你需要设计两个类,第一个类为日期类Date,对应构造函数的参数应该为年、月、日;第二个类为雇员类Employee,构造函数为名字、雇员号、年、月、日,还需要一个公有函数IsBirthday,它把Date类的对象作为参数
#4
wengbin2015-12-15 10:23
程序代码:
#include<string>
#include<iostream>
using namespace std;
class Date;
class Employee;

class Date
{
private:
    int year,month,day;
public:
    Date();
    Date(int,int,int);
    ~Date();
    friend class Employee;
};
///////////////////////////////////
Date::Date()
{
    cout<<"the year:";
    cin>>year;
    cout<<"the month:";
    cin>>month;
    cout<<"the day:";
    cin>>day;
}
Date::Date(int year_,int month_,int day_):year(year_),month(month_),day(day_)
{

}
Date::~Date(){};
///////////////////////////////////
class Employee
{
private:
    int no,birth_y,birth_m,birth_d;
    string name;
public:
    Employee(string,int,int,int,int);
    ~Employee();
    bool IsBirthday(Date);
};
///////////////////////////////////////
Employee::Employee(string name_,int no_,int birth_y_,int birth_m_,int birth_d_):\
name(name_),birth_y(birth_y_),birth_m(birth_m_),birth_d(birth_d_)
{

}
Employee::~Employee(){};
bool Employee::IsBirthday(Date today)
{
    if(today.month==birth_m&&today.day==birth_d)return true;
    else return false;
}
///////////////////////////////////////
int main()
{
    Employee Tom("Tom",1,1991,5,20);
    Date today(2015,5,22);
    if(Tom.IsBirthday(today))cout<<"happy birthday!\n";
    else cout<<"not the day today\n";
    return 0;
}
#5
诸葛欧阳2015-12-15 22:36
不要再类里进行输入,把变量做为参数传进去
#6
wengbin2015-12-16 10:05
回复 5楼 诸葛欧阳
嗯嗯,谢版主教诲,手贱,多写了个构造函数
1