oop复习资料

上传人:油条 文档编号:115841332 上传时间:2019-11-15 格式:DOC 页数:16 大小:73KB
返回 下载 相关 举报
oop复习资料_第1页
第1页 / 共16页
oop复习资料_第2页
第2页 / 共16页
oop复习资料_第3页
第3页 / 共16页
oop复习资料_第4页
第4页 / 共16页
oop复习资料_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《oop复习资料》由会员分享,可在线阅读,更多相关《oop复习资料(16页珍藏版)》请在金锄头文库上搜索。

1、实验一,补充题: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;clas

2、s 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

3、;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;

4、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个销货员的销售

5、情况为:销货员号(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(

6、);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; cou

7、t销售平均价: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两个

8、类,二者都有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 tot

9、alWeight(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类员工

10、,工人的工资为每小时工资额乘当月工作时数再加上工龄工资;销售员工资为每小时工资额乘当月工作时数加上销售额加上工龄工资,其中,销售额提成等于该销售员当月售出商品金额的1%;管理人员工资为基本工资再加上工龄工资。工龄工资就是雇员在该公司工作的工龄,每增加一年,月工资就增加35元。请用面向对象方法分析、设计这个程序,并通过main函数测试所设计的类。)#include #include using namespace std;class Employeeprotected:string name; /姓名int working_years; /工龄public:Employee() Employee

11、(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 Em

12、ployee: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 Em

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号