C++实验:类与对象

2021/10/18 22:12:14

本文主要是介绍C++实验:类与对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、实验内容

6) 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。

    7) 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。

    8) 使用股票类Stock定义一个对象数组存放连续5个交易日的股票信息。编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价,计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。

    9) 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。

    10) 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用Assign_stock赋值的对象。

11) 定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。若是,返回真;否则,返回假。

2、代码

6) 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

};

#include "Stock.h"

Stock::Stock()

{

         Number = 0;

         Max = 0;

         Min = 0;

         Begin = 0;

         End = 0;

}

double Stock::Get_End()

{

         return 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::Show_Stock()

{

         cout << Number << endl;

         cout << Max << endl;

         cout << Min << endl;

         cout << Begin << endl;

         cout << End << endl;

}

7) 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

};

#include "Stock.h"

Stock::Stock()

{

         Number = 0;

         Max = 0;

         Min = 0;

         Begin = 0;

         End = 0;

}

double Stock::Get_End()

{

         return 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::Show_Stock()

{

         cout << Number << endl;

         cout << Max << endl;

         cout << Min << endl;

         cout << Begin << endl;

         cout << End << endl;

}

#include <iostream>

#include "Stock.h"

using namespace std;

int main()

{

       double rate;

       Stock s1(1, 3.2, 2.3, 2.5, 3.1);

       Stock s2(2, 4.2, 3.6, 3.9, 3.8);

       rate = (s2.Get_End() - s1.Get_End()) / s1.Get_End() * 100;

       cout << "以当日收盘价计算该股票今天的涨幅为:" << rate << "%" << endl;

}

8) 使用股票类Stock定义一个对象数组存放连续5个交易日的股票信息。编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价,计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

       void Set_Stock();

};

#include "Stock.h"

Stock::Stock()

{

       Number = 0;

       Max = 0;

       Min = 0;

       Begin = 0;

       End = 0;

}

double Stock::Get_End()

{

       return 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::Show_Stock()

{

       cout << Number << endl;

       cout << Max << endl;

       cout << Min << endl;

       cout << Begin << endl;

       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:";

       cin >> End;

}

#include <iostream>

#include "Stock.h"

using namespace std;

int main()

{

       int i;

       Stock s[5],*p;

       for (i = 0,p=s; i < 5; i++,p++)

       {

              p->  Set_Stock();

       }

       for (i = 0,p = s; i < 5; i++)

       {

              s->Show_Stock();

       }

       for (i = 1,p=s+1; i < 5; i++,p++)

       {

             

              cout << "涨幅为:" << ((p->Get_End() - (p-1)->Get_End()) / (p-1)->Get_End() * 100) << "%"<<endl;

       }

}

9) 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       static int count;

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

       void Set_Stock();

       int Count();

};

#include "Stock.h"

int Stock::count = 0;

Stock::Stock()

{

       count++;

       Number = 0;

       Max = 0;

       Min = 0;

       Begin = 0;

       End = 0;

}

double Stock::Get_End()

{

       count++;

       return End;

}

Stock::Stock(int n, double ma, double mi, double b, double e)

{

       count++;

       Number = n;

       Max = ma;

       Min = mi;

       Begin = b;

       End = e;

}

void Stock::Show_Stock()

{

       count++;

       cout << Number << endl;

       cout << Max << endl;

       cout << Min << endl;

       cout << Begin << endl;

       cout << End << endl;

}

void Stock::Set_Stock()

{

       count++;

       cout << "Number:";

       cin >> Number;

       cout << "Max:";

       cin >> Max;

       cout << "Min:";

       cin >> Min;

       cout << "Begin:";

       cin >> Begin;

       cout << "End:";

       cin >> End;

}

int Stock::Count()

{

       count++;

       return count;

}

#include <iostream>

#include "Stock.h"

using namespace std;

int main()

{

       int i;

       Stock s[5],*p;

       for (i = 0,p=s; i < 5; i++,p++)

       {

              p->  Set_Stock();

       }

       for (i = 0,p = s; i < 5; i++)

       {

              s->Show_Stock();

       }

       for (i = 1,p=s+1; i < 5; i++,p++)

       {

              cout << "涨幅为:" << ((p->Get_End() - (p-1)->Get_End()) / (p-1)->Get_End() * 100) << "%"<<endl;

              cout << "当前Stock的对象的个数为:" << p->Count() << endl;

       }

}

