面向对象A面向对象习题(南航皮德常)

更新时间:2024-05-07 03:30:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第8章习题:

8-1、设计Date类,输出合法日期。 #include using namespace std; //类定义// class Date

{ int year; //存储日期的年份 int month;//存储日期的月份 int day;//存储日期的天数 public:

bool setDate(const int,const int,const int); void display(void);

char * getMonth(const int); };

//类的实现// //设置成员变量//

//mm:月份,dd:天数,yy:年份//

//返回值:如果成功赋值则返回true,否则返回false。// char* Date::getMonth(const int m ) { if(m==1)

return \ else if(m==2)

return \ else if(m==3) return \ else if(m==4) return \ else if(m==5) return \ else if(m==6) return \ else if(m==7) return \ else if(m==8)

return \ else if(m==9)

return \ else if(m==10)

return \ else if(m==11)

return \ else

return \}

//设置成员变量//

//参数:mm:月份,dd:天数,yy:年份//

//返回值:如果成功赋值则返回true,否则返回false// bool Date::setDate(const int mm,const int dd,const int yy) { if(mm<1 || mm>12) return false; if(dd<1 || dd>31) return false; year=yy; month=mm; day=dd; return true; }

//在屏幕上显示日期// void Date::display(void)

{ // 按照\的形式输出日期

cout<

cout<

cout<

void main(void)

{ Date myDate;//存储日期 while(true)

{ int year;//临时存储年份 int month;//临时存储月份 int day;//临时存储天数

cout<<\请按YYYY格式输入年:\ cin>>year;

cout<<\请按mm格式输入月:\ cin>>month;

cout<<\请按dd格式输入日:\ cin>>day;

if(myDate.setDate(month,day,year)) break; else

cout<<\日期输入错误,请重新输入!\ }

cout<<\您输入的日期是:\\n\ myDate.display(); }

8-2、设计一个人口类Population,存储某年的人数、出生的人数和死亡人数,其函数成员能返回出生率和死亡率。 #include using namespace std; //类定义//

class Population

{ int pnum; //存储某年的人数 int birthnum;//存储出生的人数 int deadnum;//存储死亡的人数 public:

bool setData(const int,const int,const int);

float birthrate(){return birthnum/float(pnum);} float deadrate(){return deadnum/float(pnum);} int getpnum(){return pnum;}

int getbirthnum(){return birthnum;} int getdeadnum(){return deadnum;} };

//返回值:如果成功赋值则返回true,否则返回false// bool Population::setData(const int p,const int b,const int d) { if(p<0) return false; if(b>p||b<0) return false; if(d>p||d<0) return false; pnum=p;

birthnum=b; deadnum=d; return true; }

void main(void)

{ Population people; int pn,bn,dn; while(true) {

cout<<\请输入总人数:\ cin>>pn;

cout<<\请输入出生的人数:\ cin>>bn;

cout<<\请输入死亡的人数:\ cin>>dn;

if(people.setData(pn,bn,dn)) break; else

cout<<\输入错误,请重新输入!\ }

cout<<\出生率是: \ cout<<\死亡率是: \}

8-3、设计一个类,具有一个float指针成员,且函数成员如下:

1)构造函数具有一个整型参数count,为指针成员分配count个元素空间; 2)析构函数释放该空间;

3)向指针指向的空间存储数据的函数; 4)返回空间中这些数的平均值的函数。 #include #include using namespace std; class DataArray

