C++ 第三次实验

实验3 类与对象(二)

实验步骤:
第一步:定义类Stock,并计算两个Stock对象(前后两日)的当日收盘价计算当日涨幅。

#include<iostream>
using namespace std;
const int N=5;
class Stock
{
private:
    int Number;
    float Max,Min,Begin,End;
public:
    Stock(){};
    Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock();
    float Get_End();
    void Show_Stock();
};
Stock::Stock(int n,float ma,float mi,float b,float e)
{
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
void Stock::Set_Stock(int n,float ma,float mi,float b,float e)
{
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
float Stock::Get_End()
{
    return End;
}
void Stock::Show_Stock()
{
    cout<<Number<<"\t";
    cout<<Max<<"\t";
    cout<<Min<<"\t";
    cout<<Begin<<"\t";
    cout<<End<<"\t";
}
void Stock::Set_Stock()
{
    cout<<"Number:";
    cin>>Number;
    cout<<"Max:";
    cin>>Max;
    cout<<"Min:";
    cin>>Min;
    cout<<"Begin:";
    cin>>Begin;
    cout<<"End:";
    cin>>End;
}
int main()
{
    int i;
    Stock s1[100];
    Stock *p;
    for(i=0,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;
    cout<<" %";
    return 0;
}

第二步:加静态数据成员

#include<iostream>
using namespace std;
const int N=2;
class Stock
{
private:
    static int N_count;         //静态数据成员
    int Number;
    float Max,Min,Begin,End;
public:
    Stock(){};
    Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock();
    float Get_End();
    void Show_Stock();
    int Get_N_count();
};
int Stock::N_count=0;
Stock::Stock(int n,float ma,float mi,float b,float e)
{
    N_count++;
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
void Stock::Set_Stock(int n,float ma,float mi,float b,float e)
{
    N_count++;
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
float Stock::Get_End()
{
    return End;
}
void Stock::Show_Stock()
{
    cout<<Number<<"\t";
    cout<<Max<<"\t";
    cout<<Min<<"\t";
    cout<<Begin<<"\t";
    cout<<End<<"\t";
}
void Stock::Set_Stock()
{
    N_count++;
    cout<<"Number:";
    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;
}
int main()
{
    int i;
    Stock s1[100];
    Stock *p;
    for(i=0,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<<"%";
    cout<<"\n"<<p->Get_N_count();
    return 0;
}

第三步:使用this指针

#include<iostream>
using namespace std;
const int N=2;
class Stock
{
private:
    static int N_count;
    long Number;
    float Max,Min,Begin,End;
public:
    Stock(){};
    Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock(int n,float ma,float mi,float b,float e);
    void Set_Stock();
    void Assign_Stock(Stock& p);
    void Show_Stock();
};
int Stock::N_count=0;
Stock::Stock(int n,float ma,float mi,float b,float e)
{
    N_count++;
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
void Stock::Set_Stock(int n,float ma,float mi,float b,float e)
{
    N_count++;
    Number=n;
    Max=ma;
    Min=mi;
    Begin=b;
    End=e;
}
void Stock::Assign_Stock(Stock& p)
{
    if(this!=&p)
    {
        N_count++;
        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<<End<<"\t";
}
void Stock::Set_Stock()
{
    N_count++;
    cout<<"Number:";
    cin>>Number;
    cout<<"Max:";
    cin>>Max;
    cout<<"Min:";
    cin>>Min;
    cout<<"Begin:";
    cin>>Begin;
    cout<<"End:";
    cin>>End;
}
int main()
{
    Stock s1(1,7.88,7.48,7.56,7.68);
    Stock s2;
    s2.Assign_Stock(s1);
    s2.Show_Stock();
    return 0;
}

第四步:增加友元函数
在类中加定义

int friend Get_Stock(Stock *s1);

在类外加函数定义

int Get_Stock(Stock *s1)
{
        cout<<endl<<s1->Begin<<"\t"<<s1->End;
    if ((s1->Begin)>(s1->End)) return 1;
        else return 0 ;
}

主程序变成:

int  main()
{
        int i;
    Stock s1[100];
    Stock *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<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;                      //友元函数调用
    return 0;
}

日期:2020年3月10日

发布了70 篇原创文章 · 获赞 79 · 访问量 3986

猜你喜欢

转载自blog.csdn.net/qq_45856289/article/details/104749427
今日推荐