精品学习资源试验三 类与对象 〔2〕【试验类型】 验证性试验 【试验课时】 2 学时【试验目的】〔1〕把握对象数组定义与使用方法;〔2〕懂得对象指针的概念,学会用指针引用对象;〔3〕明白 this 指针的工作方式;〔4〕把握静态数据成员和静态成员函数的基本使用方法;〔5〕懂得友元与友元函数的作用,把握其使用方法;【试验环境】硬件:电脑【试验内容】1、按要求阅读、修攺、调试和运行以下程序;〔1〕试验内容① 定义一个类 Stock, 记录一支股票交易的基本信息,信息包括交易日序号〔表示本月的第几个交易日,用整数表示〕、当日最高价、当日最低价、当日开盘价和当日收盘价;尽 量发挥想象力,为该类设计成员函数;② 在主函数中建立两个股票对象,分别储备该股票昨天和今日两天的信息,以当日收盘价运算该股票今日的涨幅;③ 使用股票类 Stock ,定义一个对象数组存放连续 5 个交易日的股票信息; 编写一个主函数,运算两个 Stock 对象〔前后两日〕的当日收盘价运算当日涨幅;用指针引用对象数组中的两个对象;在主函数中调用该函数运算从第 2 个交易日开头每天的当日涨幅;④ 在 Stock 类中定义一个静态数据成员,记录当前 Stock 对象的数量;⑤ 设计一个成员函数 Assign_stock 为对象赋值 , 其中的形式参数是对另一个 Stock 对象的引用,使用 this 指针防止对自己的赋值, 在主函数中显示用 Assign_stock 赋值的对象;⑥ 定义一个友元函数运算 Stock 对象的当日开盘价是否高于当日收盘价;假设是,返回真,否就,返回假;〔2〕试验程序及运行结果〔参考〕①以当日收盘价运算该股票今日的涨幅#include class Stock{public:欢迎下载精品学习资源private: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〔〕;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〔〕{ cout< const int N=2;class Stock{public:欢迎下载精品学习资源private: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〔〕;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〔〕{ cout<>Number; cout<<"Max:"; cin>>Max; cout<<"Min:"; cin>>Min; cout<<"Begin:"; cin>>Begin; cout<<"End:"; cin>>End;}void main〔〕{ int i;Stock s1[5],*p;for 〔i=0,p=s1;iSet_Stock〔〕; for 〔i=0,p=s1;iShow_Stock〔〕;for〔i=1,p=s1+1;iGet_End〔〕-〔p-1〕->Get_End〔〕〕/〔p-1〕->Get_End〔〕*100<<"%"< const int N=2;class Stock{public:Stock〔〕 {}欢迎下载精品学习资源private:};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〔〕;static int N_count; // 静态数据成员int Number;double Max,Min,Begin,End;欢迎下载精品学习资源int Stock::N_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=b;End=e;}double Stock::Get_End〔〕{ return End;} void Stock::Show_Stock〔〕{ cout<>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 s1[5];Stock *p;for 〔i=0,p=s1;iSet_Stock〔〕; for 〔i=0,p=s1;iShow_Stock〔〕;for〔i=1,p=s1+1;iGet_End〔〕-〔p-1〕->Get_End〔〕〕/〔p-1〕->Get_End〔〕*100<<" %";cout<<"\n"<Get_N_count〔〕< const int N=2;class Stock{public: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;}。