您好,欢迎访问三七文档
1.设计一个Person类,它有两个派生类Student和Employee,Employee有两个派生类Faculty和Staff。?Person类有一个string类型的名字name,string型的身份号id,string型的电话号码phonenumber,Person类构造函数的对应name的参数不能有默认形参值。?Student类有一个年级grade属性(Freshman、Sophomore、Junior或Senior),将属性值定义为常量;?Employee类有一个string型的办公地点office,一个double型的月薪水salary和一个MyDate型的雇佣日期dateHired;?Faculty类有一个级别rank属性(Professor=3、AssociateProfessor=2、AssistantProfessor=1),属性值也定义成常量。教师的薪水的计算方式为“薪水=基本工资×级别”;?Staff类有一个职务position属性,为string类型,教工的薪水计算方式为“薪水=基本工资+津贴×工作年数”。工作年数算到2010年1月1日为止,基本工资BasicWages、津贴Allowance定义为常量。?上述5个类都有一个print成员函数来输出该类对象除薪水外的基本信息。?MyDate类有year、month、day三个数据成员,有一个计算两个MyDate对象间年差的成员函数diffYear(MyDate&),年差计算只精确到月;三个数据成员的获取器函数。3.定义上述类,并合理地补充构造函数和其它需要的函数。在main函数中,定义一个Person类对象,一个Student类对象和一个Employee类对象一个Faculty类对象和一个Staff类对象。输出Person类及其派生类对象的基本信息,并输出Faculty类对象和Staff类对象的薪水。#includeiostream#includestringusingnamespacestd;classMyDate{public:MyDate(inta,intb,intc){year=a;month=b;day=c;}intyear;intmonth;intday;};doubleProfessor=3;doubleAssociateProfessor=2;doubleAssistantProfessor=1;doubleBasicWages=5000;//基本工资doubleAllowance=200;//津贴classperson{public:person(stringN,stringI,stringP){name=N;id=I;phonenumber=P;}voidprint1(){coutname:nameid:idphonenumber:phonenumberendl;}~person(){}private:stringname,id,phonenumber;};classStudent:publicperson//person类里的student类{public:Student(stringN,stringI,stringP,stringG):person(N,I,P){grade=G;}voidprint2(){coutgrade:grade;}~Student(){}private:stringgrade;};classemployee:publicperson//person类里的Employee类{public:employee(stringN,stringI,stringP,stringO,doubleS,doubleD):person(N,I,P){office=O;salary=S;dateHired=D;}doublegetout(){returndateHired;}voidprint3(){coutoffice:officedateHired:dateHired;}~employee(){}//Employee类有一个string型的办公地点office,一个double型的月薪水salary和一个MyDate型的雇佣日期dateHired;private:stringoffice;doublesalary;doubledateHired;};classfaculty:publicemployee{public:faculty(stringN,stringI,stringP,stringO,doubleS,doubleD):employee(N,I,P,O,S,D){}voidprint4(){coutsalary:BasicWages*AssociateProfessor;}//教师的薪水的计算方式为薪水=基本工资×级别~faculty(){}};classstaff:publicemployee{public:staff(stringN,stringI,stringP,stringO,doubleS,doubleD):employee(N,I,P,O,S,D){}voidprint5(){coutsalary:BasicWages+Allowance*getout();}//教工的薪水计算方式为薪水=基本工资+津贴×工作年数~staff(){}};intmain(){personper(张三,1,765);Studentstud(李四,2,685,Sophomore);employeeemp(王五,3,464,fds,5000,102);facultyfac(赵六,4,456,sada,5001,103);staffsta(周七,5,267,hadf,5002,104);per.print1();coutendl;stud.print1();stud.print2();coutendl;emp.print1();emp.print3();coutendl;fac.print1();fac.print3();fac.print4();coutendl;sta.print1();sta.print3();sta.print5();coutendl;coutendl;return0;}
本文标题:C++上机实验七
链接地址:https://www.777doc.com/doc-4922381 .html