一、题目:
题目描述
设计雇员Employee类,包含雇员的情况,工号、姓名、工资等级(每月工资数,整型数值)、受雇时间(年、月、日)。编写程序测试Employee类。要求输入任意员工工号,及当前日期(年、月,此日期应晚于受雇时间),输出该员工姓名接应得到的工资总额,中间用空格隔开。
已知当前所有员工信息如下:
1,"wang",5000,2000,10,23
2,"liu",4500,2008,1,20
3,"huo",3800,2003,7,3
4,"ma",5300,2015,4,10
5,"meng",6000,2016,3,16
输入样例
5 2016 5
输出样例
meng 12000
二、示例代码:
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
int num;
string name;
int salary;
int m_year, m_month, m_date;
public:
Employee(int a,string b,int c, int year, int month, int date)
{
num = a;
name = b;
salary = c;
m_year = year, m_month = month, m_date = date;
}
int searchSalary();
int searchYear();
int searchMonth();
void printName();
};
int Employee::searchSalary()
{
return salary;
}
int Employee::searchYear()
{
return m_year;
}
int Employee::searchMonth()
{
return m_month;
}
void Employee::printName()
{
cout << name;
}
int main()
{
int n, year, month, f_year,f_month,salary,rate;
Employee people[5] = { Employee(1,"wang",5000,2000,10,23),Employee(2,"liu",4500,2008,1,20) ,
Employee(3,"huo",3800,2003,7,3) ,Employee(4,"ma",5300,2015,4,10) ,
Employee(5,"meng",6000,2016,3,16) };
cin >> n >> year >> month;
f_year = people[n-1].searchYear();
f_month = people[n-1].searchMonth();
salary = people[n-1].searchSalary();
if (year == f_year)
{
rate = month - f_month;
}
else
{
rate = (year - 1 - f_year) * 12 + month+(12-f_month);
}
people[n-1].printName();
cout << " " << salary*rate;
system("pause");
return 0;
}
三、易错提醒:
此题主要还是考察的是类的定义和求解一些问题,在编程中遇到最大的问题就是字符串如何输入输出。本题用string明显是一个很好的解决方法,但是此时头文件include要写<string>而不是<string.h>否则会报错。其实也可以直接把name定义为public,但是那样就失去了思考的价值,除了像以上那样直接用一个cout的函数表示,其实也可以用字符串的赋值来解决问题,那样就更有思考价值!下面给出我的一些解决方法。
string直接赋值:
string st1;
st1 = searchName();//需要定义searchNmae函数,返回值为string类型
利用指针:
因为字符数组输入输出都很简单,所以将其全部改为字符数组,但是需要运用指针函数,即返回值是一个地址的函数。
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
int num;
char*name;//将名字命名为指针,也好修改
int salary;
int m_year, m_month, m_date;
public:
Employee(int a,char*p,int c, int year, int month, int date)
{
num = a;
name = p;
salary = c;
m_year = year, m_month = month, m_date = date;
}
int searchSalary();
int searchYear();
int searchMonth();
char* printName();
};
int Employee::searchSalary()
{
return salary;
}
int Employee::searchYear()
{
return m_year;
}
int Employee::searchMonth()
{
return m_month;
}
char* Employee::printName()
{
return name;
}//用的是一个指针函数,返回值是一个地址
int main()
{
int n, year, month, f_year,f_month,salary,rate;
Employee people[5] = { Employee(1,"wang",5000,2000,10,23),Employee(2,"liu",4500,2008,1,20) ,
Employee(3,"huo",3800,2003,7,3) ,Employee(4,"ma",5300,2015,4,10) ,
Employee(5,"meng",6000,2016,3,16) };
cin >> n >> year >> month;
f_year = people[n-1].searchYear();
f_month = people[n-1].searchMonth();
salary = people[n-1].searchSalary();
if (year == f_year)
{
rate = month - f_month;
}
else
{
rate = (year - 1 - f_year) * 12 + month+(12-f_month);
}
char*m=people[n-1].printName();//把里面的name赋给外面的指针
cout << m<<" " << salary*rate;
system("pause");
return 0;
}
代码中注释的即为修改的地方。
利用string库中的复制函数strcpy:
此处也只是给出局部代码:
void copy()
{
Length = strlen(str);
Buffer = new char[Length + 1];
strcpy(Buffer, str);
}
需要自己写一个copy函数,然后用动态内存创建一个Buffer,这个Buffer在类中应该先有一个定义,定义为字符指针。然后在cout结束后把Buffer释放掉。