山东科技大学面向对象程序设计c++答案(共13页)_第1页
山东科技大学面向对象程序设计c++答案(共13页)_第2页
山东科技大学面向对象程序设计c++答案(共13页)_第3页
山东科技大学面向对象程序设计c++答案(共13页)_第4页
山东科技大学面向对象程序设计c++答案(共13页)_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上Problem A: 平面上的点Point类 (IV)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。根据“append.cc”,完成Point类的构造方法和show()、showCounter()、showSumOfPoint()方法;实现showPoint()函数。 接口描述:showPoint()函数:按输出格式输出Point对象,调用Point:show()方法实现。Point:show()方法:按输出格式输出Point对象。Point:showCounter()方

2、法:按格式输出当前程序中Point对象的计数。Point:showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。Output对每个Point对象,调用show()方法输出其值,或者用showPoint()函数来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。调用用showCounter()方法和showSumOfPoint()输出Point对象的计数统计,输出格式见sample

3、。C语言的输入输出被禁用。Sample Input1,23,32,1Sample OutputPoint : (1, 2)Current : 2 points.Point : (3, 3)Current : 2 points.Point : (2, 1)Current : 2 points.In total : 4 points.Current : 3 points.Point : (0, 0)Point : (1, 1)Point : (0, 0)In total : 6 points.#include<iostream>#include<iomanip>using

4、namespace std;class Pointprivate: double x_,y_; static int sum,num;public: Point():x_(0),y_(0)num+;sum+; Point(double xx):x_(xx),y_(1)num+;sum+; Point(double xx,double yy):x_(xx),y_(yy)num+;sum+; Point(const Point&p)x_=p.x_;y_=p.y_;num+;sum+; Point()num-; void show()cout<<setprecision(16)&

5、lt;<"Point : ("<<x_<<", "<<y_<<")"<<endl; static void showCounter()cout<<setprecision(16)<<"Current : "<<num<<" points."<<endl; static void showSumOfPoint()cout<<setprecision(16)<

6、;<"In total : "<<sum<<" points."<<endl;void showPoint(Point &a,Point &b,Point &c) a.show();b.show();c.show();int Point:sum=0;int Point:num=0;int main() char c; double a, b; Point q; while(std:cin>>a>>c>>b) Point p(a, b); p.show()

7、; p.showCounter(); q.showSumOfPoint(); Point q1(q), q2(1); Point:showCounter(); showPoint(q1, q2, q); Point:showSumOfPoint();Problem A: 复数类的构造、析构和输出Description封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作:1. CPLX:CPLX(double, double)构造:参数为实部、虚部,用传入的参数初始化对象,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部;2. CPLX:CPLX()析构:产生一行以“RE

8、LEASE()”开始的输出,并输出复数的实部和虚部;3. CPLX:print():产生一行以“PRINT()”开始的输出,并以格式“(a, bi)”的形式输出复数;-你设计一个CPLX类,使得main()函数能够运行并得到正确的输出。调用格式见append.ccInput输入的第一个整数n,表示用n组测试样例。每组测试输入两个实数,分别为实部和虚部。Output每组测试数据对应一组输出,两组输出之间用若干“=”分割,详细格式见sample。Sample Input52 310 00 1001 -1-7 -7Sample Output=CREATE(): 2 3PRINT(): (2, 3i)

9、RELEASE(): 2 3=CREATE(): 10 0PRINT(): (10, 0i)RELEASE(): 10 0=CREATE(): 0 100PRINT(): (0, 100i)RELEASE(): 0 100=CREATE(): 1 -1PRINT(): (1, -1i)RELEASE(): 1 -1=CREATE(): -7 -7PRINT(): (-7, -7i)RELEASE(): -7 -7=#include<iostream>#include<iomanip>using namespace std;class CPLXprivate: doubl

10、e x,y;public : CPLX(double xx,double yy) x=xx;y=yy; cout<<"CREATE(): "<<x<<" "<<y<<endl; CPLX() cout<<"RELEASE(): "<<x<<" "<<y<<endl; void print() cout<<"PRINT(): ("<<x<<&q

