C++基础题目:
题目:
先设计一个 Employee 类,要求它能保存:
(1)雇员的姓名;
(2)雇员编号,格式为 “XXX-L”,其中X为0-9的数字,L为A~M的字母;(3)受雇日期。
并自行设计相应成员函数。
随后设计一个类 EmployeePay,它继承 Employee 类,能够保存:
(1)月工资,使用float类型,为正数;
(2)部门号,使用整型,为正数。
并自行设计相应成员函数。
//雇员.h及其函数实现
#pragma once
#include <iostream>
#include <regex>
using namespace std;
class Employee
{
public:
string name;
string number;
string date;
bool isnumber(string num)
{
regex pattern("[0-9]{3}-[A-M]");
return regex_match(num, pattern);
}
bool isdate(string date)
{
regex pattern("[0-9]{4}-[0-9]{2}-[0-9]{2}");
return regex_match(date, pattern);
}
void in()
{
cout << "请输入雇员姓名:" << endl;
cin >> name;
cout << "请输入雇员编号(例如:012-A):" << endl;
cin >> number;
if (!isnumber(number))
{
cout << "输出错误的雇员编号!" << endl;
system("pause");
return;
}
cout << "请输入雇员受雇日期(2016-03-25):" << endl;
cin >> date;
if (!isdate(date))
{
cout << "输入错误的受雇日期!" << endl;
system("pause");
return;
}
}
void out()
{
cout << "雇员姓名为:" << name << endl;
cout << "雇员编号为:" << number << endl;
cout << "受雇日期为:" << date << endl;
}
};
//雇员pay.h及其函数实现
#pragma once
#include "Employee.h"
class EmployeePay:public Employee
{
private:
float wage;
int department;
public:
void getdata()
{
cout << "请输入雇员月工资:" << endl;
cin >> wage;
cout << "请输入雇员部门号(例如:16):" << endl;
cin >> department;
}
void outdata()
{
cout << "月工资为:" << wage << "¥" << endl;
cout << "部门号为:" << department << endl;
}
};
//主函数.cpp
#include <iostream>
#include "EmployeePay.h"
int main()
{
EmployeePay a;
a.in();
a.getdata();
a.out();
a.outdata();
system("pause");
return 0;
}