實(shí)驗(yàn)二類與對象_第1頁
實(shí)驗(yàn)二類與對象_第2頁
實(shí)驗(yàn)二類與對象_第3頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)三 類與對象 (2)【實(shí)驗(yàn)類型】 驗(yàn)證性實(shí)驗(yàn) 【實(shí)驗(yàn)課時】 2 學(xué)時 【實(shí)驗(yàn)?zāi)康摹浚?)掌握對象數(shù)組定義與使用方法。 (2)理解對象指針的概念,學(xué)會用指針引用對象。( 3)了解 this 指針的工作方式。(4)掌握靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的基本使用方法。(5)理解友元與友元函數(shù)的作用,掌握其使用方法?!緦?shí)驗(yàn)環(huán)境】硬件:計(jì)算機(jī)軟件: Microsoft Visual C+ 6.0【實(shí)驗(yàn)內(nèi)容】1、按要求閱讀、修攺、調(diào)試和運(yùn)行以下程序。( 1)實(shí)驗(yàn)內(nèi)容 定義一個類 Stock, 記錄一支股票交易的基本信息,信息包括交易日序號(表示本月 的第幾個交易日,用整數(shù)表示)、當(dāng)日最高價、當(dāng)日最低價、當(dāng)日

2、開盤價和當(dāng)日收盤價。盡 量發(fā)揮想象力,為該類設(shè)計(jì)成員函數(shù)。 在主函數(shù)中建立兩個股票對象,分別存儲該股票昨天和今天兩天的信息,以當(dāng)日收 盤價計(jì)算該股票今天的漲幅。 使用股票類 Stock ,定義一個對象數(shù)組存放連續(xù) 5 個交易日的股票信息。 編寫一個 主函數(shù),計(jì)算兩個 Stock 對象(前后兩日)的當(dāng)日收盤價計(jì)算當(dāng)日漲幅。用指針引用對象數(shù) 組中的兩個對象。在主函數(shù)中調(diào)用該函數(shù)計(jì)算從第 2 個交易日開始每天的當(dāng)日漲幅。 在 Stock 類中定義一個靜態(tài)數(shù)據(jù)成員,記錄當(dāng)前 Stock 對象的數(shù)量。 設(shè)計(jì)一個成員函數(shù) Assign_stock 為對象賦值 , 其中的形式參數(shù)是對另一個 Stock 對

3、象的引用,使用 this 指針避免對自己的賦值, 在主函數(shù)中顯示用 Assign_stock 賦值的對象。 定義一個友元函數(shù)計(jì)算 Stock 對象的當(dāng)日開盤價是否高于當(dāng)日收盤價。若是,返回 真,否則,返回假。(2)實(shí)驗(yàn)程序及運(yùn)行結(jié)果(參考)以當(dāng)日收盤價計(jì)算該股票今天的漲幅#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

4、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

5、End; void Stock:Show_Stock() cout<<Number<<"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<<&qu

6、ot;n"<<(s2.Get_End()-s1.Get_End()/s1.Get_End()*100<<"%"<<endl; 定義一個對象數(shù)組存放連續(xù) 5 個交易日的股票信息#include <iostream.h>const int N=2;class Stockpublic:Stock(int n,double ma,double mi,double b,double e); Stock(); / 增加無輸入值的構(gòu)造函數(shù) void Set_Stock(int n,double ma,double mi,doubl

7、e 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,doubl

8、e 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<<Number<<"t"cout<<Max<<"t"cout<<Min<<"t" cout<<Begin<<"t"cout<<End<

9、<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() int i;Stock s15,*p;for (i=0,

10、p=s1;i<N;i+,p+) p->Set_Stock();for (i=0,p=s1;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;定義一個靜態(tài)數(shù)據(jù)成員,記錄當(dāng)前Stock對象的數(shù)量#include <iostream.h>const i

11、nt 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;/ 靜態(tài)數(shù)據(jù)成員int Number;double Max,Min,Begin,End; ;int Stock:N

12、_count=0; / 此處定義時不需要加上 static 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

13、=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<<"Number:"

14、;cin>>Number;cout<<"Max:"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-

15、>Set_Stock();for (i=0,p=s1;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<<" %"cout<<"n"<<p->Get_N_count()<<endl; 設(shè)計(jì)一個成員函數(shù) Assign_stock 為對象賦

16、值 , 其中的形式參數(shù)是對另一個 Stock 對象 的引用#include <iostream.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

17、;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_

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

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

20、quot;cin>>End;void main()Stock s1(4,7.88,7.48, 7.56,7.68);Stock s2; s2.Assign_Stock(s1);s2.Show_Stock();定義一個友元函數(shù)計(jì)算 Stock 對象的當(dāng)日開盤價是否高于當(dāng)日收盤價。#include <iostream.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,doubl

21、e mi,double b,double e);void Set_Stock();void Assign_Stock(Stock& p);/ 定義返回 Max 的函數(shù)/ 定義返回 End 的函數(shù)/ 定義返回 N_count 的函數(shù)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;

22、 double Max,Min,Begin,End;int Stock:N_count=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

23、 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;v

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

25、Max:" cin>>Max;cout<<"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 :"&

26、lt;<p->Get_max();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(); 數(shù)for (i=0,p=s1;i<N;i+,p+) cout<<"n"<<Get_Stock(

27、p)<<endl; / 友元函數(shù)調(diào)用 2、直角坐標(biāo)系問題( 1)實(shí)驗(yàn)內(nèi)容設(shè)計(jì)一個用來表示直角座標(biāo)系上點(diǎn)的位置的 Location 類,然后在主程序中創(chuàng)建兩個 對象A和B,要求A在第三象限,B在第二象限,計(jì)算給定兩點(diǎn)之間的距離并按如下格式輸 出結(jié)果 :A(x1,y1), B(x2,y2) Distance1=d Distance2=d其中 : x1,y1,x2,y2 為指定值, d 為計(jì)算結(jié)果。把這個 Loation 類的對象作為 Rectangle 類的對象的一個成員, 即座標(biāo)平面上的一個 矩形由一個位置和矩形的長和寬決定。 設(shè)計(jì)矩形類 Rectangle 并輸出給定點(diǎn) x2 和

28、y2 的值, 長和寬的值 , 以及矩形的面積 。(2)實(shí)驗(yàn)程序(參考)計(jì)算給定兩點(diǎn)之間的距離#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

29、);double distance(Location &a, Location &b) / 友元函數(shù) int dx=a.x-b.x;int dy=a.y-b.y;return sqrt(dx*dx+dy*dy); double Location:distance(Location b) / 成員函數(shù) 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( )<

30、<","<<A.Gety( )<<"),B("<<B.Getx( )<<","<<B.Gety( )<<")"<<endl;double d=A.distance(B);/ 調(diào)用成員函數(shù)cout<<"Distance1= "<<d<<endl;cout<<"Distance2= "<<distance(A,B)<<e

31、ndl;/ 調(diào)用友元函數(shù)輸出給定點(diǎn)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 * Getpoint(); in

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論