11、uot;, "<<y<<"i)"<<endl; ;int main() int cases; cin >> cases; for(int i = 1; i <= cases; +i) double a, b; cin >> a >> b; cout << "=" << endl; CPLX cplx(a, b); cplx.print(); cout << "=" << endl; Problem B

12、: 复数类的成员访问Description封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作:1. CPLX:CPLX()构造:初始化一个实部、虚部均为0的复数,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部;2. CPLX:CPLX()析构:产生一行以“RELEASE()”开始的输出,并输出复数的实部和虚部;3. CPLX:real():返回实部;   CPLX:imag():返回虚部。4. CPLX:real(double):传参修改实部;   CPLX:imag(double):传参修改虚部;-你设计一个CPLX类,使得

13、main()函数能够运行并得到正确的输出。调用格式见append.ccInput输入的第一个整数n,表示用n组测试样例。每组测试输入两个实数,分别为实部和虚部。Output每组测试数据对应一组输出,两组输出之间用若干“=”分割,详细格式见sample。Sample Input52 310 00 1001 -1-7 -7Sample Output=CREATE(): 0 0Complex real part is 2, imaginary part is 3.RELEASE(): 2 3=CREATE(): 0 0Complex real part is 10, imaginary part i

14、s 0.RELEASE(): 10 0=CREATE(): 0 0Complex real part is 0, imaginary part is 100.RELEASE(): 0 100=CREATE(): 0 0Complex real part is 1, imaginary part is -1.RELEASE(): 1 -1=CREATE(): 0 0Complex real part is -7, imaginary part is -7.RELEASE(): -7 -7=#include<iostream>#include<iomanip>using n

15、amespace std;class CPLXprivate: double x,y;public : CPLX(double xx=0,double yy=0)this->x=xx;this->y=yy; cout<<"CREATE(): "<<x<<" "<<y<<endl; CPLX() cout<<"RELEASE(): "<<x<<" "<<y<<endl; double r

16、eal() return x; double imag()return y; CPLX &real(double xx); CPLX &imag(double yy); ; CPLX& CPLX:real(double xx) x=xx; return *this; CPLX& CPLX:imag(double yy) y=yy; return *this; int main() int cases; cin >> cases; for(int i = 1; i <= cases; +i) double a, b; cin >> a

17、 >> b; cout << "=" << endl; CPLX cplx; cplx.real(a); cplx.imag(b); cout << "Complex real part is " << cplx.real() <<", imaginary part is " << cplx.imag() << "." << endl; cout << "=" <<

18、 endl;Problem A: 立体空间中的点(I)Description设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。设计Point类需支持一下操作:Point:Point()无参构造。Point:Point(double,double)两个坐标参数构造。Point:showPoint()按格式输出Point对象设计Point_3D类需支持一下操作:Point_3D:Point_3D()无参构造。Point_3D:Point_3D(double,double,double)三个坐标参数构造

19、。Point_3D:showPoint()按格式输出Point_3D对象。-你设计Point类和Point_3D类,使得main()函数能够正确运行。函数调用格式见append.cc。append.cc中已给出main()函数。Input输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据。每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)。Output每组测试数据对应一行输出。若输入为平面上的点,则输出:“2D Point (x,

20、y)”,x和y为输入的坐标值。若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值。Sample Input53 1 2 33 0 0 02 -1 13 -1 -1 -12 0 0Sample Output3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)#include<iostream>using namespace std;class Pointprotected:int x,y;public:Point():x(0),y(0)Po