{ int length; //存储数组元素个数 float *fltData;//存储数据 public:

DataArray(int =1); //构造函数 ~DataArray();//析构函数

bool setData(const float,const int); //设置成员变量 float getAverage(void); //计算并返回平均值 };

//构造函数//

//count:希望创建的动态数组元素个数。// DataArray::DataArray(int count) { cout<<\调用构造函数...\ if(count>0)

{ length=count;

fltData=new float[length]; }else

{ length=1;

fltData=new float[length]; }

for(int i=0;i

//析构函数//

DataArray::~DataArray()

{ cout<<\调用析构函数...\ delete [] fltData; length=0; }

//设置成员变量//

//tData:要赋给指定变量的值。//

//subscript:指定要赋值的变量数组元素为第几个。// //返回值:如果成功赋值则返回true,否则返回false。// bool DataArray::setData(const float tData,const int subscript) { if(subscript+1>length) return false; else

fltData[subscript]=tData; return true; }

9-1、定义NumDays类,功能是将以小时(hour)为单位的工作时间转换为天数(day)。构造函数具有一个代表工作小时的参数,其他函数成员实现小时和天的存储和检索。同时,该类需重载+、-、++、――运算符。

9-2、设计TimeOff类,用于计算雇员生病、休假和不支付报酬的时间。

9-3、采用TimeOff类定义一个对象。要求用户输入某雇员已经工作的月数(months),然后采用TimeOff类对象计算并显示雇员因病休假和正常休假的最多天数。注意:雇员每月可以有12小时的带薪休假和8小时的生病休假。 //NumDays.h文件// class NumDays

{ float hour; //以小时存储工作时间 float day; //以天数存储工作时间 public:

NumDays(const float =0);//构造函数

void setHours(const float);//以小时为参数设置成员变量 void setDays(const float);//以天数为参数设置成员变量 float getHours(void);//返回按小时计的工作时间 float getDays(void);//返回按天数计的工作时间 float operator +(NumDays &);//重载+操作符 float operator -(NumDays &);//重载-操作符 NumDays operator ++(); //重载++前置操作符 NumDays operator ++(int);//重载++后置操作符 NumDays operator --();//重载--前置操作符 NumDays operator --(int);//重载--后置操作符 };

//构造函数//

//tHour:工作时间(小时)//

NumDays::NumDays(const float tHour) { hour=tHour; day=tHour/8; }

//以小时为参数设置成员变量// //tHour:工作时间(小时)。//

void NumDays::setHours(const float tHour) { hour=tHour; day=tHour/8; }

//以天数为参数设置成员变量// //tDay:工作时间(天数)//

void NumDays::setDays(const float tDay) { hour=tDay*8; day=tDay; }

//返回按小时计的工作时间//

//返回值:返回工作时间(小时)// float NumDays::getHours(void) { return hour; }

//返回按天数计的工作时间//

//返回值:返回工作时间(天数)// float NumDays::getDays(void) { return day; }

//重载+操作符//

//返回值:返回两者相加之和(小时)//

float NumDays::operator +(NumDays &NumDaysObj) { return(this->hour+NumDaysObj.hour); }

//重载-操作符//

//返回值:返回两者相减之差(小时)//

float NumDays::operator -(NumDays &NumDaysObj) { return(this->hour-NumDaysObj.hour); }

//重载++前置操作符//

NumDays NumDays::operator ++() { hour++; day=hour/8; return *this; }

//重载++后置操作符//

NumDays NumDays::operator ++(int) { NumDays temp=*this; hour++;

}

//重载--前置操作符//

NumDays NumDays::operator --() { hour--;

day=hour/8; return *this; }

//重载--后置操作符//

NumDays NumDays::operator --(int) { NumDays temp=*this; hour--;

day=hour/8; return temp; }

//TimeOff.h文件

#include\class TimeOff {

char name[20],id[10]; NumDays maxSickDays; NumDays sickTaken; NumDays maxVacation; NumDays vacTaken; NumDays maxUnpaid; NumDays unpaidTaken; public:

TimeOff(float,float,float,char *,char *); //设置允许的最多小时数及雇员姓名工号

void setSickTaken(NumDays &sickObj); void setVacTaken(NumDays &vacObj);

void setUnpaidTaken(NumDays &unpaidObj); char * getName() {return name;} char * getId() {return id;}

day=hour/8; return temp;

NumDays getMaxSickDays(){return maxSickDays;} NumDays getSickTaken() {return sickTaken;} NumDays getMaxVacation(){return maxVacation;} NumDays getVacTaken() {return vacTaken;} NumDays getMaxUnpaid() {return maxUnpaid;} NumDays getUnpaidTaken(){return unpaidTaken;} };

TimeOff::TimeOff(float maxSickh,float maxVach,float maxUnph,char *n,char *i) :maxSickDays(maxSickh),maxVacation(maxVach),maxUnpaid(maxUnph) {

strcpy(name,n); strcpy(id,i); }

void TimeOff::setSickTaken(NumDays &sickObj) { float sickhours;

sickhours=sickTaken+sickObj; sickTaken.setHours(sickhours); }

void TimeOff::setVacTaken(NumDays &vacObj) {

float vachours;

vachours=vacTaken+vacObj; vacTaken.setHours(vachours); }

void TimeOff::setUnpaidTaken(NumDays &unpaidObj) {

float unpaidhours;

unpaidhours=unpaidTaken+unpaidObj; unpaidTaken.setHours(unpaidhours); }

//main.cpp

#include using namespace std; #include\

void main() {

TimeOff employee(8,12,24,\张三\ int months;

cout<<\请输入\已经工作的月数:\\n\ cin>>months;

for(int i=1;i<=months;i++) {

float hours;

NumDays offObj;

cout<<\请输入第\个月生病休假的累计小时数:\\n\ cin>>hours;

if(hours>employee.getMaxSickDays().getHours())

cout<<\该月超过标准的生病休假小时数\小时!\\n\ offObj.setHours(hours);

employee.setSickTaken(offObj);

cout<<\请输入第\个月带薪休假的累计小时数:\\n\ cin>>hours;

if(hours>employee.getMaxVacation().getHours())

cout<<\该月超过标准的带薪休假小时数\小时!\\n\ offObj.setHours(hours);

employee.setVacTaken(offObj);

cout<<\请输入第\个月不带薪休假的累计小时数:\\n\ cin>>hours;

if(hours>employee.getMaxUnpaid().getHours())

cout<<\该月超过标准的不带薪休假小时数\小时!\\n\ offObj.setHours(hours);

employee.setUnpaidTaken(offObj); }

cout<

cout<<\因病休假的天数为:\\t\ cout<<\带薪休假的天数为:\\t\ cout<<\不带薪休假的天

为:\\t\}

第10章习题:

10-2、设计Employee类,其数据成员能保存如下信息: 雇员姓名:char *指针;

雇员编号:格式为XXX-L,X是0~9之间的数字,L是A~M之间的字母; 受雇日期:

向该类增加构造函数、析构函数和其他相关函数成员。

设计Employee类的子类EmployeePay,其具有如下数据成员: 月工资:double型变量; 部门号:int型变量;

编写完整的程序,要求用户从键盘输入雇员信息,然后在屏幕上显示这些信息。 //Employee.h #include using namespace std; class Employee {

protected:

char * Name;

char EmployCode[6]; char EmployDate[9]; public:

Employee(char *);

bool setEmployCode(char *); bool setEmployDate(char *); ~Employee();

char *getName(){return Name;}

char *getEmployCode() {return EmployCode;} char *getEmployDate(){return EmployDate;} };

Employee::Employee(char *n) {

Name = new char[strlen(n) + 1]; strcpy(Name,n); }

Employee::~Employee() {

delete [] Name; }

bool Employee::setEmployCode(char* c) {

if(strlen(c)!=5)

{cout<<\编号长度为5个字符,请重输!\\n\ else if(!isdigit(c[0])||!isdigit(c[1])||!isdigit(c[2]))

{cout<<\编号前三位必须是0~9的数字,请重输!\\n\ else if(c[3]!='-')

{cout<<\编号第四位必须是-,请重输!\\n\ else if(c[4]>'M'||c[4]<'A')

{cout<<\编号第五位必须是A~M之间的字母,请重输!\\n\ else

{strcpy(EmployCode,c); return true;} }

bool Employee::setEmployDate(char* d) {

long tmp,yy,mm,dd; if(strlen(d)!=8)

{cout<<\日期长度为8个字符,请重输!\\n\ else

{tmp=atoi(d);yy=tmp/10000;mm=tmp/1000;dd=tmp0;} if(yy<2000)

{cout<<\受雇年份应大于2000,请重输!\\n\ else if(mm<1||mm>12)

{cout<<\月份应在1~12之间,请重输!\\n\ else if(dd<1||dd>31)

{cout<<\日期应在1~31之间,请重输!\\n\ else

{strcpy(EmployDate,d); return true;} }

//EmployeePay.h

#include\

class EmployeePay : public Employee {

protected:

double MonthPay; int DepartNum; public:

EmployeePay(char *,int);

void setMonthPay(double p){MonthPay=p;} double getMonthPay(){return MonthPay;} int getDepartNum() {return DepartNum;} void InfoShow(); };

EmployeePay::EmployeePay(char* n,int dnum) : Employee(n)

{ DepartNum=dnum; }

void EmployeePay::InfoShow() { cout<<\雇员的信息如下:\\n\ cout<<\姓名:\\t\

cout<<\编号:\\t\ cout<<\受雇日期:\\t\ cout<<\部门号:\\t\ cout<<\月工资:\\t\}

//main.cpp

#include using namespace std; #include\void main() {

EmployeePay empay(\张三\ double p;

cout<<\请输入\的编号:\\n\ while(true) { char c[6];

cin.getline(c,6);

if(empay.setEmployCode(c)) break; }

cout<<\请输入\的受雇日期:\\n\ while(true) { char d[9];

cin.getline(d,9);

if(empay.setEmployDate(d)) break; }

cout<<\请输入\的月工资:\\n\ cin>>p;

empay.setMonthPay(p); empay.InfoShow(); }

10-3、设计EmployeePay的子类HourlyPay,其数据成员能存储如下信息: 正常工作每小时的工资、超时工作每小时的工资和已经工作的小时数。 编写程序,要求用户输入信息并做有效性检验。 //Employee.h见上题 //EmployeePay.h见上题 //HourlyPay.h

#include\

class HourlyPay : public EmployeePay {

protected:

double HourPay,ExHourPay; int Hours,ExHours; public:

HourlyPay(char*,int); bool setHourPay(double); bool setExHourPay(double); bool setHours(int); bool setExHours(int);

void setMonthPay(){MonthPay=HourPay*Hours+ExHourPay*ExHours;} };

HourlyPay::HourlyPay(char* n,int d):EmployeePay(n,d)

{}

bool HourlyPay::setHourPay(double hp) {

if(hp<0||hp>50) return false; HourPay=hp; return true; }

bool HourlyPay::setExHourPay(double exhp) {

if(exhp<0||exhp>100) return false; ExHourPay=exhp; return true; }

bool HourlyPay::setHours(int h) {

if(h<0||h>176) return false; Hours=h; return true; }

bool HourlyPay::setExHours(int exh) {

if(exh<0||exh>64) return false; ExHours=exh; return true; }

//main.cpp

#include using namespace std; #include\void main() {

HourlyPay empay(\张三\

cout<<\请输入\的正常工作小时工资: while(true) { double hp;

\\n\

本文来源:https://www.bwwdw.com/article/wh5g.html

Top