西安交大C++程序设计第十章作业_第1页
西安交大C++程序设计第十章作业_第2页
西安交大C++程序设计第十章作业_第3页
西安交大C++程序设计第十章作业_第4页
西安交大C++程序设计第十章作业_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

1、西安交通大学实验报告课程_计算机程序设计_实验名称_多态性_第 1 页 共 44 页系 别_ _ 实 验 日 期 2014 年 5 月 31 日专业班级_ _组别_ 实 验 报 告 日 期 2014 年 5 月 31 日姓 名_ _学号_ _ 报 告 退 发 ( 订正 、 重做 )同 组 人_ 教 师 审 批 签 字 一、实验目的理解掌握多态的使用方法,学会用虚函数。二、实验内容 (一)第一题:定义一个类Base,该类含有虚函数display,然后定义它的两个派生类FirstB和SecondB,这两个派生类均含有公有成员函数display,在主程序中,定义指向基类Base的指针变量ptr,并分

2、别定义Base、FirstB、Second的对象b1、f1、s1,让ptr分别指向b1、f1、s1的起始地址,然后指向执行这些对象的成员函数display。1.源程序代码:#includeusing namespace std;class Base public:virtual void display()coutsound!sound!sound!;class FirstB:public Basepublic:void virtual display()coutmiao!miao!miao!;class SecondB:public Basepublic:void virtual displ

3、ay()coutwang!wang!wang!;int main()Base *ptr;Base b1;FirstB f1;SecondB s1;coutdisplay();coutdisplay();coutdisplay();coutendl;return 0; 2.实验结果:(二)第二题: 扩充例10-5,从中派生一个正方形类和圆柱体类,写一个测试程序,输出正方形的面积和圆柱体的体积。1.源程序代码: /shape类shape.h文件#ifndef SHAPE_H#define SHAPE_H#includeusing namespace std;class Shapepublic:vi

4、rtual double Area()constreturn 0.0;/纯虚函数,在派生类中重载virtual double Volume() const=0;virtual void PrintShapeName() const=0;virtual void Print() const=0;#endif/point.h点类#ifndef POINT_H#define POINT_H#include#includeshape.husing namespace std;class Point:public Shapeint x,y;public:Point(int a=0,int b=0)Set

5、Point(a,b);void SetPoint(int a,int b)x=a;y=b;int GetX()return x;int GetY()return y;virtual double Volume() constreturn 0.0;virtual void PrintShapeName()constcoutPoint:;virtual void Print()constcoutx,y;#endif/circle.h圆类#ifndef CIRCLE_H#define CIRCLE_H#include#includepoint.husing namespace std;class C

6、ircle:public Pointdouble radius;public:Circle(int x=0,int y=0,double r=0.0):Point(x,y)SetRadius(r);void SetRadius(double r)radius=(r=0?r:0);double GetRadius() constreturn radius;virtual double Area() constreturn 3.14159*radius*radius;virtual double Volume() constreturn 0.0;virtual void PrintShapeNam

7、e() constcoutCircle:;void Print() constcoutCenter=;Point:Print();cout;Radius=radiusendl;#endif/rectangle.h矩形类#ifndef RECTANGULAR_H#define RECTANGULAR_H#include#includepoint.husing namespace std;class Rectangle:public Pointdouble length,width;public:Rectangle(int x=0,int y=0,double l=0.0,double w=0.0

8、):Point(x,y)SetLength(l);SetWidth(w);void SetLength(double l)length=(l=0?l:0);void SetWidth(double w)width=(w=0?w:0);double GetLength() constreturn length;double GetWidth() constreturn width;virtual double Area() constreturn length*width;virtual double Volume() constreturn 0.0;virtual void Print() c

9、onstcoutLeft Top Vertex=;Point:Print();cout;Length=length,Width=widthendl;virtual void PrintShapeName() constcoutRectangle:;#endif/cylinder.h圆柱体类#ifndef CYLINDER_H#define CYLINDER_H#includecircle.h#include;using namespace std;class Cylinder:public Circledouble height;public:Cylinder(int x=0,int y=0,