21、int(double x1,double y1):x(x1),y(y1) void showPoint()cout<<"2D Point ("<<x<<","<<y<<")"<<endl;class Point_3D:public Point /Point_3D从Point中派生protected:int z;public:Point_3D():z(0)Point_3D(double x1,double y1,double z1):Point(x1,y1),z(z

22、1)void showPoint()cout<<"3D Point ("<<x<<","<<y<<","<<z<<")"<<endl; int main() int cases; cin>>cases; for(int i = 1; i <= cases; i+) double x, y, z; int point_type; cin>>point_type; if(point_type =

23、 2) cin>>x>>y; Point p(x, y); p.showPoint(); if(point_type = 3) cin>>x>>y>>z; Point_3D p(x, y, z); p.showPoint(); Problem A: 立体空间中的点(IIDescription设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。并统计输入的两种点的个数。设计Point类需支持一下操作:Point:Point()无参构造。Poi

24、nt:Point(double,double)两个坐标参数构造。Point:x()返回x坐标Point:y()返回y坐标Point:x(int)修改x坐标并返回Point:y(int)修改y坐标并返回Point:showPoint()按格式输出Point对象Point:showNumber()返回Point对象总数的静态函数设计Point_3D类需支持一下操作:Point_3D:Point_3D()无参构造。Point_3D:Point_3D(double,double,double)三个坐标参数构造。Point_3D:z()返回z坐标。Point_3D:z(int)修改z坐标并返回。Poin

25、t_3D:showPoint()按格式输出Point_3D对象。Point_3D:setPoint(double,double,double)根据三个坐标参数修改Point_3D对象的坐标。Point_3D:showNumber()返回Point_3D对象总数的静态函数。-你设计Point类和Point_3D类,使得main()函数能够正确运行。函数调用格式见append.cc。append.cc中已给出main()函数。Input输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据。每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示

26、一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)。Output开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况。输出“Test data output :”之后为测试数据对应的输出:每组测试数据对应一行输出。若输入为平面上的点,则输出:“2D Point (x,y)”,x和y为输入的坐标值。若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值。最后,分别输出总共输入的平面上的点数和3维的点数。Sample Input53 1 2 33 0 0 02 -1 13 -1 -1 -

27、12 0 0Sample OutputInvariable test output :3D Point (-100,0,100)Point (0,100,100)Test data output :3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)Number of 2D Points : 2Number of 3D Points : 3#include<iostream>using namespace std;class Pointprotected:int X,Y;st

28、atic int n; static int m; /n,m被初始化为0public:Point():X(0),Y(0)n+; /无参构造Point(double x1,double y1):X(x1),Y(y1)n+;int x()return X;int y()return Y;int x(int x1)X=x1;return X;int y(int y1)Y=y1;return Y;static int showNumber()return n;void showPoint()cout<<"2D Point ("<<X<<"

