ImageVerifierCode 换一换
格式:DOC , 页数:44 ,大小:590KB ,
资源ID:3760569      下载积分:12 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/3760569.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(西安交大C++程序设计第十章作业说课讲解.doc)为本站上传会员【快乐****生活】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

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

1、 西安交大C++程序设计第十章作业 精品资料 西安交通大学实验报告 课程__计算机程序设计__实验名称__多态性__第 1 页 共 44 页 系 别____ ______ 实 验 日 期 2014 年 5 月 31 日 专业班级__ ____组别_____________ 实 验 报 告 日 期 2014 年 5 月 31 日 姓 名___ _____学号_ _ 报 告 退 发 ( 订正 、 重做 ) 同 组 人________________________

2、 教 师 审 批 签 字 一、实验目的 理解掌握多态的使用方法,学会用虚函数。 二、实验内容 (一)第一题:定义一个类Base,该类含有虚函数display,然后定义它的两个派生类FirstB和SecondB,这两个派生类均含有公有成员函数display,在主程序中,定义指向基类Base的指针变量ptr,并分别定义Base、FirstB、Second的对象b1、f1、s1,让ptr分别指向b1、f1、s1的起始地址,然后指向执行这些对象的成员函数display。 1.源程序代码: #include using namespace

3、std; class Base { public: virtual void display() { cout<<"sound!sound!sound!"; } }; class FirstB:public Base { public: void virtual display() { cout<<"miao!miao!miao!"; } }; class SecondB:public Base { public: void virtual display() { cout<<"wang!wang!wang!"; }

4、 }; int main() { Base *ptr; Base b1; FirstB f1; SecondB s1; cout<<"指向基类Base:\t"; ptr=&b1; ptr->display(); cout<<"\n指向派生类FirstBase:\t"; ptr=&f1; ptr->display(); cout<<"\n指向派生类SecondB:\t"; ptr=&s1; ptr->display(); cout<

5、中派生一个正方形类和圆柱体类,写一个测试程序,输出正方形的面积和圆柱体的体积。 1.源程序代码: //shape类shape.h文件 #ifndef SHAPE_H #define SHAPE_H #include using namespace std; class Shape { public: virtual double Area()const { return 0.0; }//纯虚函数,在派生类中重载 virtual double Volume() const=0; virtual void PrintShapeNam

6、e() const=0; virtual void Print() const=0; }; #endif //point.h点类 #ifndef POINT_H #define POINT_H #include #include"shape.h" using namespace std; class Point:public Shape { int x,y; public: Point(int a=0,int b=0) { SetPoint(a,b); } void SetPoint(int a,int b) {

