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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验二 类与对象二.docx

1、实验二 类与对象二实验三 类与对象(2)【实验类型】验证性实验 【实验课时】2学时 【实验目的】(1)掌握对象数组定义与使用方法。(2)理解对象指针的概念,学会用指针引用对象。(3)了解this指针的工作方式。(4)掌握静态数据成员和静态成员函数的基本使用方法。(5)理解友元与友元函数的作用,掌握其使用方法。【实验环境】硬件:计算机软件:Microsoft Visual C+ 6.0【实验内容】1、按要求阅读、修攺、调试和运行以下程序。(1)实验内容 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和

2、当日收盘价。尽量发挥想象力,为该类设计成员函数。 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。 使用股票类Stock,定义一个对象数组存放连续5个交易日的股票信息。 编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中

3、显示用Assign_stock赋值的对象。 定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。若是,返回真,否则,返回假。(2)实验程序及运行结果(参考)以当日收盘价计算该股票今天的涨幅#include class Stockpublic: Stock(int n,double ma,double mi,double b,double e); void Set_Stock(int n,double ma,double mi,double b,double e); double Get_End(); void Show_Stock();private: int Number; d

4、ouble Max,Min,Begin,End;Stock:Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e; void Stock:Set_Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e;double Stock:Get_End() return End;void Stock:Show_Stock() coutNumbert;