29、;,"<<Y<<")"<<endl; class Point_3D:public Point /Point_3D从Point派生protected:int Z;public:Point_3D():Z(0)m+;Point_3D(double xx,double yy,double zz):Point(xx,yy),Z(zz)m+; /构造函数初始化列表int z()return Z;int z(int zz)return Z;void showPoint()cout<<"3D Point ("&l

30、t;<X<<","<<Y<<","<<Z<<")"<<endl;void setPoint(double xx,double yy,double zz)X=xx;Y=yy;Z=zz;static int showNumber()return m;int Point:n=0;int Point:m=0;int main() cout<<"Invariable test output :"<<endl; Point_3

31、D p3d; p3d.setPoint(-100, 0, 100); p3d.showPoint(); p3d.x(0); p3d.y(100); cout<<"Point ("<<p3d.x()<<","<<p3d.y()<<","<<p3d.z()<<")"<<endl; cout<<"nTest data output :"<<endl; int cases; cin&g

32、t;>cases; for(int i = 1; i <= cases; i+) double x, y, z; int point_type; cin>>point_type; if(point_type = 2) cin>>x>>y; Point p(x, y); p.showPoint(); if(point_type = 3) cin>>x>>y>>z; Point_3D p(x, y, z); p.showPoint(); cout<<"Number of 2D Points :

33、 "<<Point:showNumber() - Point_3D:showNumber()<<endl; cout<<"Number of 3D Points : "<<Point_3D:showNumber() - 1<<endl;Problem B: 还会用继承吗?Description定义一个Base类,包括1个int类型的属性,以及满足输出格式要求的构造函数、拷贝构造函数和析构函数。定义Base类的子类Derived,包括1个int类型的属性, 以及满足输出格式要求的构造函数、拷贝构造函数和析构

34、函数。Input第1行N>0表示测试用例个数。每个测试包括2个int类型的整数。Output见样例。Sample Input110 20Sample OutputBase = 10 is created.Base = 10 is copied.Base = 10 is created.Derived = 20 is created.Base = 10 is copied.Derived = 20 is copied.Derived = 20 is erased.Base = 10 is erased.Derived = 20 is erased.Base = 10 is erased.B

35、ase = 10 is erased.Base = 10 is erased.#include<iostream>using namespace std;class Basepublic: int a; Base(int t):a(t)cout<<"Base = "<<a<<" is created."<<endl; /拷贝构造函数 Base()cout<<"Base = "<<a<<" is erased."<&

36、lt;endl; /析构函数 Base(const Base& b)a=b.a;cout<<"Base = "<<a<<" is copied."<<endl;class Derived: public Base /Derived从Base中派生public: int b; Derived(int t):Base(t)cout<<"Derived = "<<b<<" is created."<<endl; Der

37、ived()cout<<"Derived = "<<b<<" is erased."<<endl; /析构函数 Derived(const Derived& d):Base(d),b(d.b)cout<<"Derived = "<<b<<" is copied."<<endl; Derived(int x,int y):Base(x),b(y)cout<<"Derived = "&

38、lt;<b<<" is created."<<endl;int main() int cases, data1, data2; cin>>cases; for (int i = 0; i < cases; i+) cin>>data1>>data2; Base base1(data1), base2(base1); Derived derived1(data1, data2), derived2(derived1); Problem A: 动物类-抽象类Description每种动物都有自己的叫声,如狗

39、的叫声是"汪汪汪",猫的叫声是"喵喵喵",老鼠的叫声是"吱吱吱"。构造类Animal,Dog,Cat,Mouse,他们都有成员数据name和sex,表示名字和性别。一个成员函数cry(),输出他们的叫声,在main函数中采用多态性调用他们。Input动物的姓名和性别Output动物的信息Sample InputJerry mJemmy fTom mDroopy mSample Output我叫Jerry,是一只男老鼠,我的叫声是:吱吱吱!我叫Jemmy,是一只女老鼠,我的叫声是:吱吱吱!我叫Tom,是一只男猫,我的叫声是:喵喵喵!我叫

40、Droopy,是一条男狗,我的叫声是:汪汪汪!#include<iostream>using namespace std;class Animalpublic: string name; char sex; virtual void cry()=0;/纯虚函数;/分号必不可少class Mouse: public Animalpublic: string name; char sex; Mouse(string n,char m)name=n;sex=m;if(sex='m') cout<<"我叫"<<name<&l

41、t;",是一只男老鼠," else cout<<"我叫"<<name<<",是一只女老鼠," void cry()cout<<"我的叫声是:吱吱吱!"<<endl;/分号必不可少class Dog: public Animalpublic: string name; char sex; Dog(string n,char m)name=n;sex=m;if(sex='m') cout<<"我叫"<<

42、;name<<",是一条男狗," else cout<<"我叫"<<name<<",是一条女狗," void cry()cout<<"我的叫声是:汪汪汪!"<<endl; /分号必不可少class Cat: public Animalpublic: string name; char sex; Cat(string n,char m)name=n;sex=m;if(sex='m') cout<<"我叫&qu

43、ot;<<name<<",是一只男猫," else cout<<"我叫"<<name<<",是一只女猫," void cry()cout<<"我的叫声是:喵喵喵!"<<endl;/分号必不可少int main( ) string nam; char s; cin>>nam>>s; Animal *p; Mouse m1(nam, s); p=&m1; p->cry(); cin>>n

44、am>>s; Mouse m2(nam, s); p=&m2; p->cry(); cin>>nam>>s; Cat c1(nam, s); p=&c1; p->cry(); cin>>nam>>s; Dog d1(nam, s); p=&d1; p->cry(); return 0;Problem B: 平面上的点Point类 (II)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。根据“appe

45、nd.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序。接口描述:Point:show()方法:按输出格式输出Point对象。Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。Output输出每个Point对象的构造和析构行为。对每个Point对象,调用show()方法输出其值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。输出格式见sample。C语言的输入输出被禁用。Sample Input1,23,32,1Sample OutputPoint : (0,

46、 0) is created.Point : (1, 2) is created.Point : (1, 2)Point : (1, 2) is erased.Point : (3, 3) is created.Point : (3, 3)Point : (3, 3) is erased.Point : (2, 1) is created.Point : (2, 1)Point : (2, 1) is erased.Point : (0, 0) is copied.Point : (1, 1) is created.Point : (0, 0)Point : (1, 1)Point : (0,

47、 0)Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.#include<iostream>#include<iomanip>using namespace std;class Point private: double x_,y_;public:Point(double xx,double yy) x_=xx; y_=yy; cout<<"Point : ("<<setprecision(16)<<x_<&l

48、t;", "<<setprecision(16)<<y_<<")"<<" is created."<<endl;Point(double xx=0) x_=y_=xx; cout<<"Point : ("<<setprecision(16)<<x_<<", "<<setprecision(16)<<y_<<")"<<&qu

49、ot; is created."<<endl;void show() cout<<"Point : ("<<setprecision(16)<<x_<<", "<<setprecision(16)<<y_<<")"<<endl;Point(Point &p) x_=p.x_;y_=p.y_; cout<<"Point : ("<<setprecision(16)<

50、;<x_<<", "<<setprecision(16)<<y_<<")"<<" is copied."<<endl;Point();Point:Point() cout<<"Point : ("<<setprecision(16)<<x_<<", "<<setprecision(16)<<y_<<")"<&l

51、t;" is erased."<<endl; ;int main() char c; double a, b; Point q; while(std:cin>>a>>c>>b) Point p(a, b); p.show(); Point q1(q), q2(1); q1.show(); q2.show(); q.show();Problem C: 平面上的点Point类 (III)Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。根

52、据“append.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序。实现showPoint()函数。接口描述:showPoint()函数按输出格式输出Point对象,调用Point:show()方法实现。Point:show()方法:按输出格式输出Point对象。Input输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。Output输出每个Point对象的构造和析构行为。showPoint()函数用来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输

53、出精度为最长16位。输出格式见sample。C语言的输入输出被禁用。Sample Input1,23,32,1Sample OutputPoint : (0, 0) is created.Point : (1, 2) is created.Point : (1, 2) is copied.Point : (1, 2)Point : (1, 2) is erased.Point : (1, 2) is erased.Point : (3, 3) is created.Point : (3, 3) is copied.Point : (3, 3)Point : (3, 3) is erased.P

54、oint : (3, 3) is erased.Point : (2, 1) is created.Point : (2, 1) is copied.Point : (2, 1)Point : (2, 1) is erased.Point : (2, 1) is erased.Point : (0, 0) is copied.Point : (1, 1) is created.Point : (0, 0) is copied.Point : (1, 1) is copied.Point : (0, 0) is copied.Point : (0, 0)Point : (1, 1)Point :

55、 (0, 0)Point : (0, 0) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.#include<iostream>#include<iomanip>using namespace std;class Pointprivate: double x,y;public: Point(double xx=0) x=y=xx; cout<&l

56、t;"Point : ("<<setprecision(16)<<x<<", "<<setprecision(16)<<y<<")"<<" is created."<<endl; Point(double,double);void show() cout<<"Point : ("<<setprecision(16)<<x<<", "<<setprecision(16)<<y<<")"<<endl; Point(Point &p) x=p.x;y=p.y; cout<<"Point : ("<<setprecision(16)<<x<<", &

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论