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

下载本文档

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

文档简介

1、实验三 类与对象 (2)【实验类型】 验证性实验 【实验课时】 2 学时 【实验目的】1掌握对象数组定义与使用方法。 2理解对象指针的概念,学会用指针引用对象。 3了解 this 指针的工作方式。4掌握静态数据成员和静态成员函数的根本使用方法。 5理解友元与友元函数的作用,掌握其使用方法。【实验环境】硬件:电脑【实验内容】1、按要求阅读、修攺、调试和运行以下程序。 1实验内容 定义一个类 Stock, 记录一支股票交易的根本信息,信息包括交易日序号表示本月 的第几个交易日,用整数表示、当日最高价、当日最低价、当日开盘价和当日收盘价。尽 量发挥想象力,为该类设计成员函数。 在主函数中建立两个股票

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

3、ock 赋值的对象。 定义一个友元函数计算 Stock 对象的当日开盘价是否高于当日收盘价。假设是,返 回真,否那么,返回假。2实验程序及运行结果参考以当日收盘价计算该股票今天的涨幅#include <iostream.h>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 Numbe

4、r;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() cout<<Numb

5、er<<"t"cout<<Max<<"t" cout<<Min<<"t"cout<<Begin<<"t"cout<<End<<endl;void main() Stock s1(1,4.18,4.00,4.05,4.09); Stock s2(2,4.41,4.03,4.04,4.40);cout<<"n"<<(s2.Get_End()-s1.Get_End()/s

6、1.Get_End()*100<<"%"<<endl; 定义一个对象数组存放连续 5 个交易日的股票信息#include <iostream.h>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()

7、;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=m

8、a;Min=mi;Begin=b;End=e; double Stock:Get_End() return End; void Stock:Show_Stock() cout<<Number<<"t"cout<<Max<<"t"cout<<Min<<"t"cout<<Begin<<"t"cout<<End<<endl;void Stock:Set_Stock() cout<<"

9、;Number:"cin>>Number;cout<<"Max:"cin>>Max;cout<<"Min:"cin>>Min; cout<<"Begin:" cin>>Begin; cout<<"End:"cin>>End;void main() int i;Stock s15,*p;for (i=0,p=s1;i<N;i+,p+)p->Set_Stock();for (i=0,p=s1

10、;i<N;i+,p+) p->Show_Stock();for(i=1,p=s1+1;i<N;i+,p+) cout<<"n"<<(p->Get_End()-(p-1 )->Get_End()/(p-1)->Get_End()*100<<"%"<<endl;定义一个静态数据成员,记录当前Stock对象的数量#include <iostream.h>const int N=2;class Stockpublic:Stock() Stock(int n,doubl

11、e 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; / 此处定义时不需要加上 static Stock:Stock() / 无输

12、入值时,将其都清零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

13、 Stock:Show_Stock() cout<<Number<<"t"cout<<Max<<"t" cout<<Min<<"t" cout<<Begin<<"t"cout<<End<<endl; void Stock:Set_Stock() cout<<"Number:"cin>>Number;cout<<"Max:"

14、cin>>Max;cout<<"Min:"cin>>Min;cout<<"Begin:"cin>>Begin;cout<<"End:"cin>>End;int Stock:Get_N_count()return N_count;void main() int i;Stock s15;Stock *p;for (i=0,p=s1;i<N;i+,p+) p->Set_Stock();for (i=0,p=s1;i<N;i+,p+) p-&

15、gt;Show_Stock();for(i=1,p=s1+1;i<N;i+,p+) cout<<"n"<<(p->Get_End()-(p-1)->Get _End()/(p-1)->Get_End()*100<<" %"cout<<"n"<<p->Get_N_count()<<endl; 设计一个成员函数 Assign_stock 为对象赋值 , 其中的形式参数是对另一个 Stock 对象 的引用#include <iostr

16、eam.h>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

17、: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; v

18、oid 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() cout<<Number<<"t"cout<<Max<<"t" cout<<Min<<"t" cout<<Begin<<"t" cout<

19、<End<<endl; void Stock:Set_Stock() cout<<"Number:" cin>>Number; cout<<"Max:"cin>>Max; cout<<"Min:" cin>>Min; cout<<"Begin:"cin>>Begin; cout<<"End:" cin>>End;void main()Stock s1(4,7.

20、88,7.48, 7.56,7.68);Stock s2; s2.Assign_Stock(s1);s2.Show_Stock();定义一个友元函数计算 Stock 对象的当日开盘价是否高于当日收盘价。 #include <iostream.h> const int N=2; class Stockpublic:Stock() Stock(int n,double ma,double mi,double b,double e);Max的函数End 的函数 count 的函数/ 定义返回/ 定义返回/ 定义返回 Nvoid Set_Stock(int n,double ma,doub

21、le mi,double b,double e); void Set_Stock(); void Assign_Stock(Stock& p); void Show_Stock();double Get_max() return Max; double Get_End() return End; int Get_N_count() return N_count; int friend Get_Stock(Stock *s1);private:static int N_count;long Number;double Max,Min,Begin,End;int Stock:N_count

22、=0;int Get_Stock(Stock *s1) cout<<endl<<s1->Begin<<"t"<<s1->End; 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+

23、;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() cout<<Number

24、<<"t"cout<<Max<<"t"cout<<Min<<"t"cout<<Begin<<"t"cout<<End<<endl;void Stock:Set_Stock() N_count+;cout<<"Number:" cin>>Number;cout<<"Max:" cin>>Max;cout<<&qu

25、ot;Min:" cin>>Min;cout<<"Begin:" cin>>Begin; cout<<"End:"cin>>End;void main()int i;Stock s1100 , *p;for (i=0,p=s1;i<N;i+,p+)p->Set_Stock();for (i=0,p=s1;i<N;i+,p+) cout<<"nMax :"<<p->Get_max();for (i=1,p=s1+1;i&

26、lt;N;i+,p+)cout<<"n"<<(p->Get_End()-(p-1)->Get_End()/(p-1)->Get_End()*100<<" %" cout<<"n"<<p->Get_N_count(); / 显示对象个for (i=0,p=s1;i<N;i+,p+) cout<<"n"<<Get_Stock(p)<<endl; / 友元函数调用 2、直角坐标系问题 1实验内容设

27、计一个用来表示直角座标系上点的位置的 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实验程序参考计算给定两点之间的距离

28、#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 &a

29、mp;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);cout<<"A("<<A.Getx( )<<","<<A.Gety( )<<&quo

30、t;),B("<<B.Getx( )<<","<<B.Gety( )<<")"<<endl;double d=A.distance(B);/ 调用成员函数cout<<"Distance1= "<<d<<endl;cout<<"Distance2= "<<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

温馨提示

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

评论

0/150

提交评论