已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验一,补充题:1. 设计一个Student(学生)类,并使Student类具有以下要求:(bsy1_1.cpp)(1)该类具有学生姓名、学号、程序设计、信息处理、数据结构三门课程的成绩;(2)学生全部信息由键盘键入;(3)通过成员函数统计学生平均成绩,当课程数量增加时,成员函数无须修改仍可求取平均成绩;(4)输出学生的各科成绩与平均成绩;(5)学生对象的定义采用对象数组实现;(6)统计不及格学生人数;(7)能以最方便的方式实现对课程数量和学生人数的修改。#include #include using namespace std;const int N=3;const int M=2;class Studentpublic:Student();Student();void display();double get_avg();bool no_pass();protected:char name20;char id10;double courseN;double avg_score;Student:Student() cout请输入学生姓名及学号:nameid; for(int i=0;icoursei; Student:Student() cout执行析构函数endl;void Student:display() coutname ; coutid ; for(int i=0;iN;i+) coutcoursei ; coutget_avg(); coutendl;double Student:get_avg() avg_score=0; for(int i=0;iN;i+) avg_score+=coursei; avg_score=avg_score/N; return avg_score;bool Student:no_pass() for(int i=0;iN;i+) if(coursei60) return true; return false;void main() int i,no_pass=0; cout输入学生信息,不同数据之间以空格分隔:endl; cout姓名 学号 程序设计 信号处理 数据结构endl; Student sM; coutendl; coutendl以下是输出信息与统计信息:endl; cout姓名 学号 程序设计 信号处理 数据结构 平均成绩endl; for(i=0;iM;i+) si.display(); if(si.no_pass()=true) no_pass+; cout不及格人数为:no_pass人。endl;2商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优惠。现已知当天3个销货员的销售情况为:销货员号(num) 销货件数(quantity) 销货单价(price) 101 5 23.5 102 12 24.56 103 100 21.5请编程,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。(bsy1_2.cpp)#include using namespace std;class Productpublic:Product(int m,int q,float p):num(m),quantity(q),price(p);void total();static float average();static void display();private:int num; /销售员号int quantity; /销售件数float price; /销货单价static float discount; /商店统一折扣static float sum; /总销售款static int n; /商品销售总件数;void Product:total() float rate=1.0; if(quantity10) rate=0.98*rate; sum+=quantity*price*rate*(1-discount); n+=quantity;void Product:display() cout总销售款:sumendl; cout销售平均价:average()endl;float Product:average() return (sum/n);float Product:discount=0.05f;float Product:sum=0;int Product:n=0;int main()Product Prod3=Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5);for(int i=0;i3;i+) Prodi.total();Product:display();return 0;实验二 补充题答案 1. 声明Book与Ruler两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和。(bsy2_1.cpp)#include using namespace std;class Book;class Rulerpublic:Ruler()weight=0;Ruler(double x) weight=x;friend double totalWeight(Book &b,Ruler &r);private:double weight;class Bookpublic:Book()weight=0;Book(double x) weight=x; friend double totalWeight(Book &a,Ruler &b);private:double weight;double totalWeight(Book &a,Ruler &b) return a.weight+b.weight;void main()Book b1(3);Ruler r1(5);couttotalWeight is:totalWeight(b1,r1)endl; 1. 请将实验二中补充部分的第4题见下,改用抽象类来实现雇员类。要求用基类指针数组,使它的每一个元素指向一个派生类对象,求若干个雇员的月工资。(实验二 补充:2. 某公司财务部需要开发一个计算雇员工资的程序。该公司有3类员工,工人的工资为每小时工资额乘当月工作时数再加上工龄工资;销售员工资为每小时工资额乘当月工作时数加上销售额加上工龄工资,其中,销售额提成等于该销售员当月售出商品金额的1%;管理人员工资为基本工资再加上工龄工资。工龄工资就是雇员在该公司工作的工龄,每增加一年,月工资就增加35元。请用面向对象方法分析、设计这个程序,并通过main函数测试所设计的类。)#include #include using namespace std;class Employeeprotected:string name; /姓名int working_years; /工龄public:Employee() Employee(string na, int wy) name=na; working_years=wy;virtual float computepay() return 35*working_years;virtual void display()=0;class Worker: public Employeeprivate:float hours,wage; /工作时数,每小时工资额public:Worker(string na,int wy,float h1,float w1):Employee(na,wy) hours=h1; wage=w1;float computepay()return Employee:computepay()+wage*hours;void display() coutWorker Salary for name is ; coutcomputepay()endl;class Salesperson: virtual public Workerprivate:float sales_made; /销售额public:Salesperson(string na,int wy,float h1,float w1,float sm):Worker(na,wy,h1,w1) sales_made=sm;float computepay()return Worker:computepay()+0.01*sales_made;void display() coutSalesperson Salary for name is ; coutcomputepay()endl;class Manager:public Employeeprivate:float salary; /基本工资public:Manager(string nm,int wy,float sl):Employee(nm,wy) salary=sl;float computepay() return Employee:computepay()+salary;void display() coutManager Salary for name is ; coutcomputepay()endl;int main() Worker ming(xiao ming,8,181.5,9.5);Salesperson wang(wang fang,8,181.5,9.5,30000.0f);Manager li(xiao li,8,5000.0f); Employee *p3=&ming,&wang,&li;for(int i=0;idisplay();return 0;2.定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleManager(销售经理)。要求:(1)在Staff类中包含的数据成员有编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)和奖金(prize)。在Saleman类中还包含数据成员销售员提成比例(deductRate)和个人销售额(personAmount),在Manager类中还包含数据成员经理提成比例(totalDeductRate)和总销售额(totalAmount)。在SaleManager类中不包含其他数据成员。(2)各类人员的实发工资公式如下:员工实发工资=基本工资+奖金出勤率销售员实发工资=基本工资+奖金出勤率+个人销售额销售员提成比例经理实发工资=基本工资+奖金出勤率+总销售额经理提成比例销售经理实发工资=基本工资+奖金出勤率+个人销售额销售员提成比例+总销售额经理提成比例(3)每个类都有构造函数、输出基本信息函数(Output)和输出实发工资函数(OutputWage)。#include #include using namespace std;class Staff /基类protected: int num; /编号 string name; /姓名 float rateOfAttend; float basicSal; float prize; public: Staff() Staff(int n1,string name1,float rate,float base,float prize1) num=n1; name=name1; rateOfAttend=rate; basicSal=base; prize=prize1; Staff() virtual float Wage() return basicSal+prize*rateOfAttend; void OutputWage() /计算实发工资 cout实发工资:Wage()endl; void Output() /输出员工信息函数 cout编号:num, 姓名:nameendl; ;class Saleman : virtual public Staff/派生类,销售员类protected: float deductRate; /按销售额提成百分比 float personAmount; /个人销售额 public: Saleman() Saleman(int n1,string name1,float rate,float base,float prize1,float deduct1,float person1):Staff(n1,name1,rate,base,prize1) deductRate=deduct1; personAmount=person1; float Wage() return Staff:Wage()+personAmount*deductRate;class Manager : virtual public Staff /派生类,经理类protected:float totalDeductRate; float totalAmount; public: Manager() Manager(int n1,string name1,float rate,float base,float prize1,float totaldeduct1,float total1):Staff(n1,name1,rate,base,prize1) totalDeductRate=totaldeduct1; totalAmount=total1; float Wage() return Staff:Wage()+totalAmount*totalDeductRate;class Salesmanager : public Manager, public Saleman /销售经理类 public: Salesmanager() Salesmanager(int n1,string name1,float rate,float base,float prize1,float deduct1,float person1,float totaldeduct1,float total1):Staff(n1,name1,rate,base,prize1),Manager(n1,name1,rate,base,prize1,totaldeduct1,total1),Saleman(n1,name1,rate,base,prize1,deduct1,person1) float Wage() return Manager:Wage()+Saleman:Wage()-Staff:Wage(); int main() /主函数 Staff e1(1,staff,1,500,500);Manager m1(2,manager,1,2000,2000,0.8,100000); Saleman s1(3,saleman,1,1000,1000,0.5,10000); Salesmanager sm1(4,salesmanager,1,1000,1000,0.5,10000,0.8,100000); cout上述人员的基本信息为: endl;Staff *p4=&e1,&m1,&s1,&sm1;for(int i=0;iOutput(); pi-OutputWage(); return 0;3. 下面是一个数组类Carray的定义,请实现print()成员函数打印数组,并重载=,+和-运算符使之能对该数组类对象进行赋值和加减运算。请通过main函数验证它们。class CArray private: int *p_arr; int size; public: CArray(); CArray(int *p_a,int s); CArray(const CArray& r_other); CArray(); int& operator(int pos); CArray operator = (const CArray & r_other); CArray operator + (const CArray & r_other); CArray operator - (const CArray & r_other); void print() const;CArray:CArray() p_arr=0;size=0;CArray:CArray(int *p_a,int s) if(s0) size=s; p_arr=new intsize; for(int i=0;isize;i+) p_arri=p_ai;else p_arr=0;size=0;CArray:CArray(const CArray& r_other) size=r_other.size;if(size) p_arr=new intsize; for(int i=0;isize;i+) p_arri=r_other.p_arri;CArray:CArray() if(p_arr) delete p_arr;int& CArray:operator (int pos) if(pos=size) cout out of rangeendl; exit(1); return p_arrpos;CArray CArray:operator +(const CArray &r_other) int *tt; tt=new intsize; CArray temp(tt,size); for(int i=0;isize;i+) temp.p_arri=p_arri+r_other.p_arri; return temp;CArray CArray:operator -(const CArray &r_other) int *tt; tt=new intsize; CArray temp(tt,size); for(int i=0;isize;i+) temp.p_arri=p_arri-r_other.p_arri; return temp;CArray CArray:operator =(const CArray &r_other) int *tt; tt=new intsize; CArray temp(tt,size); if(this!=&r_other) delete p_arr; p_arr=new intr_other.size+1; for(int i=0;isize;i+) p_arri=r_other.p_arri; return *this;void CArray:print() constcout数组的数据为:endl;for(int i=0;isize;i+) coutp_arriendl;int main() int a5=1,2,3,4,5;CArray pa(a,5);CArray pb(pa),pc(pa);pc=pa+pb;pc.print();pc=pa-pb;pc.print();pc1=100;pa=pc; pa.print();return 0;3. 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c=a+b。并重载流插入运算符“”,使之能用于该矩阵的输入和输出。class Matrixpublic:Matrix();friend Matrix operator+(Matrix & a,Matrix & b);friend ostream& operator(istream &,Matrix &);private:int mat23;Matrix:Matrix() for(int i=0;i2;i+) for(int j=0;j3;j+) matij=0;Matrix operator+(Matrix &a,Matrix &b) Matrix c; for(int i=0;i2;i+) for(int j=0;j(istream& in,Matrix &m) cout请输入数组的值:endl; for(int i=0;i2;i+) for(int j=0;jm.matij; return in;ostream& operator(ostream& out,Matrix &m) cout输出数组的值:endl; for(int i=0;i2;i+) for(int j=0;j3;j+) outm.matij ; outa; cinb; coutMatrix a:endlaendl; coutMatrix b:endlbendl; c=a+b; coutMatrix c=Matrix a+Matrix b:endlcendl; return 0;实验四 补充题部分答案 2.从键盘上输入四条学生的信息(包括:学号,姓名,成绩,内容不限),并将这些学生数据写入到文件stud.dat中,以二进制形式存放。然后将其中第三条学生的数据和相应的成绩等级(大于等于90为优,8089为良,7079为中,6069为及格,小于等于59为不及格)显示到屏幕上,并且自定义异常类,当文件无法正常打开时,给出“文件访问失败!”的信息。(bsy4_2.cpp)#include #include #include using namespace std;class Studentint no;string name;int score;public: void setno(int no1) no=no1; void setname(string name1) name=nam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 结构化面试作答方法论
- 车位销售话术技巧
- 公益事业计划方案计
- 重症患者疼痛评估与管理新进展
- 咳嗽咳痰症状的分级护理与处理措施
- 肾盂癌护理查房
- 2025年西安石油大学招聘真题(行政管理岗)
- 妇科护理年终总结
- 儿童重症护理会诊:安全协作与风险规避技巧
- 戏曲创意美术课件
- 单片机智能鞋柜控制系统的设计与实现
- 重庆芯片项目商业计划书
- 2025天津市滨海新区辅警考试试卷真题
- 水样采集考试题及答案
- 工艺验证检查指南2025
- 资源与运营管理-第二次形考任务-国开-参考资料
- TCSCP 0006-2024 桥梁钢结构腐蚀程度评价方法
- 缠绕垫片基础知识培训
- 河北定向选调笔试真题2024
- 北京市海淀市2024-2025学年七年级上学期期末历史试题(含答案)
- 《玉米青贮技术》课件
评论
0/150
提交评论