收藏 分销(赏)

西安交大C++程序设计第十章作业说课讲解.doc

上传人:快乐****生活 文档编号:3760569 上传时间:2024-07-17 格式:DOC 页数:44 大小:590KB
下载 相关 举报
西安交大C++程序设计第十章作业说课讲解.doc_第1页
第1页 / 共44页
西安交大C++程序设计第十章作业说课讲解.doc_第2页
第2页 / 共44页
西安交大C++程序设计第十章作业说课讲解.doc_第3页
第3页 / 共44页
西安交大C++程序设计第十章作业说课讲解.doc_第4页
第4页 / 共44页
西安交大C++程序设计第十章作业说课讲解.doc_第5页
第5页 / 共44页
点击查看更多>>
资源描述

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

2、指向基类Base的指针变量ptr,并分别定义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

3、:void virtual display()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;c

4、lass Shapepublic:virtual 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(

5、int a=0,int b=0)SetPoint(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 na

6、mespace std;class Circle: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

7、 void PrintShapeName() 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

8、 l=0.0,double w=0.0):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;vir

9、tual void Print() constcoutLeft 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:Cylind

10、er(int x=0,int y=0,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/

11、square.h正方形类,几乎跟矩形类一样而已#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*sideleng

12、th;virtual double Volume() 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

13、.h#includecylinder.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 cylind

14、er(120,80,10.0,40.0);/输出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;/定义了

15、个基类指针arrayOfShapes0=&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 referen

16、cesn;for(int j=0;jPrintShapeName();baseClassPtr-Print();coutArea=Area();couttVolume=Volume()endl;/通过基类对象引用访问虚函数实现动态绑定?void virtualViaReference(const Shape &baseClassRef)baseClassRef.PrintShapeName();baseClassRef.Print();coutArea=baseClassRef.Area();couttVolume=baseClassRef.Volume()endl;2.实验结果: (三)第三

17、题: 扩充实例编程中的日期类,为Date增加一个成员函数,可以判断日期是否为系统当前日期。从键盘输入你的生日,如果今天是你的生日则显示:“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 i

18、nt days;/每月的天数public: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

19、() const;/输出日期yy_mm_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

20、 Date:days = 0, 31, 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 = g

21、m-tm_mday;void Date: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 *

22、this;const Date &Date: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

23、false;bool Date:IsEndofMonth() 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 - 1

24、900;dd = yy*365;if(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 d

25、ay , year endl;void 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!/”否则显示“还有*天是你的生日”或者“你的生日已经过去了* 天,距离明年生日还有

26、*天”。#include #include 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

27、;cout the next date 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 重载函数、输出生日祝词与蛋糕形状函数。并编写主函数测试各成员函数。继承方式如下

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

29、 volume() 、 area() 、 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,

30、word);double Vol()return 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

31、 YUAN_FANG_H#define 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 fan

32、gs,char word)Yuan:Set(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

33、)if(Vol()yf.Vol()cout第一个蛋糕大;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

34、_Fang:Set(0,0,0,0,Happy 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 wor

35、d)Yuan_Fang:Set(yuanh,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 高:L

36、ingHt 长轴长:LingAt 短轴长: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.hint

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服