7、 x=a; y=b; } int GetX() { return x; } int GetY() { return y; } virtual double Volume() const { return 0.0; } virtual void PrintShapeName()const { cout<<"Point:"; } virtual void Print()const { cout<<'['<

8、fndef CIRCLE_H #define CIRCLE_H #include #include"point.h" using namespace std; class Circle:public Point { double 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

9、) const { return radius; } virtual double Area() const { return 3.14159*radius*radius; } virtual double Volume() const { return 0.0; } virtual void PrintShapeName() const { cout<<"Circle:"; } void Print() const { cout<<"Center="; Point::Print(); cout<<

10、";Radius="< #include"point.h" using namespace std; class Rectangle:public Point { double length,width; public: Rectangle(int x=0,int y=0,double l=0.0,double w=0.0):Point(x,y)

11、{ SetLength(l); SetWidth(w); } void SetLength(double l) { length=(l>=0?l:0); } void SetWidth(double w) { width=(w>=0?w:0); } double GetLength() const { return length; } double GetWidth() const { return width; } virtual double Area() const { return l

12、ength*width; } virtual double Volume() const { return 0.0; } virtual void Print() const { cout<<"Left Top Vertex="; Point::Print(); cout<<";Length="<

13、er.h圆柱体类 #ifndef CYLINDER_H #define CYLINDER_H #include"circle.h" #include ;using namespace std; class Cylinder:public Circle { double height; public: Cylinder(int x=0,int y=0,double r=0,double h=0):Circle(x,y,r) { SetHeight(h); } void SetHeight(double h) { height

14、h>=0?h:0); } double GetHeight() const { return height; } double Volume() const { return Area()*height; } virtual void PrintShapeName() const { cout<<"Cylinder:"; } void Print() const { Circle::Print(); cout<<"Height="<

15、h正方形类,几乎跟矩形类一样而已 #ifndef SQUARE_H #define Square_H #include"rectangle.h" #include using namespace std; class Square:public Rectangle { double sidelength; public: Square(int x=0,int y=0,double s=0.0):Rectangle(x,y) { Seta(s); } void Seta(double s) { sidelength=s;

16、 } virtual double Area() const { return sidelength*sidelength; } virtual double Volume() const { return 0.0; } virtual void Print() const { cout<<"Left Top Vertex="; Point::Print(); cout<<";Length="<

17、ut<<"Square:"; } }; #endif main.h//要求:派生出圆柱类和正方形类,计算面积、体积 #include #include"shape.h" #include"point.h" #include"circle.h" #include"rectangle.h" #include"square.h" #include"cylinder.h" using namespace std;//为何系统报错提示要输入一个“;”在此句首???? void virtualViaPointer(const Shape*); void v

18、irtualViaReference(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); //输出point circle rectangular 对象信息 point.PrintShapeName(

19、); point.Print(); cout<

20、 arrayOfShapes[1]=&circle; arrayOfShapes[2]=&rectangle; arrayOfShapes[3]=□ arrayOfShapes[4]=&cylinder; //通过基类对象指针访问派生类对象 cout<<"Virtual function calls made off"<<"base-class pointers\n"; for(int i=0;i<5;i++) virtualViaPointer(arrayOfShapes[i]); cout<<"Virtual function calls

21、 made off"<<"base-case references\n"; for(int j=0;j<5;j++) virtualViaReference(*arrayOfShapes[j]); return 0; } //通过基类对象访问虚函数实现动态绑定??? void virtualViaPointer(const Shape*baseClassPtr) { baseClassPtr->PrintShapeName(); baseClassPtr->Print(); cout<<"Area="<Area(); cout

22、<<"\tVolume="<Volume()<

