实验二类与对象二Word格式文档下载.docx

上传人:b****6 文档编号:20468429 上传时间:2023-01-23 格式:DOCX 页数:15 大小:17.99KB
下载 相关 举报
实验二类与对象二Word格式文档下载.docx_第1页
第1页 / 共15页
实验二类与对象二Word格式文档下载.docx_第2页
第2页 / 共15页
实验二类与对象二Word格式文档下载.docx_第3页
第3页 / 共15页
实验二类与对象二Word格式文档下载.docx_第4页
第4页 / 共15页
实验二类与对象二Word格式文档下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验二类与对象二Word格式文档下载.docx

《实验二类与对象二Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验二类与对象二Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。

实验二类与对象二Word格式文档下载.docx

〔2〕实验程序及运行结果〔参考〕

①以当日收盘价计算该股票今天的涨幅

#include<

iostream.h>

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)

}

doubleStock:

Get_End()

returnEnd;

Show_Stock() 

cout<

<

Number<

"

\t"

;

Max<

Min<

Begin<

End<

endl;

voidmain()

Stocks1(1,4.18,4.00,4.05,4.09);

 

Stocks2(2,4.41,4.03,4.04,4.40);

\n"

(s2.Get_End()-s1.Get_End())/s1.Get_End()*100<

%"

②定义一个对象数组存放连续5个交易日的股票信息

constintN=2;

Stock();

//增加无输入值的构造函数

voidSet_Stock();

Stock() 

//无输入值时,将其都清零

Number=0;

Max=0;

Min=0;

Begin=0;

End=0;

Show_Stock()

Set_Stock()

Number:

cin>

>

Number;

Max:

Max;

Min:

Min;

Begin:

cin>

Begin;

End:

End;

inti;

Stocks1[5],*p;

for(i=0,p=s1;

i<

N;

i++,p++)

p->

Set_Stock();

Show_Stock();

for(i=1,p=s1+1;

i++,p++) 

(p->

Get_End()-(p-1)->

Get_End())/(p-1)->

Get_End()*100<

③定义一个静态数据成员,记录当前Stock对象的数量

Stock() 

{}

intGet_N_count();

staticintN_count;

//静态数据成员

intStock:

N_count=0;

//此处定义时不需要加上static

N_count++;

N_count++;

returnEnd;

cout<

Get_N_count()

returnN_count;

Stocks1[5];

Stock*p;

%"

p->

Get_N_count()<

④设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用

Stock(){}

voidAssign_Stock(Stock&

p);

staticintN_count;

longNumber;

Assign_Stock(Stock&

p)

if(this!

=&

p) 

{

Number=p.Number;

Max=p.Max;

Min=p.Min;

Begin=p.Begin;

End=p.End;

{

Stocks1(4,7.88,7.48,7.56,7.68);

Stocks2;

s2.Assign_Stock(s1);

s2.Show_Stock();

⑤定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。

doubleGet_max(){returnMax;

//定义返回Max的函数

doubleGet_End(){returnEnd;

//定义返回End的函数

intGet_N_count(){returnN_count;

//定义返回N_count的函数

intfriendGet_Stock(Stock*s1);

intGet_Stock(Stock*s1)

endl<

s1->

if((s1->

Begin)>

(s1->

End))return1;

elsereturn0;

End=e;

p)

Number=p.Number;

inti;

Stocks1[100],*p;

for(i=0,p=s1;

\nMax:

Get_max();

for(i=1,p=s1+1;

Get_N_count();

//显示对象个数 

Get_Stock(p)<

//友元函数调用}

2、直角坐标系问题

①设计一个用来表示直角座标系上点的位置的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"

math.h"

classLocation{

intx,y;

public:

Location(inti,intj):

x(i),y(j){}

intGetx(){returnx;

intGety(){returny;

doubledistance(Locationb);

frienddoubledistance(Location&

a,Location&

b);

doubledistance(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;

voidmain()

{

LocationA(-10,-20),B(-40,60);

A("

A.Getx()<

"

A.Gety()<

),B("

B.Getx()<

B.Gety()<

)"

doubled=A.distance(B);

//调用成员函数

Distance1="

d<

Distance2="

distance(A,B)<

//调用友元函数 

②输出给定点x2和y2的值,长和宽的值,以及矩形的面积

classLocation{

intx,y;

Location(inti,intj):

x(i),y(j){}

intGetx(){returnx;

intGety(){returny;

classRectrangle{

Locationpoint;

intH,W;

intSquare;

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;

Rectranglerect(5,2,10,20);

Location*pPos=rect.Getpoint();

cout<

"

x="

pPos

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

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

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