10、double r=0,double h=0):Circle(x,y,r)SetHeight(h);void SetHeight(double h)height=(h=0?h:0);double GetHeight() constreturn height;double Volume() constreturn Area()*height;virtual void PrintShapeName() constcoutCylinder:;void Print() constCircle:Print();coutHeight=heightendl;#endif/square.h正方形类,几乎跟矩形类

11、一样而已#ifndef SQUARE_H#define Square_H#includerectangle.h#includeusing namespace std;class Square:public Rectangledouble sidelength;public:Square(int x=0,int y=0,double s=0.0):Rectangle(x,y)Seta(s);void Seta(double s)sidelength=s;virtual double Area() constreturn sidelength*sidelength;virtual double V

12、olume() constreturn 0.0;virtual void Print() constcoutLeft Top Vertex=;Point:Print();cout;Length=sidelengthendl;virtual void PrintShapeName() constcoutSquare:;#endifmain.h/要求:派生出圆柱类和正方形类,计算面积、体积#include#includeshape.h#includepoint.h#includecircle.h#includerectangle.h#includesquare.h#includecylinder.

13、husing namespace std;/为何系统报错提示要输入一个“;”在此句首?void virtualViaPointer(const Shape*);void virtualViaReference(const Shape&);int main()/创建point circle rectangular对象信息Point point(30,50);Circle circle(120,80,10.0);Rectangle rectangle(10,10,8.0,5.0);Square square(10,20,5.0);Cylinder cylinder(120,80,10.0,40.0

14、);/输出point circle rectangular 对象信息point.PrintShapeName();point.Print();coutendl;circle.PrintShapeName();circle.Print();rectangle.PrintShapeName();rectangle.Print();square.PrintShapeName();square.Print();cylinder.PrintShapeName();cylinder.Print();/定义基类对象指针Shape *arrayOfShapes5;/定义了个基类指针arrayOfShapes0

