- 73.01 KB
- 2022-07-28 发布
- 1、本文档由用户上传,淘文库整理发布,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,请立即联系网站客服。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细阅读内容确认后进行付费下载。
- 网站客服QQ:403074932
实验一,补充题:1.设计一个Student(学生)类,并使Student类具有以下要求:(bsy1_1.cpp)(1)该类具有学生姓名、学号、程序设计、信息处理、数据结构三门课程的成绩;(2)学生全部信息由键盘键入;(3)通过成员函数统计学生平均成绩,当课程数量增加时,成员函数无须修改仍可求取平均成绩;(4)输出学生的各科成绩与平均成绩;(5)学生对象的定义采用对象数组实现;(6)统计不及格学生人数;(7)能以最方便的方式实现对课程数量和学生人数的修改。#include#includeusingnamespacestd;constintN=3;constintM=2;classStudent{public:Student();~Student();voiddisplay();doubleget_avg();boolno_pass();protected:charname[20];charid[10];doublecourse[N];doubleavg_score;};Student::Student(){cout<<"请输入学生姓名及学号:"<>name>>id;for(inti=0;i>course[i];}}Student::~Student(){cout<<"执行析构函数"<usingnamespacestd;classProduct{public:Product(intm,intq,floatp):num(m),quantity(q),price(p){};voidtotal();staticfloataverage();staticvoiddisplay();private:intnum;//销售员号intquantity;//销售件数floatprice;//销货单价staticfloatdiscount;//商店统一折扣staticfloatsum;//总销售款staticintn;//商品销售总件数};voidProduct::total(){floatrate=1.0;if(quantity>10)rate=0.98*rate;sum+=quantity*price*rate*(1-discount);n+=quantity;}voidProduct::display(){cout<<"总销售款:"<usingnamespacestd;classBook;classRuler{public:Ruler(){weight=0;}Ruler(doublex){weight=x;}frienddoubletotalWeight(Book&b,Ruler&r);private:doubleweight;};classBook{public:Book(){weight=0;}Book(doublex){weight=x;}frienddoubletotalWeight(Book&a,Ruler&b);\nprivate:doubleweight;};doubletotalWeight(Book&a,Ruler&b){returna.weight+b.weight;}voidmain(){Bookb1(3);Rulerr1(5);cout<<"totalWeightis:"<#includeusingnamespacestd;classEmployee{protected:stringname;//姓名intworking_years;//工龄public:Employee(){}Employee(stringna,intwy){name=na;working_years=wy;}virtualfloatcomputepay(){return35*working_years;}virtualvoiddisplay()=0;\n};classWorker:publicEmployee{private:floathours,wage;//工作时数,每小时工资额public:Worker(stringna,intwy,floath1,floatw1):Employee(na,wy){hours=h1;wage=w1;}floatcomputepay(){returnEmployee::computepay()+wage*hours;}voiddisplay(){cout<<"WorkerSalaryfor"<display();}return0;}2.定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleManager(销售经理)。要求:(1)在Staff类中包含的数据成员有编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)和奖金(prize)。在Saleman类中还包含数据成员销售员提成比例(deductRate)和个人销售额(personAmount),在Manager类中还包含数据成员经理提成比例(totalDeductRate)和总销售额(totalAmount)。在SaleManager类中不包含其他数据成员。(2)各类人员的实发工资公式如下:员工实发工资=基本工资+奖金×出勤率销售员实发工资=基本工资+奖金×出勤率+个人销售额×销售员提成比例\n经理实发工资=基本工资+奖金×出勤率+总销售额×经理提成比例销售经理实发工资=基本工资+奖金×出勤率+个人销售额×销售员提成比例+总销售额×经理提成比例(3)每个类都有构造函数、输出基本信息函数(Output)和输出实发工资函数(OutputWage)。#include#includeusingnamespacestd;classStaff//基类{protected:intnum;//编号stringname;//姓名floatrateOfAttend;floatbasicSal;floatprize;public:Staff(){}Staff(intn1,stringname1,floatrate,floatbase,floatprize1){num=n1;name=name1;rateOfAttend=rate;basicSal=base;prize=prize1;}~Staff(){}virtualfloatWage(){returnbasicSal+prize*rateOfAttend;}voidOutputWage()//计算实发工资{cout<<"实发工资:"<Output();p[i]->OutputWage();}return0;}3.下面是一个数组类Carray的定义,请实现print()成员函数打印数组,并重载=,+和-运算符使之能对该数组类对象进行赋值和加减运算。请通过main函数验证它们。classCArray{private:int*p_arr;intsize;public:CArray();CArray(int*p_a,ints);CArray(constCArray&r_other);~CArray();int&operator[](intpos);CArrayoperator=(constCArray&r_other);CArrayoperator+(constCArray&r_other);CArrayoperator-(constCArray&r_other);voidprint()const;};CArray::CArray(){p_arr=0;size=0;}CArray::CArray(int*p_a,ints){if(s>0){size=s;p_arr=newint[size];for(inti=0;i=size){cout<<"outofrange"<>”,使之能用于该矩阵的输入和输出。classMatrix{public:Matrix();friendMatrixoperator+(Matrix&a,Matrix&b);friendostream&operator<<(ostream&,Matrix&);friendistream&operator>>(istream&,Matrix&);private:intmat[2][3];};Matrix::Matrix(){for(inti=0;i<2;i++)for(intj=0;j<3;j++)mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){Matrixc;for(inti=0;i<2;i++)for(intj=0;j<3;j++)c.mat[i][j]=a.mat[i][j]+b.mat[i][j];returnc;}istream&operator>>(istream&in,Matrix&m){\ncout<<"请输入数组的值:"<>m.mat[i][j];returnin;}ostream&operator<<(ostream&out,Matrix&m){cout<<"输出数组的值:"<>a;cin>>b;cout<<"Matrixa:"<#include#includeusingnamespacestd;classStudent{intno;stringname;intscore;\npublic:voidsetno(intno1){no=no1;}voidsetname(stringname1){name=name1;}voidsetscore(intscore1){score=score1;}intgetno(){returnno;}stringgetname(){returnname;}intgetscore(){returnscore;}voidpscore(intscore1){inttemp=score1/10;switch(temp){case10:case9:cout<<"优"<>no>>name>>score;stu[i].setno(no);stu[i].setname(name);stu[i].setscore(score);fout.write((char*)&stu[i],sizeof(Student));}fout.close();ifstreamfin("stud.dat",ios::binary);if(!fout){throwFileOpenException();exit(1);}fin.seekg(sizeof(Student)*2);fin.read((char*)&s,sizeof(Student));cout<