5、 coutMaxt; coutMint; coutBegint; coutEndendl;void main() Stock s1(1,4.18,4.00,4.05,4.09); Stock s2(2,4.41,4.03,4.04,4.40); coutn(s2.Get_End()-s1.Get_End()/s1.Get_End()*100%endl;定义一个对象数组存放连续5个交易日的股票信息#include const int N=2;class Stockpublic: Stock(int n,double ma,double mi,double b,double e);Stock();

6、 /增加无输入值的构造函数 void Set_Stock(int n,double ma,double mi,double b,double e); void Set_Stock(); double Get_End(); void Show_Stock();private: int Number; double Max,Min,Begin,End;Stock:Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e;Stock:Stock() /无输入值时,将其都清零

7、Number=0; Max=0; Min=0; Begin=0; End=0;void Stock:Set_Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e;double Stock:Get_End() return End;void Stock:Show_Stock() coutNumbert; coutMaxt; coutMint; coutBegint; coutEndendl;void Stock:Set_Stock() coutNumber; cout

8、Max; coutMin; coutBegin; coutEnd;void main() int i; Stock s15,*p; for (i=0,p=s1;iSet_Stock(); for (i=0,p=s1;iShow_Stock(); for(i=1,p=s1+1;iN;i+,p+) coutnGet_End()-(p-1)-Get_End()/(p-1)-Get_End()*100%endl;定义一个静态数据成员,记录当前Stock对象的数量#include const int N=2;class Stockpublic: Stock() Stock(int n,double ma

9、,double mi,double b,double e); void Set_Stock(int n,double ma,double mi,double b,double e); void Set_Stock(); double Get_End(); void Show_Stock(); int Get_N_count();private: static int N_count; /静态数据成员 int Number; double Max,Min,Begin,End;int Stock:N_count=0; /此处定义时不需要加上staticStock:Stock() /无输入值时,将其

10、都清零 N_count+; Number=0; Max=0; Min=0; Begin=0; End=0;Stock:Stock(int n,double ma,double mi,double b,double e) N_count+; Number=n; Max=ma; Min=mi; Begin=b; End=e;void Stock:Set_Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e;double Stock:Get_End() return En

11、d;void Stock:Show_Stock() coutNumbert; coutMaxt; coutMint; coutBegint; coutEndendl;void Stock:Set_Stock()coutNumber; coutMax; coutMin; coutBegin; coutEnd;int Stock:Get_N_count() return N_count;void main() int i; Stock s15; Stock *p; for (i=0,p=s1;iSet_Stock(); for (i=0,p=s1;iShow_Stock(); for(i=1,p=

12、s1+1;iN;i+,p+) coutnGet_End()-(p-1)-Get_End()/(p-1)-Get_End()*100 %; coutnGet_N_count()endl;设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用#include const int N=2;class Stockpublic: Stock() Stock(int n,double ma,double mi,double b,double e); void Set_Stock(int n,double ma,double mi,double b,double e

13、); void Set_Stock(); void Assign_Stock(Stock& p); void Show_Stock();private:static int N_count; long Number; double Max,Min,Begin,End;int Stock:N_count=0;Stock:Stock() /无输入值时,将其都清零 N_count+; Number=0; Max=0; Min=0; Begin=0; End=0;Stock:Stock(int n,double ma,double mi,double b,double e) N_count+; Num

14、ber=n; Max=ma; Min=mi; Begin=b; End=e;void Stock:Set_Stock(int n,double ma,double mi,double b,double e) N_count+; Number=n; Max=ma; Min=mi; Begin=b; End=e;void Stock:Assign_Stock(Stock& p) if (this!=&p) Number=p. Number;Max=p.Max;Min=p.Min;Begin=p.Begin;End=p.End;void Stock:Show_Stock() coutNumbert;

15、 coutMaxt; coutMint; coutBegint; coutEndendl;void Stock:Set_Stock() coutNumber; coutMax; coutMin; coutBegin; coutEnd;void main()Stock s1(4,7.88,7.48, 7.56,7.68);Stock s2;s2.Assign_Stock(s1);s2.Show_Stock();定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。#include const int N=2;class Stockpublic: Stock() Stock(int n

16、,double ma,double mi,double b,double e); void Set_Stock(int n,double ma,double mi,double b,double e); void Set_Stock(); void Assign_Stock(Stock& p); void Show_Stock();double Get_max() return Max; /定义返回Max的函数 double Get_End() return End; /定义返回End的函数 int Get_N_count() return N_count; /定义返回N_count的函数in

17、t friend Get_Stock(Stock *s1);private:static int N_count; long Number; double Max,Min,Begin,End;int Stock:N_count=0;int Get_Stock(Stock *s1) coutendlBegintEnd; if (s1-Begin)(s1-End) return 1; else return 0 ;Stock:Stock() /无输入值时,将其都清零 N_count+; Number=0; Max=0; Min=0; Begin=0; End=0;Stock:Stock(int n

18、,double ma,double mi,double b,double e) N_count+; Number=n; Max=ma; Min=mi; Begin=b;End=e;void Stock:Set_Stock(int n,double ma,double mi,double b,double e) Number=n; Max=ma; Min=mi; Begin=b; End=e;void Stock:Assign_Stock(Stock& p) if (this!=&p) Number=p.Number;Max=p.Max;Min=p.Min;Begin=p.Begin;End=p

19、.End;void Stock:Show_Stock() coutNumbert; coutMaxt;coutMint; coutBegint; coutEndendl;void Stock:Set_Stock() N_count+; coutNumber; coutMax; coutMin; coutBegin; coutEnd;void main()int i;Stock s1100,*p;for (i=0,p=s1;iSet_Stock();for (i=0,p=s1;iN;i+,p+) coutnMax :Get_max();for (i=1,p=s1+1;iN;i+,p+) cout

20、nGet_End()-(p-1)-Get_End()/(p-1)-Get_End()*100 %;coutnGet_N_count(); /显示对象个数 for (i=0,p=s1;iN;i+,p+)coutnGet_Stock(p)endl; /友元函数调用2、直角坐标系问题(1)实验内容设计一个用来表示直角座标系上点的位置的Location类,然后在主程序中创建两个对象A和B,要求A在第三象限,B在第二象限,计算给定两点之间的距离并按如下格式输出结果 : A(x1,y1), B(x2,y2) Distance1=d Distance2=d其中: x1,y1,x2,y2为指定值,d为计算结果

21、。把这个Loation类的对象作为Rectangle类的对象的一个成员,即座标平面上的一个矩形由一个位置和矩形的长和宽决定。设计矩形类 Rectangle并输出给定点x2和y2的值,长和宽的值,以及矩形的面积 。(2)实验程序(参考)计算给定两点之间的距离#include iostream.h#include math.h class Location private:int x,y;public:Location(int i, int j):x(i),y(j) int Getx( ) return x;int Gety( ) return y;double distance(Location

22、 b);friend double distance(Location &a, Location &b);double distance(Location &a, Location &b) /友元函数 int dx=a.x-b.x; int dy=a.y-b.y; return sqrt(dx*dx+dy*dy); double Location:distance(Location b) /成员函数 int dx=x-b.x; int dy=y-b.y; return sqrt(dx*dx+dy*dy); void main( ) Location A(-10,-20),B(-40,60);c

23、outA(A.Getx( ),A.Gety( ),B(B.Getx( ),B.Gety( )endl;double d=A.distance(B); /调用成员函数coutDistance1= dendl; coutDistance2= distance(A,B)endl; /调用友元函数输出给定点x2和y2的值,长和宽的值,以及矩形的面积#include iostream.h#include math.h class Locationprivate: int x, y;public: Location(int i, int j): x(i),y(j) int Getx( ) return x

24、; int Gety( ) return y;class Rectrangle private: Location point; int H, W; int Square;public: Rectrangle(int x, int y, int h, int w); Location * Getpoint(); int GetHeight( ) return H; int GetWidth( ) return W; int GetSquare( ) return H*W; Rectrangle:Rectrangle(int x, int y, int h, int w): point(x, y) H = h; W = w; Location *Rectrangle:Getpoint( ) return &point; void main( ) Rectrangle rect(5, 2, 10, 20);Location *pPos = rect.Get

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

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