27、e(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::SetDate( int yy, int mm, int dd ) { month = ( mm >= 1 &

28、 mm <= 12 ) ? mm : 1; year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900; if ( month == 2 && IsLeapYear( year ) ) day = ( dd >= 1 && dd <= 29 ) ? dd : 1; else day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1; } void Date::SetDate(int mm, int dd ) { tm *gm; time_t t=time(NULL); gm

29、 = gmtime(&t); month = ( mm >= 1 && mm <= 12 ) ? mm : 1; year = 1900 + gm->tm_year; if ( month == 2 && IsLeapYear( year ) ) day = ( dd >= 1 && dd <= 29 ) ? dd : 1; else day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1; } const Date &Date::operator+( int days ) { //重载+ for ( int i

30、 = 0; i < days; i++ ) IncDay(); return *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

31、IsLeapYear( int y ) const { if ( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) ) return true; return false; } bool Date::IsEndofMonth() const { if ( month == 2 && IsLeapYear( year ) ) return day == 29; //二月需要判断是否闰年 else return day == days[ month ]; } void Date::IncDay() {

32、 //日期递增一天 if ( IsEndofMonth()) if (month == 12){ // 年末 day = 1; month = 1; year++; } else { // 月末 day = 1; month++; } else day++; } int Date::DayCalc() const { int dd; int yy = year - 1900; dd = yy*365; if(yy) dd += (yy-1)/4; for(int i=1;

33、i2)) dd++; dd += day; return dd; } void Date::print_ymd() const { cout << year << "-" << month << "-" << day << endl; } void Date::print_mdy() const { char *monthName[ 12 ] = {"January", "February", "March", "April", "Ma

34、y", "June", "July", "August", "September", "October", "November", "December" }; cout << monthName[ month-1 ] << ' ' << day << ", " << year << endl; } void Date::Show(Date& da) { if(day==da.day&&month==da.month) cout<<"Happy Birthday!"; else { if(da-Date::Date()<0) da.ye

35、ar++; cout<<"It will be your birthday after "< #include "date.h" using namespace std; int main() {

36、 Date today,Olympicday(2004,8,13); cout << "Today (the computer's 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 <<

37、"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." <

38、n>>a>>b; Date birthday(a,b); today.Show(birthday); cout< 重载函数、输出生日祝词与蛋糕形状函数。并编写主函数测试各成员函数。继承方式如下:首先定义圆柱体形状蛋糕类,只有 3 个数据成员,半径、高、生日祝词。然后派生出圆柱体 _ 方柱体蛋糕类,即在圆柱体形状上增加方柱体

39、形状。假定方柱体的正方形面积小于圆柱体的圆形面积,注意方柱体的高与正方形的边长不一定相等。再用圆柱体 _ 方柱体蛋糕类派生出圆柱体 _ 方柱体 _ 菱形体蛋糕类,即在方柱体形状上增加菱形柱体形状。假定菱形柱体的菱形面积小于方柱体的正方形面积。提示: ① 运算符 > 重载指两个蛋糕对象的体积大小。 ②基类和派生类数据成员不能定义为 public 。③ 设置数据成员函数、求蛋糕体积、求蛋糕表面积函数、输出生日祝词与蛋糕形状函数均为同名重载函数,例如分别采用函数名为 init() 、 volume() 、 area() 、 output() 。程序运行参考图如下:(红颜色的功能下一章实验完成) 1

40、源程序代码: //yuan.h圆类头文件 #ifndef YUAN_H #define YUAN_H class Yuan { int YuanH,YuanR; char Word[50]; 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;

41、 YuanR=r; strcpy(Word,word); } double Vol() { return YuanH*YuanR*YuanR*3.14159; } double Area() { return 2*(YuanH*YuanR*3.14159+3.14159*YuanR*YuanR); } void Print() { cout<

42、); } Yuan operator >(Yuan& y) { if(Vol()>y.Vol()) cout<<"第一个蛋糕大"; if(Vol()==y.Vol()) cout<<"两个蛋糕一样大"; if(Vol() #include"yuan.h" class Yuan_Fang:pub

43、lic Yuan { int 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[])

44、{ 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::Prin

45、t(); cout<<"\n方形蛋糕:"; cout<<"高:"<(Yuan_Fang& yf) { if(Vol()>yf.Vol()) cout<<"第一个蛋糕大"; if(Vol()==yf.Vol()) cout<<"两个蛋糕一样大"; if(Vol()

46、ing.h圆方菱类头文件 #ifndef YUAN_FANG_LING_H #define YUAN_FANG_LING_H #include #include"yuanfang.h" using namespace std; class Yuan_Fang_Ling:public Yuan_Fang { int LingH; int LingA; int LingB; public: Yuan_Fang_Ling() { Yuan_Fang::Set(0,0,0,0,"Happy birthday to you!"); Li

47、ngH=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[]) {

48、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()-Lin

49、gA*LingB; } void Print() { Yuan_Fang::Print(); cout<<"\n菱形蛋糕:"; cout<<" 高:"<(Yuan_Fang_Lin

50、g& yfl) { if(Vol()>yfl.Vol()) cout<<"第一个蛋糕大"; if(Vol()==yfl.Vol()) cout<<"两个蛋糕一样大"; if(Vol() using namespace std; #include"yuan.h" #include"yuanfang.h" #include"yuanfangling.h" int

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

关于我们      便捷服务       自信AI       AI导航        抽奖活动

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

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

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

关注我们 :微信公众号    抖音    微博    LOFTER 

客服