面向对象A面向对象习题(南航皮德常).doc_第1页
面向对象A面向对象习题(南航皮德常).doc_第2页
面向对象A面向对象习题(南航皮德常).doc_第3页
面向对象A面向对象习题(南航皮德常).doc_第4页
面向对象A面向对象习题(南航皮德常).doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第8章习题:8-1、设计Date类,输出合法日期。#include using namespace std;/类定义/class Dateint 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 January;else if(m=2)return February;else if(m=3)return March;else if(m=4)return April;else if(m=5)return May;else if(m=6)return June;else if(m=7)return July;else if(m=8)return August;else if(m=9)return September;else if(m=10)return October;else if(m=11)return November;elsereturn December;/设置成员变量/参数:mm:月份,dd:天数,yy:年份/返回值:如果成功赋值则返回true,否则返回false/bool Date:setDate(const int mm,const int dd,const int yy)if(mm12) return false;if(dd31) return false;year=yy;month=mm;day=dd;return true;/在屏幕上显示日期/void Date:display(void)/ 按照12-25-2004的形式输出日期coutmonth-day-yearn;/ 按照December 25,2004的形式输出日期coutgetMonth(month) day, yearn;/ 按照25 December 2004的形式输出日期coutday getMonth(month) yearn;void main(void)Date myDate;/存储日期while(true)int year;/临时存储年份int month;/临时存储月份int day;/临时存储天数coutyear;coutmonth;coutday;if(myDate.setDate(month,day,year)break;elsecout日期输入错误,请重新输入!endl;cout您输入的日期是:n;myDate.display();8-2、设计一个人口类Population,存储某年的人数、出生的人数和死亡人数,其函数成员能返回出生率和死亡率。#include using namespace std;/类定义/class Populationint 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(pp|bp|d0) return false;pnum=p;birthnum=b;deadnum=d;return true;void main(void)Population people;int pn,bn,dn;while(true)coutpn;coutbn;coutdn;if(people.setData(pn,bn,dn)break;elsecout输入错误,请重新输入!endl;cout出生率是: people.birthrate()endl;cout死亡率是: people.deadrate()endl;8-3、设计一个类,具有一个float指针成员,且函数成员如下:1)构造函数具有一个整型参数count,为指针成员分配count个元素空间;2)析构函数释放该空间;3)向指针指向的空间存储数据的函数;4)返回空间中这些数的平均值的函数。#include #include using namespace std;class DataArrayint length; /存储数组元素个数float *fltData;/存储数据public:DataArray(int =1); /构造函数DataArray();/析构函数bool setData(const float,const int);/设置成员变量float getAverage(void); /计算并返回平均值;/构造函数/count:希望创建的动态数组元素个数。/DataArray:DataArray(int count)cout调用构造函数.0)length=count;fltData=new floatlength;elselength=1;fltData=new floatlength;for(int i=0;ilength;i+)fltDatai=0;/析构函数/DataArray:DataArray()cout调用析构函数.length)return false;elsefltDatasubscript=tData;return true;/计算并返回平均值/返回值:返回数组所存储的数据的平均值。/float DataArray:getAverage(void)float sum=0;for(int i=0;ilength;i+)sum+=fltDatai;return(sum/length);void main(void)/对DataArray类进行测试cout创建DataArray对象并初始化.endl;DataArray Obj(3);cout给DataArray对象赋值:endl;for(int i=0;i3;i+)/ 产生一个随机数float temp=(float)rand()/10;Obj.setData(temp,i);cout第(i+1)个值为:tempendl;cout平均值为:Obj.getAverage()endl;8-4、设计一个薪水类Payroll,数据成员包括单位小时工资、已经工作的小时数、本周应付工资数。在主函数定义一个具有10个元素的对象数组(10个雇员),询问每个雇员本周已经工作的小时数,然后显示应得的工资。输入有效性检验:每周工作的小时数不能大于60,也不能为负数。#include #include using namespace std;/薪水类声明/class Payrollfloat pay_per_hour;/存储每小时的工钱float hours;/存储雇员工作时间float total_pay;/存储雇员应得工资public:Payroll(const float =0);/构造函数bool setHours(const float);/设置工作时间float getPayment(void);/获取应得工资;/构造函数/hour:雇员工作时间。/Payroll:Payroll(const float hour)pay_per_hour=5.0f;hours=0;total_pay=0;/设置工作时间/hour:雇员工作时间/返回值:如果变量赋值成功则返回true,否则返回false。/bool Payroll:setHours(const float hour)/判断工作时间是否介于0至60之间if(hour=0 & hour=60)hours=hour;total_pay=pay_per_hour * hours;return true;elsereturn false;/获取应得工资/返回值:返回计算得到的雇员应得的工资/float Payroll:getPayment(void)return total_pay;void main(void)Payroll employee10;/雇员对象数组/对10个雇员对象进行赋值for(int i=0;i10;i+)float tHour;cout请输入第setw(2)(i+1)tHour;if(employeei.setHours(tHour)break;elsecout您输入的数据不正确,请重新输入!endl;/显示每个雇员应得的工资for(i=0;i10;i+)cout第setw(2)(i+1)个雇员应得工资setiosflags(ios:fixed)setprecision(2)employeei.getPayment()元endl;8-5、设计InvoiceItem类和商品销售类,完成如下功能:1)询问客户购买的商品名称和数量;2)从InvoiceItem对象获得每个商品的成本价;3)在成本价基础上加上30利润,得到每个商品的单价;4)将商品单价与购买数量相乘,得到商品小计;5)将商品小计乘以6,得到商品的零售税;6)将商品小计加上零售税得到该商品的销售额;7)显示客户本次交易购买商品的小计、零售税和销售额。有效性检验:商品数量不能为负数。#include using namespace std;class InvoiceItem char *desc; int units;floatcost;public: InvoiceItem(char *d, int u=0, float c=0) desc = new char strlen(d)+1;strcpy(desc, d); units = u;cost = c; InvoiceItem( ) delete desc; units = 0;cost = 0; char *getDesc( ) return desc; int getUnits( ) return units; float getCost( ) return cost; ;class Sale float price, salesum, saletax, totalsum;int buynum;public:bool setnum(int, int);void calcSale(float p) price=p;/单价salesum=price*buynum;/商品小计saletax=salesum*0.06;/零售税totalsum = salesum + saletax; /销售额float getprice() return price; int getbuynum() return buynum; float getsalesum() return salesum; float getsaletax() return saletax; float gettotalsum() return totalsum; ;bool Sale:setnum(int n,int invoicenum)if(n=0 & n=invoicenum )buynum=n;return true;elsereturn false;void main()InvoiceItem Inventory3 = InvoiceItem(ABC,10,0.5f),InvoiceItem(DEF,20,1.2f), InvoiceItem(GHI,30,2.5f), ;Sale s1;int i, index=-1;cout 库存商品列表:n; for(i = 0; i 3; i+) cout 名称:Inventoryi.getDesc( )t数量:;cout Inventoryi.getUnits( )t成本价:;cout Inventoryi.getCost( )元endl; /输出库存商品信息cout请输入购买的商品名称: ;while(true)char name51;cin.getline(name,51);for(i = 0; i 3; i+) if(strcmp(Inventoryi.getDesc(),name)=0)index=i;if(index!=-1)break;elsecout您输入的商品不存在,请重新输入!endl;coutnum;if(s1.setnum(num,Inventoryindex.getUnits()break;elsecout您输入的数量不正确,请重新输入!endl;/计算销售额s1.calcSale(Inventoryindex.getCost()*1.3);/输出销售信息cout本次购买的商品名称:Inventoryindex.getDesc()endl;cout本次购买的商品数量:s1.getbuynum()endl;cout本次购买的商品单价:s1.getprice()元endl;cout本次购买的商品小计:s1.getsalesum()元endl;cout本次购买的零售税:s1.getsaletax()元endl;cout本次购买的销售额:s1.gettotalsum()元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+;day=hour/8;return temp;/重载前置操作符/NumDays NumDays:operator -()hour-;day=hour/8;return *this;/重载后置操作符/NumDays NumDays:operator -(int)NumDays temp=*this;hour-;day=hour/8;return temp;/TimeOff.h文件#includeNumDays.hclass TimeOffchar name20,id10;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;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#includeusing namespace std;#includeTimeOff.hvoid main()TimeOff employee(8,12,24,张三,0007);int months;cout请输入employee.getName()months;for(int i=1;i=months;i+)float hours;NumDays offObj;cout请输入第ihours;if(hoursemployee.getMaxSickDays().getHours()cout该月超过标准的生病休假小时数:employee.getMaxSickDays().getHours()小时!n;offObj.setHours(hours);employee.setSickTaken(offObj);cout请输入第ihours;if(hoursemployee.getMaxVacation().getHours()cout该月超过标准的带薪休假小时数:employee.getMaxVacation().getHours()小时!n;offObj.setHours(hours);employee.setVacTaken(offObj);cout请输入第ihours;if(hoursemployee.getMaxUnpaid().getHours()cout该月超过标准的不带薪休假小时数:employee.getMaxUnpaid().getHours()小时!n;offObj.setHours(hours);employee.setUnpaidTaken(offObj);coutemployee.getName()的休假天数统计如下:n;cout因病休假的天数为:temployee.getSickTaken().getDays()endl;cout带薪休假的天数为:temployee.getVacTaken().getDays()endl;cout不带薪休假的天数为:temployee.getUnpaidTaken().getDays()endl;第10章习题:10-2、设计Employee类,其数据成员能保存如下信息:雇员姓名:char *指针;雇员编号:格式为XXX-L,X是09之间的数字,L是AM之间的字母;受雇日期:向该类增加构造函数、析构函数和其他相关函数成员。设计Employee类的子类EmployeePay,其具有如下数据成员:月工资:double型变量;部门号:int型变量;编写完整的程序,要求用户从键盘输入雇员信息,然后在屏幕上显示这些信息。/Employee.h#includeusing namespace std;class Employee protected:char * Name;char EmployCode6;char EmployDate9; 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 charstrlen(n) + 1;strcpy(Name,n);Employee:Employee()delete Name;bool Employee:setEmployCode(char* c)if(strlen(c)!=5) cout编号长度为5个字符,请重输!n; return false;else if(!isdigit(c0)|!isdigit(c1)|!isdigit(c2)cout编号前三位必须是09的数字,请重输!n; return false;else if(c3!=-)coutM|c4A)cout编号第五位必须是AM之间的字母,请重输!n; return false;else strcpy(EmployCode,c); return true;bool Employee:setEmployDate(char* d)long tmp,yy,mm,dd;if(strlen(d)!=8) cout日期长度为8个字符,请重输!n; return false;else tmp=atoi(d);yy=tmp/10000;mm=tmp/100%100;dd=tmp%100;if(yy2000)cout受雇年份应大于2000,请重输!n; return false;else if(mm12)cout月份应在112之间,请重输!n; return false;else if(dd31)cout日期应在131之间,请重输!n; return false;else strcpy(EmployDate,d); return true;/EmployeePay.h#includeEmployee.hclass 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姓名:tNameendl;cout编号:tEmployCodeendl;cout受雇日期:tEmployDateendl;cout部门号:tDepartNumendl;cout月工资:tMonthPayendl;/main.cpp#includeusing namespace std;#includeEmployeePay.hvoid main()EmployeePay empay(张三,2);double p;cout请输入empay.getName()的编号:n;while(true)char c6;cin.getline(c,6);if(empay.setEmployCode(c)break;cout请输入empay.getName()的受雇日期:n;while(true)char d9;cin.getline(d,9);if(empay.setEmployDate(d)break;cout请输入empay.getName()p;empay.setMonthPay(p);empay.InfoShow();10-3、设计EmployeePay的子类HourlyPay,其数据成员能存储如下信息:正常工作每小时的工资、超时工作每小时的工资和已经工作的小时数。编写程序,要求用户输入信息并做有效性检验。/Employee.h见上题/EmployeePay.h见上题/HourlyPay.h#includeEmployeePay.hclass HourlyPay : public EmployeePayprotected: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(hp50) return false;HourPay=hp;return true;bool HourlyPay:setExHourPay(double exhp)if(exhp100) return false;ExHourPay=exhp;return true;bool HourlyPay:setHours(int h)if(h176) return false;Hours=h;return true;bool HourlyPay:setExHours(int exh)if(exh64) return false;ExHours=exh;return true;/main.cpp#includeusing namespace std;#includeHourlyPay.hvoid main()HourlyPay empay(张三,2);cout请输入empay.getName()hp;if(empay.setHourPay(hp)break;else cout正常工作小时工资应在150之间,请重输!n;cout请输入empay.getName()exhp;if(empay.setExHourPay(exhp)break;else cout超时工作小时工资应在1100之间,请重输!n;cout请输入empay.getName()h;if(empay.setHours(h)break;else cout正常工作小时数应在1176之间,请重输!n;cout请输入empay.getName()exh;if(empay.setExHours(exh)break;else cout超时工作小时数应在164之间,请重输!n;empay.setMonthPay();coutempay.getName()的月工资为:empay.getMonthPay();10-6、(略)10-7、定义抽象类BasicShape及其子类Circle和Rectangle,创建Circle和Rectangle类的对象,检验程序能否正确计算各形状的面积。#include using namespace std;#define PI 3.1415926/BasicShape类声明/class BasicShapepublic:double getArea(void) return area;/返回面积virtual void calcArea(void) =0;/计算面积 protected:double area;/存储面积;/Circle类声明/class Circle:public BasicShapepublic:Circle(int X=0,int Y=0,double R=0)/构造函数centerX=X;centerY=Y;radius=R;calcArea();int getCenterX(void) return centerX;/返回圆心X坐标int getCenterY(void) return centerY;/返回圆心Y坐标void calcArea(void) area=PI*radius*radius;/计算圆面积 private:int centerX; /存储圆心X坐标int centerY; /存储圆心Y坐标double radius; /存储圆半径;/Rectangle类声明class Rectangle:public BasicShapepublic:Rectangle(long W=0,long L=0)/构造函数width=W;length=L;calcArea();long getWidth(void) return width; /返回矩形的宽long getLength(void) return length;/返回举行的长void calcArea(vo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论