1、 /类的结束int main() /测试函数 Test x; x.initx(30,20); x.printx(); return 0;得到的测试结果:4.定义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle(): 累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft(): 设置矩形的左上角坐标SetLength(),SetWidth(): 设置矩形的长和宽Perimeter(): 求矩形的周长Area(): 求矩形的面积GetWidth(): 返回矩形的宽度Getlength(): 返回矩形的长度IsSquare(): 判断矩形是否为正
2、方形Move(): 将矩形从一个位置移动到另一个位置Size(): 改变矩形的大小Where(): 返回矩形的左上角的坐标PrintRectangle(): 输出矩形的四个顶点的坐标数据成员int top,left;int length,width;class Crectangle /类的开始 int top,left; int length,width; Crectangle(int 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 w
3、idth=0; void SetTop(int t) void SetLeft(int l) left=l; void SetLength(int len) length=len; void SetWidth(int w) width=w; int Perimeter() return 2*(width+length); int Area() return width*length; int GetWidth() return width; int GetLength() return length; int IsSquare() if(width=length) cout该矩形是正方形 re
4、turn 1; else cout该矩形不是正方形 return 0; void Move(int x,int y) left+=x; top+=y; void Size(int l,int w) length=l; width=w; void PrintRectangle()左上方的点为:(left,top)右上方的点为:left+length左下方的点为:top+width右下方的点为: /类的结束int main() Crectangle a(1,1,5,5); a.PrintRectangle(); a.SetTop(2); a.SetLeft(3); a.SetLength(4);
5、a.SetWidth(5); a.IsSquare();周长为:a.Perimeter()面积为:a.Area()用getwidth等函数得到的值为: 长为:a.GetLength 宽为:a.GetWidth6.某次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum名,现为比赛积分编写一个CompetitionResult类,类的定义如下: (定义略)(1)写出所有的成员函数和实现代码。(2)编写main()函数对该类进行测试。在函数体中,定义了一个competitionResult类的对象数组rPlauerNum,他的每个元素记录了每个选手的所有信息,个评委的
6、打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。(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);float CompetitionResult:MaxScore() int max=score0; if(maxscorei) min
7、=scorei; return min;void CompetitionResult:SetAvg() int i;float sum=0.0; for(i=0; sum+=scorei; sum=sum-MaxScore()-MinScore(); average=sum/(JudgeNum-2);GetAvg() return average;short CompetitionResult:GetNo(int i) return num;SetNo(int i) num=i;char CompetitionResult:* GetName() return name ;GetScore(i
8、nt j) return scorej;Setscore(int k,float av) scorek=av;void Sort(CompetitionResult *pr,int n) int i,j ,k; CompetitionResult temp;n-1; k=i; for(j=i+1;j(prk.average) k=j; if(k!=j) temp=pri; pri=prk; prk=temp; (2) CompetitionResult playerPlayerNum=CompetitionResult(1,one), CompetitionResult(2,two),Comp
9、etitionResult(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;PlayerNum;请评委给第i+1a; playeri.Setscore(j,a); playeri.SetAvg(); Sort(player,PlayerNum); 最后的
10、得分情况为: for (int j=0; coutj+1;setw(6)playerj.GetAvg();playerj.GetName()程序运行的结果为:第九章1. 在下面的程序中,派生类Derived 的成员函数sets()能否访问基类base中得变量a和b?为什么?如果在基类base中将数据成员a和b定义为私有成员,下面的程序能否通过编译?class Base void set(int i,int j) a=i;b=j; void show() couta=a,b=bprotected: int a,b;class Derived:public Base void sets() s=a
11、*b; void shows() couts=s int s;int main() Derived obj; obj.set(2,3); obj.show(); obj.sets(); obj.shows();可以访问a和b;因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived 的成员函数sets()能访问基类base中得变量a和b。如果定义为私有,则不能通过编译。因为派生类中的成员函数不能访问基类中得私有成员2声明一个图形基类Shape,在它的基础上派生出矩形类Rectangle和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在Rect
12、angle类的基础上派生方形类Square。编写程序和各类的定义和实现,以及类的使用。 #include class Shape double getArea() double getPerimeter() class Rectangle:public Shape double height; double width; Rectangle() Rectangle(double a,double b) height=a; width=b; double getArea() return height*width; double getPerimeter() return 2*(height+w
13、idth);class Circle:public Shape Circle(double x):r(x) return 2*3.1416*r; return r*r*3.1416; double r;class Square:public Rectangle Square(double x) height=x; width=x; Rectangle a1(2.2,3.2); Circle a2(4.2); Square a3(5.3);Area=a1.getArea()Perimeter=a1.getPerimeter()a2.getArea()a2.getPerimeter()a3.get
14、Area()a3.getPerimeter()5.某销售公司有销售经理和销售员工,月工资的计算办法为:销售激励的底薪为4000元,并将销售额的2/1000做提成,销售员工无底薪,只提取5/1000为工资。编写程序:(1)定义一个积累Employee,它包含三个数据成员number,name和salary,以及编号和姓名的构造函数。(2)有Employee类派生Salesman类。Salesman类包含两个新数据成员commrate和sales,还包括了用于输入销售额并计算销售员工工资的成员函数pay()和用于输出打印的print()。(3)由Salesman派生Salemanager,Sale
15、smmanage类包含新数据成员monthlypay,以及用于输入销售额并计算销售经理工资的成员函数pay()和用于输出的print()。(4)编写main()函数中测试所设计的类结构。(1)class Employee Employee(long num,char *n) number=num; strcpy(name,n); long number; char name20; double salary;(2)class Salesman:public Employee Salesman(long num,char *n,float c=0.005):Employee(num,n) com
16、mrate=c; void pay()namesales; salary=commrate*sales; void print()salary float commrate; double sales;(3) class Salesmanager:public Salesman Salesmanager(long num,char *n,float c=0.002,double m=4000):Salesman(num,n,c) monthlypay=m; void pay() salary=monthlypay+commrate*sales; double monthlypay;(4) in
17、t main() Salesman s1(1234, Salesmanager s2(1357, s1.pay(); s1.print(); s2.pay(); s2.print();第十章1.定义一个辅助类Complex,重载运算符+、-、*、/使之能用于复数的加减乘除运算。运算符重载函数为Complex类的成员函数。编写程序,分别求出两个复数之和、差、积、商。class Complex Complex(double r=0.0,double i=0.0) real=r;imag=i; Complex operator + (Complex); friend Complex operator
18、 - (Complex,Complex); Complex operator * (Complex); friend Complex operator / (Complex,Complex); void display(); 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);
19、operator * (Complex c) return Complex(real*c.real-imag+c.imag,imag*c.imag+real*c.real);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
20、 Complex:display()realimag Complex c1(7.1,8.4),c2(1.4,5),c3;c1= c1.display();c2= c2.display(); c3=c1+c2;c1+c2=c3.display(); c3=c1-c2;c1-c2= c3=c1*c2;c1*c2= c3=c1/c2;c1/c2=2.定义一个日期类Date,包括年,月,日等私有数据成员。要求为所定义的Date类设计如下重载运算符函数:Date operator+(int days); 返回一日起加天数days后得到的日期Date operator-(int days); 返回一日起减
21、天数days后得到的日期Int operator-(Date &b); 返回两日期相差的天数int day_tab12=31,28,31,30,31,30,31,31,30,31,30,31;class Date Date() Date(int y,int m,int d) year=y;mon=m;day=d; void setday(int d) day=d; void setmon(int m) mon=m; void setyear(int y) year=y; int getday() return day; int getmon() return mon; int getyear() return year; Date operator + (int days) static
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1