10) 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用Assign_stock赋值的对象。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       static int count;

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

       void Set_Stock();

       int Count();

       void Assign_stock(Stock& p);

};

#include "Stock.h"

int Stock::count = 0;

Stock::Stock()

{

       count++;

       Number = 0;

       Max = 0;

       Min = 0;

       Begin = 0;

       End = 0;

}

double Stock::Get_End()

{

       count++;

       return End;

}

Stock::Stock(int n, double ma, double mi, double b, double e)

{

       count++;

       Number = n;

       Max = ma;

       Min = mi;

       Begin = b;

       End = e;

}

void Stock::Show_Stock()

{

       count++;

       cout << Number << endl;

       cout << Max << endl;

       cout << Min << endl;

       cout << Begin << endl;

       cout << End << endl;

}

void Stock::Set_Stock()

{

       count++;

       cout << "Number:";

       cin >> Number;

       cout << "Max:";

       cin >> Max;

       cout << "Min:";

       cin >> Min;

       cout << "Begin:";

       cin >> Begin;

       cout << "End:";

       cin >> End;

}

int Stock::Count()

{

       count++;

       return count;

}

void Stock::Assign_stock(Stock& p)

{

       if (this != &p)

       {

              Number = p.Number;

              Max = p.Max;

              Min = p.Min;

              Begin = p.Begin;

              End = p.End;

       }

}

#include <iostream>

#include "Stock.h"

using namespace std;

int main()

{

       Stock s1(3, 7.4, 6.5, 6.65, 7.23);

       Stock s2;

       s2.Assign_stock(s1);

       s2.Show_Stock();

}

11) 定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。若是,返回真;否则,返回假。

#pragma once

#include<iostream>

using namespace std;

class Stock

{

private:

       static int count;

       int Number;

       double Max;

       double Min;

       double Begin;

       double End;

public:

       Stock();

       Stock(int n, double ma, double mi, double b, double e);

       double Get_End();

       void Show_Stock();

       void Set_Stock();

       int Count();

       void Assign_stock(Stock& p);

       friend int GetStock(Stock& s1);

};

#include "Stock.h"

int Stock::count = 0;

Stock::Stock()

{

       count++;

       Number = 0;

       Max = 0;

       Min = 0;

       Begin = 0;

       End = 0;

}

double Stock::Get_End()

{

       count++;

       return End;

}

Stock::Stock(int n, double ma, double mi, double b, double e)

{

       count++;

       Number = n;

       Max = ma;

       Min = mi;

       Begin = b;

       End = e;

}

void Stock::Show_Stock()

{

       count++;

       cout << Number << endl;

       cout << Max << endl;

       cout << Min << endl;

       cout << Begin << endl;

       cout << End << endl;

}

void Stock::Set_Stock()

{

       count++;

       cout << "Number:";

       cin >> Number;

       cout << "Max:";

       cin >> Max;

       cout << "Min:";

       cin >> Min;

       cout << "Begin:";

       cin >> Begin;

       cout << "End:";

       cin >> End;

}

int Stock::Count()

{

       count++;

       return count;

}

void Stock::Assign_stock(Stock& p)

{

       if (this != &p)

       {

              Number = p.Number;

              Max = p.Max;

              Min = p.Min;

              Begin = p.Begin;

              End = p.End;

       }

}

int GetStock(Stock&s1)

{

       cout << s1.Begin << "\t" << s1.End;

       if ((s1.Begin) > (s1.End))

              return 1;

       else return 0;

}

#include <iostream>

#include "Stock.h"

using namespace std;

int main()

{

       int i;

       Stock s[5], * p;

       for (i = 0, p = s; i < 5; i++, p++)

       {

              p->Set_Stock();

       }

       for (i = 0, p = s; i < 5; i++)

       {

              s->Show_Stock();

       }

       for (i = 1, p = s + 1; i < 5; i++, p++)

       {

              cout << "涨幅为:" << ((p->Get_End() - (p - 1)->Get_End()) / (p - 1)->Get_End() * 100) << "%" << endl;

              cout << "当前Stock的对象的个数为:" << p->Count() << endl;

       }

       for (i = 0, p = s; i < 5; i++, p++)

       {

              cout << GetStock(*p) << endl;

       }

}



这篇关于C++实验:类与对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程