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

加入VIP,免费下载
 

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

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

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++第三次实验.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++第三次实验.docx

1、C+第三次实验天津理工大学计算机与通信工程学院实验报告2013 至 2014 学年 第 二 学期课程名称高级语言程序设计实验( 3 )实验名称派生与继承实验时间 2013 年 4 月 21 日 第 3 节 至 第 6 节学号姓名专业信息安全主讲教师唐召东辅导教师唐召东软件环境VC+6硬件环境PC机实验目的(1)理解继承的含义,掌握派生类的定义方法和实现;(2)理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3)理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;(4)理解虚函数在类的继承层次中的

2、作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。实验内容(应包括实验题目、实验要求、实验任务等)二、练习项目:1.先定义“点”类Point,再由“点”类派生出“圆”类Circle。#include #define PI 3.14159class Point / 定义“点”类 int x, y;public: Point(int a=0, int b=0) x=a; y=b; void ShowPoint( ) coutPoint:(x,y)n; int Getx( ) return x; int Gety( ) return y;void Setxy(int a,

3、 int b) x=a; y=b; ; class Circle: public Point / 定义“圆”类,公有继承 int r; / “圆”的半径 public: Circle(int x, int y, int ra) : Point(x, y) / B r = ra; void Setr(int ra) r = ra; double Area( ) /求圆的面积 return PI*r*r; void Move(int x_offset, int y_offset) /将圆心坐标平移 int x1=Getx( ); /存取基类的私有成员 int y1=Gety( ); / D x1

4、+= x_offset; y1 += y_offset; Setxy(x1, y1); / E void ShowCircle( ) ShowPoint( ); / F cout Radius: rt; coutArea: Area( )endl; /G ;void main( ) Circle c(1, 1, 1); c.ShowCircle( ); c.Move(1, 2); c.ShowCircle( ); c.Setxy(4, 5); /重新置圆心坐标 c.Setr(2); /重新置半径值 c.ShowCircle( );(1)记录程序的运行结果。(2)测试能否将move函数体改写为x

5、=x+x_offset;y=y+y_offset;2. 先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。#include #define PI 3.14159class Point protected: /A int x, y;public: Point(int a=0, int b=0) x=a; y=b; void ShowPoint( ) coutPoint:(x,y)n; int Getx( ) return x; int Gety( ) return y; void Setxy(int a, int b) x=a; y

6、=b; ;class Radius protected: /B int r;public: Radius(int ra=0) r = ra; void Setr(int ra) r = ra; int Getr( ) return r; ;class Circle : public Point, public Radius public: Circle(int x, int y, int ra) : Point(x, y), Radius(ra) /D double Area( ) double Area( ) return PI*r*r; /直接访问基类的保护成员 void Move(int

7、 x_offset, int y_offset) x += x_offset; y += y_offset; void ShowCircle( ) ShowPoint( ); coutRadius: rt; coutArea: Area( )endl; ;void main( ) Circle c(1, 1, 1); c.ShowCircle( ); c.Move(1, 2); c.ShowCircle( ); c.Setxy(4, 5); c.Setr(2); c.ShowCircle( );(1)记录程序的运行结果(2)为什么可以直接使用x,y(3)如果x,y在基类中是私有的行吗?3. 多

8、重继承中基类构造函数和析构函数的调用顺序#include class Base1protected: int data1;public:Base1(int a=0) data1 = a; coutBase Constructor1n; Base1( ) coutBase Destructor1n; ;class Base2protected: int data2;public: Base2(int a=0) data2 = a; coutBase Constructor2n; Base2( ) coutBase Destructor2n; ;class Derived: public Base

9、1, public Base2 int d;public: Derived(int x, int y, int z):Base1(x), Base2(y) d=z; coutDerived Constructorn; Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3); c.Show( );(1)记录程序的运行结果4. 对象成员构造函数和析构函数的调用顺序#include class Base1protected: int data1;

10、public: Base1(int a=8) data1 = a; coutdata1, Base Constructor1n; Base1( ) coutdata1, Base Destructor1n; ;class Base2protected: int data2;public: Base2(int a=9) data2 = a; coutdata2, Base Constructor2n; Base2( ) coutdata2, Base Destructor2n; ;class Derived:public Base1, public Base2 int d; Base1 b1,

11、b2; public: Derived(int x, int y, int z) : Base1(x), Base2(y), b1(x+y), b2(x+z) d=z; coutDerived Constructorn; Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3); c.Show( );(1)记录程序的运行结果5. 编程题假设某商店有如下几种货品:衬衣、帽子、立柜。每一种货物都有与其关联的说明信息。衬衣:单价、产地、库存量、布料;

