实验二 类与对象(二).doc_第1页
实验二 类与对象(二).doc_第2页
实验二 类与对象(二).doc_第3页
实验二 类与对象(二).doc_第4页
实验二 类与对象(二).doc_第5页
已阅读5页,还剩6页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验三 类与对象(2)【实验类型】验证性实验 【实验课时】2学时 【实验目的】(1)掌握对象数组定义与使用方法。(2)理解对象指针的概念,学会用指针引用对象。(3)了解this指针的工作方式。(4)掌握静态数据成员和静态成员函数的基本使用方法。(5)理解友元与友元函数的作用,掌握其使用方法。【实验环境】硬件:计算机软件:Microsoft Visual C+ 6.0【实验内容】1、按要求阅读、修攺、调试和运行以下程序。(1)实验内容 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。 使用股票类Stock,定义一个对象数组存放连续5个交易日的股票信息。 编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用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; 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; 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 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(); /增加无输入值的构造函数 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() /无输入值时,将其都清零 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; coutMax; 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,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() /无输入值时,将其都清零 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 End;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=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); 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+; Number=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; 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,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的函数int 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,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.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+) coutnGet_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为计算结果。把这个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 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);coutA(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; 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 * Getpoi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论