2022年C++程序设计习题 .pdf
《2022年C++程序设计习题 .pdf》由会员分享,可在线阅读,更多相关《2022年C++程序设计习题 .pdf(28页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C+程序设计练习题学院:计算机学院专业班级:物联网1002 学号:0121010340705 姓名:名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 28 页 -第八章1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.Int main()Test x;x.initx(30,20);x.printx();return 0;解:#include using namespace std;class Test/类的开始 public:void initx(int i,int j);void printx();private:int x,y;void Test:initx(int
2、i,int j)x=i;y=j;void Test:printx()coutx*y=x*yendl;/类的结束int main()/测试函数 Test x;x.initx(30,20);x.printx();return 0;得到的测试结果:名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 28 页 -4.定义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle():累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft():设置矩形的左上角坐标SetLength(),SetWidth():设置矩形的长和宽Perimeter():求
3、矩形的周长Area():求矩形的面积GetWidth():返回矩形的宽度Getlength():返回矩形的长度IsSquare():判断矩形是否为正方形Move():将矩形从一个位置移动到另一个位置Size():改变矩形的大小Where():返回矩形的左上角的坐标PrintRectangle():输出矩形的四个顶点的坐标数据成员int top,left;int length,width;解:#include using namespace std;class Crectangle/类的开始 int top,left;int length,width;public:Crectangle(int
4、t=0,int l=0,int len=1,int w=1)top=t;left=l;if(len0)length=len;else length=0;if(w0)width=w;else width=0;void SetTop(int t)top=t;void SetLeft(int l)left=l;名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 28 页 -void SetLength(int len)length=len;void SetWidth(int w)width=w;int Perimeter()return 2*(width+length);int Area(
5、)return width*length;int GetWidth()return width;int GetLength()return length;int IsSquare()if(width=length)cout该矩形是正方形endl;return 1;else cout该矩形不是正方形endl;return 0;void Move(int x,int y)left+=x;top+=y;void Size(int l,int w)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 28 页 -length=l;width=w;void PrintRectangle()cou
6、t左上方的点为:(left,top)endl;cout右上方的点为:(left+length,top)endl;cout左下方的点为:(left,top+width)endl;cout右下方的点为:(left+length,top+width)endl;/类的结束int main()Crectangle a(1,1,5,5);a.PrintRectangle();a.SetTop(2);a.SetLeft(3);a.SetLength(4);a.SetWidth(5);a.IsSquare();cout周长为:a.Perimeter()endl;cout面积为:a.Area()endl;a.P
7、rintRectangle();cout用 getwidth 等函数得到的值为:endl;cout 长为:a.GetLengthendl;cout 宽为:a.GetWidthendl;return 0;得到的测试结果:名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 28 页 -6.某次歌手大赛中,有JudgeNum 个评委给选手打分,参加比赛的选手有PlayerNum 名,现为比赛积分编写一个CompetitionResult 类,类的定义如下:(定义略)(1)写出所有的成员函数和实现代码。(2)编 写 main()函 数 对该 类 进 行 测 试。在 函 数 体 中,定 义 了
8、 一 个competitionResult 类的对象数组 rPlauerNum,他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。解:(1)CompetitionResult:CompetitionResult()num=0;strcpy(name,);for(int i=0;iJudgeNum;i+)scorei=0.0;average=0;CompetitionResult:CompetitionResult(short n,char*ps)num=n;strcpy(name,ps);for(int i
9、=0;iJudgeNum;i+)scorei=0.0;average=0;float CompetitionResult:MaxScore()int max=score0;for(int i=0;iJudgeNum;i+)if(maxscorei)max=scorei;return max;float CompetitionResult:MinScore()int min=score0;for(int i=0;iscorei)min=scorei;return min;void CompetitionResult:SetAvg()int i;float sum=0.0;for(i=0;iJud
10、geNum;i+)sum+=scorei;sum=sum-MaxScore()-MinScore();average=sum/(JudgeNum-2);名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 28 页 -float CompetitionResult:GetAvg()return average;short CompetitionResult:GetNo(int i)return num;void CompetitionResult:SetNo(int i)num=i;char CompetitionResult:*GetName()return name;float C
11、ompetitionResult:GetScore(int j)return scorej;void CompetitionResult:Setscore(int k,float av)scorek=av;void Sort(CompetitionResult*pr,int n)int i,j,k;CompetitionResult temp;for(i=0;in-1;i+)k=i;for(j=i+1;j(prk.average)k=j;if(k!=j)temp=pri;pri=prk;prk=temp;(2)int main()CompetitionResult playerPlayerNu
12、m=CompetitionResult(1,one),CompetitionResult(2,two),CompetitionResult(3,there),CompetitionResult(4,four),CompetitionResult(5,five),CompetitionResult(6,six),CompetitionResult(7,seven),CompetitionResult(8,eight),CompetitionResult(9,nine),CompetitionResult(10,ten);float a;for(int i=0;iPlayerNum;i+)cout
13、请评委给第 i+1个选手打分:endl;for(int j=0;ja;playeri.Setscore(j,a);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 28 页 -playeri.SetAvg();coutendl;Sort(player,PlayerNum);cout 最后的得分情况为:endl;for(int j=0;jPlayerNum;j+)coutj+1;coutsetw(6)playerj.GetAvg();cout playerj.GetName()endl;return 0;程序运行的结果为:名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共
14、28 页 -第九章1.在下面的程序中,派生类Derived 的成员函数sets()能否访问基类base 中得变量a 和b?为什么?如果在基类base 中将数据成员a 和 b 定义为私有成员,下面的程序能否通过编译?为什么?#include using namespace std;class Base public:void set(int i,int j)a=i;b=j;void show()couta=a,b=bendl;protected:int a,b;class Derived:public Base public:void sets()s=a*b;void shows()couts=
15、sendl;private:int s;int main()Derived obj;obj.set(2,3);obj.show();obj.sets();obj.shows();return 0;解:可以访问a 和 b;因为 a,b 为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived 的成员函数sets()能访问基类base 中得变量a 和 b。如果定义为私有,则不能通过编译。因为派生类中的成员函数不能访问基类中得私有成员2声明一个图形基类Shape,在它的基础上派生出矩形类Rectangle 和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在R
16、ectangle 类的基础上派生方形类Square。编写程序和各类的定义和实现,以及类的使用。解:#include using namespace std;class Shape public:double getArea()double getPerimeter();名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 28 页 -class Rectangle:public Shape protected:double height;double width;public:Rectangle()Rectangle(double a,double b)height=a;width=b
17、;double getArea()return height*width;double getPerimeter()return 2*(height+width);class Circle:public Shape public:Circle(double x):r(x)double getPerimeter()return 2*3.1416*r;double getArea()return r*r*3.1416;private:double r;class Square:public Rectangle public:Square(double x)height=x;width=x;int
18、main()名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 28 页 -Rectangle a1(2.2,3.2);Circle a2(4.2);Square a3(5.3);coutArea=a1.getArea()endl;coutPerimeter=a1.getPerimeter()endl;coutArea=a2.getArea()endl;coutPerimeter=a2.getPerimeter()endl;coutArea=a3.getArea()endl;coutPerimeter=a3.getPerimeter()endl;return 0;5.某销售公司有销
19、售经理和销售员工,月工资的计算办法为:销售激励的底薪为4000 元,并将销售额的2/1000 做提成,销售员工无底薪,只提取5/1000 为工资。编写程序:(1)定义一个积累Employee,它包含三个数据成员number,name 和 salary,以及编号和姓名的构造函数。(2)有 Employee 类派生 Salesman 类。Salesman 类包含两个新数据成员commrate 和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。(3)由 Salesman 派生 Salemanager,Salesmmanage类包含新数据成员mo
20、nthlypay,以及用于输入销售额并计算销售经理工资的成员函数pay()和用于输出的print()。(4)编写 main()函数中测试所设计的类结构。解:(1)class Employee public:Employee(long num,char*n)number=num;strcpy(name,n);protected:long number;char name20;double salary;名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 28 页 -;(2)class Salesman:public Employee public:Salesman(long num,
21、char*n,float c=0.005):Employee(num,n)commrate=c;void pay()cout?namesales;salary=commrate*sales;void print()coutname?1?a osalaryendl;protected:float commrate;double sales;(3)class Salesmanager:public Salesman public:Salesmanager(long num,char*n,float c=0.002,double m=4000):Salesman(num,n,c)monthlypay
22、=m;void pay()cout?namesales;salary=monthlypay+commrate*sales;void print()coutname?1?a osalaryendl;private:double monthlypay;(4)int main()Salesman s1(1234,?e);Salesmanager s2(1357,?);s1.pay();s1.print();s2.pay();s2.print();名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 28 页 -return 0;第十章1.定义一个辅助类Complex,重载运算符+、-、*、
23、/使之能用于复数的加减乘除运算。运算符重载函数为Complex 类的成员函数。编写程序,分别求出两个复数之和、差、积、商。解:#include using namespace std;class Complex public:Complex(double r=0.0,double i=0.0)real=r;imag=i;Complex operator+(Complex);friend Complex operator-(Complex,Complex);Complex operator*(Complex);friend Complex operator/(Complex,Complex);v
24、oid display();private:double real,imag;Complex Complex:operator+(Complex c)return Complex(real+c.real,imag+c.imag);Complex operator-(Complex c1,Complex c2)return Complex(c1.real-c2.real,c1.imag-c2.imag);Complex Complex:operator*(Complex c)return Complex(real*c.real-imag+c.imag,imag*c.imag+real*c.rea
25、l);名师资料总结-精品资料欢迎下载-名师精心整理-第 13 页,共 28 页 -Complex operator/(Complex c1,Complex c2)double r,i;r=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);i=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return(r,i);void Complex:display()cout(real,imag)endl;int main()Comp
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年C+程序设计习题 2022 C+ 程序设计 习题
限制150内