12、帽子:单价、产地、库存量、布料、样式(平顶或尖顶);立柜:单价、产地、库存量、木料、颜色。对这些商品的操作有:商品的进库(增加库存量),商品的出库(减少库存量),该类货品总价格的计算。要求自行设计数据结构,用类的继承与派生关系将上述的各种货品表示出来,并使用类的构造函数来初始化每一类对象的初始数据。而后将上述的商品管理计算机化,完成操作要求的功能。【实现方法提示】1设立3 个不同的类来描述与处理3 种不同的货品。首先注意到上述3 种货品数据之间的相互关联关系,可使用C+基类及其派生类的定义方法,先抽象出(“提取”出)如下每一货品都具有的 “公有”数据构成一个所谓的基类base,而后再派生出所需

13、的那3 个类。(1)base(基)类:单价、产地、库存量;(2)由base 作为基类,派生出shirt(衬衣)类:增加“布料”数据;(3)由base 出发,派生出wardrobe(立柜)类:增加“木料”与“颜色”数据;(4)而后又由shirt 类出发(作为基类),派生出cap(帽子)类:增加“样式”数据。2对应于要对各类数据所进行的操作,而设立出类的如下几个成员函数。(1)构造函数通过传递来的实参数据,来构造出每一对象所具有的各数据成员。如基类base 需要传递place、count 与price 三项数据,而派生类shirt 则需在base 数据的基础上增加第四项即布料数据material

14、等。(2)商品的进库(增加库存量)void in_something(int add_cnt);将对象的库存量count 增加一个数量add_cnt。(3)商品的出库(减少库存量)void out_something(int del_cnt);将对象的库存量count 减少一个数量del_cnt。(4)该类货品总价格的计算double total_price();通过使用“price*count”计算并返回对象所代表货品的总价格。(5)对象数据的输出。在屏幕上显示出对象所拥有的当前数据。实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)1.源代码:见实验内容中的源代码。输出结果

15、:测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;答:不能2.源代码:见实验内容中的源代码。输出结果:为什么可以直接使用x,y?答:x,y为保护继承,可以被派生类使用如果x,y在基类中是私有的行吗?答:不行3.源代码:见实验内容中的源代码。输出结果:4.源代码:见实验内容中的源代码。输出结果:5.源代码:count”计算并返回对象所代表货品的总价格。(5)对象数据的输出。在屏幕上显示出对象所拥有的当前数据。源代码如下:#include #include using namespace std;class Baseprivate: double price; s

16、tring place; int count;public: Base(double _price=0.0,string _place=NULL,int _count=0) price=_price; place=_place; count=_count; virtual Base(); void add(int x) count-=x; double total() return count*price; double getprice() return price; string getplace() return place; int getcount() return count; v

17、oid setprice(double _price=0.0) price=_price; void setplace(string _place=NULL) place=_place; void setcount(int _count) count=_count; void print() cout单价:price 产地:place 数量:count 总价:total()endl; ;class Shirt:public Baseprivate: string materal;public: Shirt(double _price=0.0,string _place=NULL,int _co

18、unt=0,string _materal=NULL):Base(_price,_place,_count) materal=_materal; virtual Shirt(); void setmateral(string _materal) materal=_materal; string getmateral() return materal; void print() Base:print(); cout布料:materalendl; ;class Wardrobe:public Baseprivate: string materal; string color;public: War

19、drobe(double _price=0.0,string _place=NULL,int _count=0,string _materal=NULL,string _color=NULL):Base(_price,_place,_count) materal=_materal; color=_color; Wardrobe() void setmateral(string _materal) materal=_materal; string getmateral() return materal; void setcolor(string _color) color=_color; str

20、ing getcolor() return color; void print() Base:print(); cout木料:materal 颜色:colorendl; ;class Cap:public Shirtprivate: string style;public: Cap(double _price=0.0,string _place=NULL,int _count=0,string _materal=NULL,string _style=NULL):Shirt(_price,_place,_count,_materal) style=_style; Cap() void setst

21、yle(string _style) style=_style; string getstyle() return style; void print() Shirt:print(); cout样式:styleendl; ;int main() Shirt s(100,上海,100,布料); Wardrobe w(1000,天津,10,檀木,黑色); Cap c(20,北京,1000,布料,样式); s.print(); w.print(); c.print(); s.add(10); w.add(20); c.add(30); cout-endl; s.print(); w.print(); c.print(); return 0;输出结果:附录(可包括源程序清单或其它说明)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1