1. 1、本文档共14页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C实验七

第七次实验报告实验目的:练习使用派生,继承,熟悉掌握不同继承方式的使用,派生类的方法等。一、基础题1.P266 7-5定义一个Shape基类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。试题分析及核心代码:创建一个shape类含有GetArea()函数,派生出Rectangle和Circle,使用Rectangle创建对象square。#includeiostreamusing namespace std;#define PI 3.1416class shape{public:shape(float lenth,float width);shape(float R);protected:floatlen,wid,r;};shape::shape(float lenth,float width){ len=lenth;wid=width;}shape::shape(float R){r=R;}classRectangle:public shape{public:Rectangle(float lenth,float width):shape(lenth,width){}floatGetArea();};float Rectangle::GetArea(){float a=len*wid;return a;coutThe area is aendl;}classCircle:public shape{public:Circle(float R):shape(R){}floatGetArea();};float Circle::GetArea(){float a=PI*(r*r);return a;}void main(){floata,b,c;cout请输入矩形的长,宽endl;cinab;Rectangle square(a,b);floatsarea=square.GetArea();coutThe area is sareaendl;cout请输入圆的半径endl;cinc;Circle circle(c);floatcarea=circle.GetArea();coutThe area is careaendl;}运行结果及实验小结:输入长宽和半径分别能计算出矩形和圆的面积。2. P266 7-6定义一个哺乳动物Mammal类,再由此派生出狗Dog类,定义一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。核心代码:#includeiostream#includestringusing namespace std;class mammal{public:mammal(string Sex,intAge,float Weight){sex=Sex;age=Age;weight=Weight;cout调用mammal构造函数endl;}~mammal(){cout调用mammal析构函数!endl;}private:string sex;int age;float weight;};classdog:public mammal{public:dog(string n,mammal m):mammal(m){name=n;cout调用dog构造函数!endl;}~dog(){cout调用dog析构函数!endl;}private:string name;};int main(){mammal m(male,5,12);dog a(Green,m);return 0;}运行结果:二、提高题1、设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。试题分析及核心代码:创建一个building类含有num,floor,count,area成员,由building派生出教学楼Teach-Building和宿舍楼类Dorm-Building分别含有getinformaition和setname两函数用于接受信息和设置名字。核心代码:#includeiostream#includestringusing namespace std;#define N 30class Building{public:intnum,floor,count,area;protected:};classTeach_Building:public Building{public:voidset_name(string Name);voidgetinformation(i

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档