合肥工业大学宣城校区程序设计基础周波实验十仅供参考.docx
- 文档编号:10766447
- 上传时间:2023-02-22
- 格式:DOCX
- 页数:36
- 大小:101.19KB
合肥工业大学宣城校区程序设计基础周波实验十仅供参考.docx
《合肥工业大学宣城校区程序设计基础周波实验十仅供参考.docx》由会员分享,可在线阅读,更多相关《合肥工业大学宣城校区程序设计基础周波实验十仅供参考.docx(36页珍藏版)》请在冰豆网上搜索。
合肥工业大学宣城校区程序设计基础周波实验十仅供参考
实验十
1实验目的:
掌握面向对象编程中继承、派生的用法。
2实验题目:
(1)编写一个集合类,要求类的每个对象可以保存0~100个不同的整数,并使用友元函数实现如下功能:
从集合中加入一个整数
从集合中去掉一个整数
判断一个整数是否在集合中
求两个集合的并集,结果是一个集合
求两个集合的交集,结果是一个集合
(2)下面是一个形状类Shape,编写Shape的派生类:
圆类Circle、三角形类Triangle和矩形类Rectangle,并重定义基类的成员函数使之返回正确的结果(show函数要输出对象的基本信息),然后编写程序进行测试。
classShape
{public:
doublearea()
{return0;};
doublegirth()
{return0;};
voidshow()
{cout<<”ShapeObject”< private: }; (3)编写一个笔类,描述所有笔的共同属性,然后编写笔类的派生类: 钢笔、铅笔、签字笔、毛笔,在各派生类中尽量描述清楚各自的属性。 (4)自学完成: 定义复数类,重载“+”“-”“=”运算符。 3实验结果截图 图片1题目 (1)程序运行截图 图片2题目 (2)程序运行截图 图片3题目(3)程序运行截图 图片4题目(4)程序运行截图 4源代码 //题目 (1)源代码: //_set.h #ifndef_SET_H_INCLUDED #define_SET_H_INCLUDED #include #include #include class_set { friend_add(_set&,int); friend_del(_set&,int); friendbool_judge(_set&,int); //friend_set&_union(_set&,_set&);//求并集 //friend_set&_intersection(_set&,_set&); friend_set_union(_set&,_set&);//求并集 friend_set_intersection(_set&,_set&);//求交集 public: _set(int); _set(); ~_set(); display(_set&); private: int*p; int_size;//sizeofnumber }; #endif//_SET_H_INCLUDED //_set.cpp: implementationofthe_setclass. // ////////////////////////////////////////////////////////////////////// #include"_set.h" ////////////////////////////////////////////////////////////////////// //Construction/Destruction ////////////////////////////////////////////////////////////////////// _set: : _set() { } _add(_set&s,intn) { s.p[s._size++]=n; } _del(_set&s,intn) { printf("Afterdeleted%d,theelementsoftheoperatedsetinclude: \n",n); intindex; for(inti=0;i if(s.p[i]==n) index=i; for(intj=index;j { s.p[j]=s.p[j+1]; } s._size--; } bool_judge(_set&s,intn) { for(inti=0;i if(s.p[i]==n) { printf("%disintheoperatedset.\n",n); returntrue; } printf("%disnotintheoperatedset.\n",n); returnfalse; } _set_union(_set&s1,_set&s2)//求并集 //_set_union(_set&s1,_set&s2)//求并集 { _sets3(s1._size+s2._size); for(inti=0;i s3.p[s3._size++]=s1.p[i]; for(intj=0;j { intindex=0; for(;index if(s3.p[index]==s2.p[j]) //continue; break; if(index==s1._size) { s3.p[s3._size++]=s2.p[j]; } } std: : cout<<"Theunionofsets: "< : endl; returns3; } _set_intersection(_set&s1,_set&s2) //_set_intersection(_set&s1,_set&s2) { _sets3(s1._size+s2._size); intbigger; if(s1._size>s2._size) bigger=s1._size; else bigger=s2._size; for(inti=0;i { for(intj=0;j { if(s1.p[i]==s2.p[j]) s3.p[s3._size++]=s1.p[i]; } } std: : cout<<"Theintersectionofsets: "< : endl; returns3; } _set: : ~_set() { delete[]p; std: : cout<<"Thedestructionhasbeencalled"< : endl; } _set: : _set(intn) { p=newint[n]; _size=0; } _set: : display(_set&s) { for(inti=0;i { std: : cout< if((i+1)%4==0) std: : cout< : endl; } } //test.cpp #include"_set.h" intmain()//测了几次感觉没毛病 { intn; std: : cin>>n; _sets1(n),s2(n); for(inti=0;i { _add(s1,i); _add(s2,i+3); } std: : cout<<"s1: \n"; s1.display(s1); std: : cout< : endl; std: : cout< : endl; std: : cout<<"s2: \n"; s2.display(s2); std: : cout<<"\n\n"; _sets3=_union(s1,s2); s3.display(s3); std: : cout<<"\n\n"; _sets4=_intersection(s1,s2); s4.display(s4); std: : cout<<"\n\n"; _del(s1,8); s1.display(s1); std: : cout<<"\n\n"; _judge(s1,8); std: : cout<<"\n"; _judge(s2,5); std: : cout<<"Thescreenwillbeclosedin60s."< : endl; Sleep(1000*60); return0; } //题目 (2)源代码: //Shape.h #ifndefSHAPE_H_INCLUDED #defineSHAPE_H_INCLUDED #include //#include classShape { public: Shape(); virtual~Shape(); doublearea(){return0;}; doublegirth(){return0;}; virtualvoidshow(){std: : cout<<"ShapeObject"< : endl;}; protected: double_girth,_area; }; #endif//SHAPE_H_INCLUDED //Shape.cpp: implementationoftheShapeclass. // ////////////////////////////////////////////////////////////////////// #include"Shape.h" ////////////////////////////////////////////////////////////////////// //Construction/Destruction ////////////////////////////////////////////////////////////////////// Shape: : Shape() { } Shape: : ~Shape() { } //Circle.h #ifndefCIRCLE_H_INCLUDED #defineCIRCLE_H_INCLUDED #include"Shape.h" #definepi3.1415926 classCircle: publicShape { public: Circle(); Circle(double); virtual~Circle(); doublearea(); doublegirth(); voidshow(); private: doubler; }; #endif//CIRCLE_H_INCLUDED //ircle.cpp: implementationoftheCircleclass. // ////////////////////////////////////////////////////////////////////// #include"Circle.h" ////////////////////////////////////////////////////////////////////// //Construction/Destruction ////////////////////////////////////////////////////////////////////// Circle: : Circle() { } Circle: : Circle(doubleR) { r=R; } Circle: : ~Circle() { std: : cout<<"Thecircledestructionhasbeencalled."< : endl; } doubleCircle: : area() { //_area=pi*r*r; returnpi*r*r; } voidCircle: : show() { _area=area(); _girth=girth(); std: : cout<<"Itisacircleobject."< : endl; std: : cout<<"Theradiusofthecircleis"< : endl; std: : cout<<"Theareaofthecircleis"<<_area< : endl; std: : cout<<"Thegirthofcircleis"<<_girth< : endl; } doubleCircle: : girth() { //_girth=2*pi*r; return2*pi*r; } //Rectangle.h #ifndefRECTANGLE_H_INCLUDED #defineRECTANGLE_H_INCLUDED //#include #include"Shape.h" classRectangle: publicShape { public: Rectangle(); Rectangle(double,double); ~Rectangle(); doublearea(); doublegirth(); voidshow(); private: doublelength,width; }; #endif//RECTANGLE_H_INCLUDED //Rectangle.cpp #include"Rectangle.h" Rectangle: : Rectangle() { } Rectangle: : Rectangle(double_a,double_b) { length=_a; width=_b; } Rectangle: : ~Rectangle() { std: : cout<<"Therectangledestructionhasbeencalled."< : endl; } voidRectangle: : show() { _girth=girth(); _area=area(); std: : cout<<"Itisarectangleobject."< : endl; std: : cout<<"Thelengthandwidthoftherectangleare"< : endl; std: : cout<<"Theareaoftherectangleis"<<_area< : endl; std: : cout<<"Thegirthoftherectangleis"<<_girth< : endl; } doubleRectangle: : area() { returnlength*width; } doubleRectangle: : girth() { return2*(length+width); } //Triangle.h #ifndefTRIANGLE_H_INCLUDED #defineTRIANGLE_H_INCLUDED #include"Shape.h" #include //#include classTriangle: publicShape { public: Triangle(); Triangle(double,double,double); virtual~Triangle(); doublearea(); doublegirth(); voidshow(); booljudge(); private: double_side1,_side2,_side3; }; #endif//TRIANGLE_H_INCLUDED //Triangle.cpp: implementationoftheTriangleclass. // ////////////////////////////////////////////////////////////////////// #include"Triangle.h" #include ////////////////////////////////////////////////////////////////////// //Construction/Destruction ////////////////////////////////////////////////////////////////////// Triangle: : Triangle() { } Triangle: : Triangle(double_a,double_b,double_c) { _side1=_a; _side2=_b; _side3=_c; } Triangle: : ~Triangle() { std: : cout<<"Thetriangledestructionhasbeencalled."< : endl; } doubleTriangle: : area() { if(judge()) { doubles=0.0,l=(_side1+_side2+_side3)/2; s=sqrt(l*(l-_side1)*(l-_side2)*(l-_side3)); returns; } return0.0; } doubleTriangle: : girth() { if(judge()) return_side1+_side2+_side3; return0.0; } voidTriangle: : show() { if(judge()) { _area=area(); _girth=girth(); std: : cout<<"Itisatriangleobject."< : endl; std: : cout<<"Thethreesidesofthetriangleare"<<_side1<<""<<_side2<<""<<_side3<<""< : endl; std: : cout<<"Theareaofthetriangleis"<<_area< : endl; std: : cout<<"Thegirthoftriangleis"<<_girth< : endl; } else std: : cout<<"Itisnotalawfultriangle."< : endl; } boolTriangle: : judge() { //abcacbbacbcacabcba if(_side1+_side2>_side3&&_side1+_side3>_side2&&_side2+_side1>_side3&&_side2+_side3>_side1&&_side3+_side1>_side2&&_side3+_side2>_side1) returntrue; returnfalse; } //test.cpp #include"Circle.h" #include"Triangle.h" #include"Rectangle.h" /* 为什么不能在main.cpp或者其他的的头文件中添加 */ intmain() { Circlec1(1.0); c1.show(); std: : cout< : endl; Trianglet1(1.0,2.0,3.0); t1.show(); std: : cout< : endl; Trianglet2(10.0,3.0,8.0); t2.show(); std: : cout< : endl; std: : cout< : endl; Trianglet3(3.0,4.0,5.0); t3.show(); std: : cout< : endl; Rectangler1(1.0,2.0),r2(3.0,5.0); r1.show(); std: : cout< : endl; r2.show(); std: : cout< : endl; //std: : cout<<"Thescreenwillbeclosedin60s."< : endl; //Sleep(1000*60); return0; } //题目(3)源代码: //writing_instruments.h #ifndefWRITING_INSTRUMENTS_H_INCLUDED #defineWRITING_INSTRUMENTS_H_INCLUDED #include #include #include classwriting_instruments { protected: doubleprice; boolused_in_exam; boolme_owned_; doubleservice_span; std: : stringplace_of_origin; intnum; public: writing_instruments(); writing_instruments(int); virtual~writing_instruments(); virtualvoidshow(){std: : cout<<"..."< : endl;} virtualvoidadd(int); virtualvoid_minus(int); }; #endif//WRITING_INSTRUMENTS_H_INCLUDED //writing_instruments.cpp #include"writing_instruments.h" writing_instruments: : wr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 合肥 工业大学 宣城 校区 程序设计 基础 周波 实验 仅供参考
![提示](https://static.bdocx.com/images/bang_tan.gif)