实验二 类与对象二.docx
- 文档编号:1852001
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:18
- 大小:20.28KB
实验二 类与对象二.docx
《实验二 类与对象二.docx》由会员分享,可在线阅读,更多相关《实验二 类与对象二.docx(18页珍藏版)》请在冰点文库上搜索。
实验二类与对象二
实验三 类与对象
(2)
【实验类型】验证性实验【实验课时】2学时
【实验目的】
(1)掌握对象数组定义与使用方法。
(2)理解对象指针的概念,学会用指针引用对象。
(3)了解this指针的工作方式。
(4)掌握静态数据成员和静态成员函数的基本使用方法。
(5)理解友元与友元函数的作用,掌握其使用方法。
【实验环境】
硬件:
计算机
软件:
MicrosoftVisualC++6.0
【实验内容】
1、按要求阅读、修攺、调试和运行以下程序。
(1)实验内容
①定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。
尽量发挥想象力,为该类设计成员函数。
②在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。
③使用股票类Stock,定义一个对象数组存放连续5个交易日的股票信息。
编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价计算当日涨幅。
用指针引用对象数组中的两个对象。
在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。
④在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。
⑤设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用Assign_stock赋值的对象。
⑥定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。
若是,返回真,否则,返回假。
(2)实验程序及运行结果(参考)
①以当日收盘价计算该股票今天的涨幅
#include
classStock
{public:
Stock(intn,doublema,doublemi,doubleb,doublee);
voidSet_Stock(intn,doublema,doublemi,doubleb,doublee);
doubleGet_End();
voidShow_Stock();
private:
intNumber;
doubleMax,Min,Begin,End;};
Stock:
:
Stock(intn,doublema,doublemi,doubleb,doublee)
{ Number=n;
Max=ma;
Min=mi;
Begin=b;
End=e;}
voidStock:
:
Set_Stock(intn,doublema,doublemi,doubleb,doublee)
{ Number=n;
Max=ma;
Min=mi;
Begin=b;
End=e;}
doubleStock:
:
Get_End()
{ returnEnd;}
voidStock:
:
Show_Stock()
{ cout< cout< cout< cout< cout< voidmain() { Stocks1(1,4.18,4.00,4.05,4.09); Stocks2(2,4.41,4.03,4.04,4.40); cout<<"\n"<<(s2.Get_End()-s1.Get_End())/s1.Get_End()*100<<"%"< ②定义一个对象数组存放连续5个交易日的股票信息 #include constintN=2; classStock {public: Stock(intn,doublema,doublemi,doubleb,doublee); Stock(); //增加无输入值的构造函数 voidSet_Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(); doubleGet_End(); voidShow_Stock(); private: intNumber; doubleMax,Min,Begin,End;}; Stock: : Stock(intn,doublema,doublemi,doubleb,doublee) { Number=n; Max=ma; Min=mi; Begin=b; End=e;} Stock: : Stock() //无输入值时,将其都清零 { Number=0; Max=0; Min=0; Begin=0; End=0;} voidStock: : Set_Stock(intn,doublema,doublemi,doubleb,doublee) { Number=n; Max=ma; Min=mi; Begin=b; End=e;} doubleStock: : Get_End() { returnEnd;} voidStock: : Show_Stock() { cout< cout< cout< cout< cout< voidStock: : Set_Stock() { cout<<"Number: "; cin>>Number; cout<<"Max: "; cin>>Max; cout<<"Min: "; cin>>Min; cout<<"Begin: "; cin>>Begin; cout<<"End: "; cin>>End;} voidmain() { inti; Stocks1[5],*p; for(i=0,p=s1;i p->Set_Stock(); for(i=0,p=s1;i p->Show_Stock(); for(i=1,p=s1+1;i ③定义一个静态数据成员,记录当前Stock对象的数量 #include constintN=2; classStock {public: Stock() {} Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(); doubleGet_End(); voidShow_Stock(); intGet_N_count(); private: staticintN_count; //静态数据成员 intNumber; doubleMax,Min,Begin,End; }; intStock: : N_count=0; //此处定义时不需要加上static Stock: : Stock() //无输入值时,将其都清零 { N_count++; Number=0; Max=0; Min=0; Begin=0; End=0;} Stock: : Stock(intn,doublema,doublemi,doubleb,doublee) { N_count++; Number=n; Max=ma; Min=mi; Begin=b; End=e;} voidStock: : Set_Stock(intn,doublema,doublemi,doubleb,doublee) { Number=n; Max=ma; Min=mi; Begin=b; End=e;} doubleStock: : Get_End() { returnEnd;} voidStock: : Show_Stock() { cout< cout< cout< cout< cout< voidStock: : Set_Stock() { cout<<"Number: "; cin>>Number; cout<<"Max: "; cin>>Max; cout<<"Min: "; cin>>Min; cout<<"Begin: "; cin>>Begin; cout<<"End: "; cin>>End;} intStock: : Get_N_count() { returnN_count;} voidmain() { inti; Stocks1[5]; Stock*p; for(i=0,p=s1;i p->Set_Stock(); for(i=0,p=s1;i p->Show_Stock(); for(i=1,p=s1+1;i cout<<"\n"< ④设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用 #include constintN=2; classStock {public: Stock(){} Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(); voidAssign_Stock(Stock&p); voidShow_Stock(); private: staticintN_count; longNumber; doubleMax,Min,Begin,End; }; intStock: : N_count=0; Stock: : Stock() //无输入值时,将其都清零 { N_count++; Number=0; Max=0; Min=0; Begin=0; End=0;} Stock: : Stock(intn,doublema,doublemi,doubleb,doublee) { N_count++; Number=n; Max=ma; Min=mi; Begin=b; End=e;} voidStock: : Set_Stock(intn,doublema,doublemi,doubleb,doublee) { N_count++; Number=n; Max=ma; Min=mi; Begin=b; End=e;} voidStock: : Assign_Stock(Stock&p) { if(this! =&p) { Number=p.Number; Max=p.Max; Min=p.Min; Begin=p.Begin; End=p.End; } } voidStock: : Show_Stock() { cout< cout< cout< cout< cout< voidStock: : Set_Stock() { cout<<"Number: "; cin>>Number; cout<<"Max: "; cin>>Max; cout<<"Min: "; cin>>Min; cout<<"Begin: "; cin>>Begin; cout<<"End: "; cin>>End; } voidmain() { Stocks1(4,7.88,7.48,7.56,7.68);Stocks2; s2.Assign_Stock(s1); s2.Show_Stock();} ⑤定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。 #include constintN=2; classStock {public: Stock(){} Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(intn,doublema,doublemi,doubleb,doublee); voidSet_Stock(); voidAssign_Stock(Stock&p); voidShow_Stock(); doubleGet_max(){returnMax;} //定义返回Max的函数 doubleGet_End(){returnEnd;} //定义返回End的函数 intGet_N_count(){returnN_count;} //定义返回N_count的函数 intfriendGet_Stock(Stock*s1); private: staticintN_count; longNumber; doubleMax,Min,Begin,End; }; intStock: : N_count=0; intGet_Stock(Stock*s1) { cout< if((s1->Begin)>(s1->End))return1; elsereturn0; } Stock: : Stock() //无输入值时,将其都清零 { N_count++; Number=0; Max=0; Min=0; Begin=0; End=0;} Stock: : Stock(intn,doublema,doublemi,doubleb,doublee) { N_count++; Number=n; Max=ma; Min=mi; Begin=b;End=e;} voidStock: : Set_Stock(intn,doublema,doublemi,doubleb,doublee) { Number=n; Max=ma; Min=mi; Begin=b; End=e;} voidStock: : Assign_Stock(Stock&p) { if(this! =&p) { Number=p.Number; Max=p.Max;Min=p.Min; Begin=p.Begin;End=p.End; } } voidStock: : Show_Stock() { cout< cout< cout< cout< } voidStock: : Set_Stock() { N_count++; cout<<"Number: ";cin>>Number; cout<<"Max: "; cin>>Max; cout<<"Min: ";cin>>Min; cout<<"Begin: ";cin>>Begin; cout<<"End: ";cin>>End; } voidmain() { inti; Stocks1[100],*p; for(i=0,p=s1;i p->Set_Stock(); for(i=0,p=s1;i cout<<"\nMax: "< for(i=1,p=s1+1;i cout<<"\n"<<(p->Get_End()-(p-1)->Get_End())/(p-1)->Get_End()*100<<"%"; cout<<"\n"< for(i=0,p=s1;i cout<<"\n"< 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" classLocation{ private: intx,y; public: Location(inti,intj): x(i),y(j){} intGetx(){returnx;} intGety(){returny;} doubledistance(Locationb); frienddoubledistance(Location&a,Location&b); }; doubledistance(Location&a,Location&b) //友元函数 { intdx=a.x-b.x; intdy=a.y-b.y; returnsqrt(dx*dx+dy*dy);} doubleLocation: : distance(Locationb) //成员函数 { intdx=x-b.x; intdy=y-b.y; returnsqrt(dx*dx+dy*dy);} voidmain() { LocationA(-10,-20),B(-40,60); cout<<"A("< doubled=A.distance(B); //调用成员函数 cout<<"Distance1="< cout<<"Distance2="< ②输出给定点x2和y2的值,长和宽的值,以及矩形的面积 #include"iostream.h" #include"math.h" classLocation{ private: intx,y; public: Location(inti,intj): x(i),y(j){} intGetx(){returnx;} intGety(){returny;} }; classRectrangle{ private: Locationpoint; intH,W; intSquare; public: Rectrangle(intx,inty,inth,intw); Location*Getpoint(); intGetHeight(){returnH;} intGetWidth() {returnW;} intGetSquare(){returnH*W;} }; Rectrangle: : Rectrangle(intx,inty,inth,intw): point(x,y) {H=h;W=w;} Location*Rectrangle: : Getpoint() {return&point;} voidmain() { Rectranglerect(5,2,10,20); Location*pPos=rect.Get
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验二 类与对象二 实验 对象
![提示](https://static.bingdoc.com/images/bang_tan.gif)