




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第八章 多态性典型案例【案例8-1】编写矩形类重载成员函数#include#include#includeusing namespace std;class CRectlong left; long top;long right; long bottom;public: CRect(int l=1, int t=2, int r=3, int b=4);void SetRect(int x1, int y1, int x2, int y2); /重载的成员函数void SetRect(const CRect& r) ; /重载的成员函数 void Disp();void CRect:Disp() /成员函数显示自身的数据coutleft=left; top=top; right=right; bottom=right = right;this-bottom = bottom;int main()CRect r,s; /调用构造函数CRect(1, 2, 3, 4)定义对象r.Disp(); /r对象显示数据r.SetRect(5,6,7,8); s.SetRect(r); /调用重载的成员函数 s.Disp(); /显示s对象数据return 0; 【案例8-2】电话号码类PhoneNumber重载流提取与流插入运算符#include #include using namespace std;class PhoneNumber /重载流插入操作符,如果需要使用cout somePhoneNumber;则不能重载为成员函数friend ostream &operator( ostream &output, const PhoneNumber &num ) output ( num.areaCode ) num.exchange - somePhoneNumber;则不能重载为成员函数friend istream &operator( istream &input, PhoneNumber &num )input.ignore(); input setw( 4 ) num.areaCode; input.ignore( 2 ); input setw( 4 ) num.exchange; input.ignore(); input setw( 5 ) num.line; return input; private: char areaCode4,exchange4,line5; ; int main() PhoneNumber phone; /创建对象phone cout phone; /语句1:使用流提取运算符 cout The phone number entered was: phone endl; /语句2:使用流插入运算符 return 0;【案例8-3】编写词汇类Words重载下标操作符#include #include using namespace std;class Words char *str;public:Words (char *s) str=new charstrlen (s) +1 ; strcpy(str, s) ; void disp() cout str =0) word n-1 =word n-1 -32 ; /语句2:字母转化为大写 n-; word.disp () ;return 0;【案例8-4】显示和隐式调用虚函数#include using namespace std;class Base public:virtual void fun(); /fun()是虚函数private: long m_nx; ;void Base:fun()coutBase=sizeof(Base)endl;class Derived: public Base long m_ny; void fun(); /派生类的覆盖版本是私有的; void Derived:fun()coutDerived=sizeof(Derived)fun(); /指针形参隐式调用虚函数,动态绑定 void Show(Base& r)r.Base:fun(); /语句1:引用形参显式调用基类虚函数,静态绑定 void main() Base* pobjx=new Base();Derived objy;int k;coutk;if(k=1)Show(pobjx);Show(&objy); / k=1调用指针形参的Show版本else / k不等于1调用引用形参Show版本Show(*pobjx);Show(objy); Showv(objy); / Showv调用中实参是派生类对象objy,形参是基类对象objx【案例8-5】设计形状类Shape通过两级抽象基类实现统一接口#include #include using namespace std;const double PI=3.14159265358979;class Shape/声明抽象基类public: virtual void Disp() = 0; virtual float area() = 0;class TwoDShape : public Shape /抽象派生类TwoDShapepublic: virtual float perimeter() = 0;class ThreeDShape : public Shape /抽象派生类ThreeDShape public: virtual float volume() = 0;class Circle : public TwoDShapepublic: Circle(float r) : radius(r) void Disp() cout Shape is a circle.n; float perimeter() return 2*PI*radius; float area() return PI*radius*radius; private: float radius;class Rectangle : public TwoDShapepublic: Rectangle(float l, float w) : length(l),width(w) void Disp() cout Shape is a Rectangle.n; float perimeter() return 2*(length+width); float area() return length*width; private: float length,width;class Cone : public ThreeDShapepublic: Cone(float r, float h) : radius(r), height(h) void Disp() cout Shape is a Cone.n; float area()return PI*radius*(radius+sqrt(radius*radius + height*height); float volume() return PI*radius*radius*height/3; private: float radius, height;class Box : public ThreeDShapepublic: Box(float l, float w,float h) : length(l),width(w), height(h) void Disp() cout Disp();/coutperimeter(); /语句1:编译错误coutarea()Disp();/coutperimeter(); /语句2:编译错误c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 车险核保考试题及答案
- 发展新质生产力的
- 福建新质生产力发展计划
- 新质生产力赋能出版业
- 民族英雄戚继光课件
- 民族舞蹈基本功训练课件
- 植树节活动方案(模板)
- 数字科技赋能新质生产力
- 2025年妇产科超声常见疾病诊断模拟考试答案及解析
- 科学家视角:新质生产力的创新密码
- 2025三门县国企招聘考试题目及答案
- 2025-2030红色旅游行业市场发展现状及发展前景与投资机会研究报告
- 植筋施工方案 全
- 2025四川省前期物业服务合同示范文本
- 法院舆情风险防控课件
- 动态系统仿真技术-全面剖析
- 护理人员绩效考核制度
- 人教版六年级语文上册教学计划(含进度表)
- 苏教版科学五年级上册全册教案(含反思)
- 餐饮服务与数字化运营 习题及答案 项目六
- 天津地铁设备管理制度范文
评论
0/150
提交评论