15、=&point;arrayOfShapes1=&circle;arrayOfShapes2=&rectangle;arrayOfShapes3=□arrayOfShapes4=&cylinder;/通过基类对象指针访问派生类对象coutVirtual function calls made offbase-class pointersn;for(int i=0;i5;i+)virtualViaPointer(arrayOfShapesi);coutVirtual function calls made offbase-case referencesn;for(int j=0;jP

16、rintShapeName();baseClassPtr-Print();coutArea=Area();couttVolume=Volume()endl;/通过基类对象引用访问虚函数实现动态绑定?void virtualViaReference(const Shape &baseClassRef)baseClassRef.PrintShapeName();baseClassRef.Print();coutArea=baseClassRef.Area();couttVolume=baseClassRef.Volume()endl;2.实验结果: (三)第三题: 扩充实例编程中的日期类,为Dat

17、e增加一个成员函数,可以判断日期是否为系统当前日期。从键盘输入你的生日,如果今天是你的生日则显示:“Harry Birthday!”否则显示“还有*天是你的生日”或者“你的生日已经过去了* 天,距离明年生日还有*天”。1.源程序代码:/ 日期类定义date.h#ifndef DATE_H#define DATE_H#include using namespace std;class Date int day,month,year;void IncDay();/日期增加一天int DayCalc() const;/距基准日期的天数static const int days;/每月的天数publi

18、c:Date( int y, int m, int d);/构造函数Date( int m, int d);/构造函数,年默认为系统当前年份Date();/构造函数,默认为系统日期void SystemDate();void SetDate( int yy, int mm, int dd );/日期设置void SetDate( int mm, int dd );/日期设置,年默认为系统年份bool IsLeapYear(int yy) const;/ 是否闰年?bool IsEndofMonth() const;/ 是否月末?void print_ymd() const;/输出日期yy_mm

19、_ddvoid print_mdy() const;/输出日期mm_dd_yyconst Date &operator+(int days); / 日期增加任意天const Date &operator+=(int days); / 日期增加任意天int operator-(const Date& ymd)const; / 两个日期之间的天数void Show(Date& da);#endif/ Date类成员函数定义date.cpp#include #include #include date.husing namespace std;const int Date:days = 0, 31,

20、 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 ;/构造函数Date:Date(int y,int m,int d) SetDate(y,m,d); Date:Date(int m,int d) SetDate(m,d); Date:Date() SystemDate();void Date:SystemDate()/取得系统日期tm *gm;time_t t=time(NULL);gm = gmtime(&t);year = 1900 + gm-tm_year;month = gm-tm_mon +1;day = gm-tm_mday;void Date

21、:SetDate( int yy, int mm, int dd )month = ( mm = 1 & mm = 1900 & yy = 1 & dd = 1 & dd = 1 & mm tm_year;if ( month = 2 & IsLeapYear( year ) )day = ( dd = 1 & dd = 1 & dd = days month ) ? dd : 1;const Date &Date:operator+( int days )/重载+for ( int i = 0; i days; i+ )IncDay();return *this;const Date &Da

22、te:operator+=( int days )/重载+=for ( int i = 0; i days; i+ )IncDay();return *this;int Date:operator-(const Date& ymd )const/重载-int days;days = DayCalc()-ymd.DayCalc();return days;bool Date:IsLeapYear( int y ) constif ( y % 400 = 0 | ( y % 100 != 0 & y % 4 = 0 ) )return true;return false;bool Date:IsE

23、ndofMonth() constif ( month = 2 & IsLeapYear( year ) )return day = 29; /二月需要判断是否闰年elsereturn day = days month ;void Date:IncDay()/日期递增一天if ( IsEndofMonth()if (month = 12) / 年末day = 1;month = 1;year+;else / 月末day = 1;month+;else day+;int Date:DayCalc() constint dd;int yy = year - 1900;dd = yy*365;if(

24、yy) dd += (yy-1)/4;for(int i=1;i2)dd+;dd += day;return dd;void Date:print_ymd() constcout year - month - day endl;void Date:print_mdy() constchar *monthName 12 = January,February, March, April, May, June,July, August, September, October,November, December ;cout monthName month-1 day , year endl;void

25、 Date:Show(Date& da)if(day=da.day&month=da.month)coutHappy Birthday!;elseif(da-Date:Date()0)da.year+;coutIt will be your birthday after da-Date:Date() days!;/扩充实例编程中的日期类,为Date增加一个成员函数,可以判断日期是否为系统当前日期。/从键盘输入你的生日,如果今天是你的生日则显示:“Harry Birthday!/”否则显示“还有*天是你的生日”或者“你的生日已经过去了* 天,距离明年生日还有*天”。#include #inclu

26、de date.husing namespace std;int main()Date today,Olympicday(2004,8,13);cout Today (the computers day) is: ;today.print_ymd();cout After 365 days, the date is: ;today += 365;today.print_ymd();Date testday(2,28);cout the test date is: ;testday.print_ymd();Date nextday = testday + 1;cout the next date

27、 is: ;nextday.print_ymd();today.SystemDate();cout the Athens Olympic Games openday is: ;Olympicday.print_mdy();cout And after Olympicday-today days, the Athens Olympic Games will open. endl;coutab;Date birthday(a,b);today.Show(birthday);cout 重载函数、输出生日祝词与蛋糕形状函数。并编写主函数测试各成员函数。继承方式如下:首先定义圆柱体形状蛋糕类,只有 3

28、个数据成员,半径、高、生日祝词。然后派生出圆柱体 _ 方柱体蛋糕类,即在圆柱体形状上增加方柱体形状。假定方柱体的正方形面积小于圆柱体的圆形面积,注意方柱体的高与正方形的边长不一定相等。再用圆柱体 _ 方柱体蛋糕类派生出圆柱体 _ 方柱体 _ 菱形体蛋糕类,即在方柱体形状上增加菱形柱体形状。假定菱形柱体的菱形面积小于方柱体的正方形面积。提示: 运算符 重载指两个蛋糕对象的体积大小。基类和派生类数据成员不能定义为 public 。 设置数据成员函数、求蛋糕体积、求蛋糕表面积函数、输出生日祝词与蛋糕形状函数均为同名重载函数,例如分别采用函数名为 init() 、 volume() 、 area()

29、、 output() 。程序运行参考图如下:(红颜色的功能下一章实验完成)1.源程序代码:/yuan.h圆类头文件#ifndef YUAN_H#define YUAN_Hclass Yuanint YuanH,YuanR;char Word50;public:Yuan()YuanH=0;YuanR=0;strcpy(Word,Happy Birthday!);Yuan(int h,int r,char word)Set(h,r,word);void Set(int h,int r,char word)YuanH=h;YuanR=r;strcpy(Word,word);double Vol()r

30、eturn YuanH*YuanR*YuanR*3.14159;double Area()return 2*(YuanH*YuanR*3.14159+3.14159*YuanR*YuanR);void Print()coutWord;coutn圆形蛋糕:;cout半径:YuanRt高:YuanHt体积:Vol()t表面积:(Yuan& y)if(Vol()y.Vol()cout第一个蛋糕大;if(Vol()=y.Vol()cout两个蛋糕一样大;if(Vol()y.Vol()cout第二个蛋糕大;#endif/yuanfang.h圆方类头文件#ifndef YUAN_FANG_H#define

31、 YUAN_FANG_H#include#includeyuan.hclass Yuan_Fang:public Yuanint FangH,FangS;public:Yuan_Fang()FangH=0;FangS=0;Yuan:Set(0,0,Happy birthday!);Yuan_Fang(int yuanh,int yuanr,int fangh,int fangs,char word)Set(yuanh,yuanr,fangh,fangs,word);void Set(int yuanh,int yuanr,int fangh,int fangs,char word)Yuan:S

32、et(yuanh,yuanr,word);FangH=fangh;FangS=fangs;double Vol()return Yuan:Vol()+FangH*FangS*FangS;double Area()return 4*FangH*FangS+2*FangS*FangS;double Areato()return Area()+Yuan:Area()-2*FangS*FangS;void Print()Yuan:Print();coutn方形蛋糕:;cout高:FangHt底边长:FangSt体积:Vol()t表面积:(Yuan_Fang& yf)if(Vol()yf.Vol()co

33、ut第一个蛋糕大;if(Vol()=yf.Vol()cout两个蛋糕一样大;if(Vol()yf.Vol()cout第二个蛋糕大;#endif/yuanfangling.h圆方菱类头文件#ifndef YUAN_FANG_LING_H#define YUAN_FANG_LING_H#include#includeyuanfang.husing namespace std;class Yuan_Fang_Ling:public Yuan_Fangint LingH;int LingA;int LingB;public:Yuan_Fang_Ling()Yuan_Fang:Set(0,0,0,0,H

34、appy birthday to you!);LingH=0;LingA=0;LingB=0;Yuan_Fang_Ling(int yuanh,int yuanr,int fangh,int fangs,int lingh,int linga,int lingb,char word)Set(yuanh,yuanr,fangh,fangs,lingh,linga,lingb,word);void Set(int yuanh,int yuanr,int fangh,int fangs,int lingh,int linga,int lingb,char word)Yuan_Fang:Set(yua

35、nh,yuanr,fangh,fangs,word);LingH=lingh;LingA=linga;LingB=lingb;double Vol()return Yuan_Fang:Vol()+LingH*LingA*LingB/2;double Area()return LingA*LingB+2*LingH*(LingA+LingB);double Areato()return Yuan_Fang:Areato()+Area()-LingA*LingB;void Print()Yuan_Fang:Print();coutn菱形蛋糕:;cout 高:LingHt 长轴长:LingAt 短轴

36、长:LingBt 体积:Vol()t表面积:Area();coutn总体积:Yuan:Vol()+Yuan_Fang:Vol()+Vol();coutt总表面积:(Yuan_Fang_Ling& yfl)if(Vol()yfl.Vol()cout第一个蛋糕大;if(Vol()=yfl.Vol()cout两个蛋糕一样大;if(Vol()yfl.Vol()cout第二个蛋糕大;return *this;#endif/main.cpp#includeusing namespace std;#includeyuan.h#includeyuanfang.h#includeyuanfangling.hin

37、t main()Yuan_Fang_Ling yuanfangling1(20,20,20,20,20,20,20,Happy birthday!),yuanfangling2;cout第一个蛋糕:n;yuanfangling1.Print();coutendl;cout第二个蛋糕:n;yuanfangling2.Print();coutendl;int yuanh1,yuanr1,fangh1,fangs1,lingh1,linga1,lingb1,yuanh2,yuanr2,fangh2,fangs2,lingh2,linga2,lingb2;char word150,word250;co

38、utyuanh1yuanr1fangh1fangs1lingh1linga1lingb1;cin.get(word1,50);coutyuanh2yuanr2fangh2fangs2lingh2linga2lingb2;cin.get(word2,50);yuanfangling1.Set(yuanh1,yuanr1,fangh1,fangs1,lingh1,linga1,lingb1,word1);yuanfangling2.Set(yuanh2,yuanr2,fangh2,fangs2,lingh2,linga2,lingb2,word2);cout第一个蛋糕:n;yuanfangling

39、1.Print();coutendl;cout第二个蛋糕:n;yuanfangling2.Print();coutyuanfangling2;coutendl;return 0;2.实验结果:五、第五题:(必做题) 定义一个一元二次方程类,通过继承方式定义一元三次方程类,再继承定义一元四次方程类。类中至少包含构造函数、求根函数、运算符 + 重载函数、运算符 - 重载函数、运算符=重载函数、输出方程的函数等6 个函数,并编写主函数测试各成员函数。提示: 两个一元三次方程对应相加仍然是一个一元三次方程; 求根方法采用迭代方法,迭代公式为: Xn+1=Xn F(Xn)/F (Xn) ,结束迭代的条件

40、 |F(Xn+1)|10-7 与 |Xn+1-Xn|10-7 同时成立; 一元三次方程的一般形式如下: F(X)=AX3+BX2+CX+D=0 。输出方程格式为: A*X3+B*X2+C*X+D=0 ;两个一元三次方程对应相加和对应相减仍然是一元三次方程。假定类中的方程系数能求解出实根。不考虑方程存在虚根和无根的情况。求根函数应该有一个参数,该参数指明迭代初值。例如方程 2X3-4X2+3x-6 0 在 1.5 附近的根。又例如方程 X3+12X2+48X+64=0 在 -4.5 附近的根。例如方程X4-10X3+35X2-50x+240在4.5附近的根。又例如方程X4+12X3+54X2+1

41、08X+81=0在-1.5附近的根。因此类中数据成员除了系数外,还应考虑迭代初值作为数据成员。 1.源程序代码:#include#includeusing namespace std;class Equation2protected:double A,B,C;double X0;public:Equation2(double a=1,double b=1,double c=1,double x0=0.0)Set(a,b,c,x0);void Set(double a,double b,double c,double x0)A=a;B=b;C=c;X0=x0;double root()double X=X0;double temp;dotemp=X;X=X-(A*X*X+B*X+C)/(2*A*X+B);while(fabs(X)=10e-7|fabs(temp-X)=10e-7);return X;Equation2 operator +(Equation2 eq2)A=A+eq2.A;B=B+eq2.B;C=C+eq2.C;return *this;Equation2 operator -(Equation2 eq2)A=A-eq2.A;B=B-eq2

温馨提示

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

评论

0/150

提交评论