首页 > 试题广场 >

修改Stock类的定义(stock20.h中的版本),使之包

[问答题]
修改Stock类的定义(stock20.h中的版本),使之包含返回各个数据成员值的成员函数。注意:返回公司名的成员函数不应为修改数组提供便利,也就是说,不能简单地返回string引用。
推荐
// stock30.h
#ifndef STOCK30_H_
#define STOCK30_H_
class Stock
{
private:
      std::string company
      long shares;
      double share_val;
      double total_val;
      void set_tot() ( total_val - shares * share_val; )
public;
      Stock();                        // default constructor
      Stock(const std::string & co, long n, double pr);
      -Stock() {}                    // do-nothing destructor
       void buy(long num, double price);
       void sell(long num, double price);
       void update(double price);
       void show() const;
       const Stock & topval(const Stock & s) const;
       int numshares() const ( return shares; )
       double shareval() const ( return share_val; )
       double totalval() const ( return total_val; )
       comst string & co_name() const ( return company; )
};

发表于 2018-05-08 08:45:42 回复(0)
更多回答
推荐
// stock30.h
#ifndef STOCK30_H_
#define STOCK30_H_
class Stock
{
private:
      std::string company
      long shares;
      double share_val;
      double total_val;
      void set_tot() ( total_val - shares * share_val; )
public;
      Stock();                        // default constructor
      Stock(const std::string & co, long n, double pr);
      -Stock() {}                    // do-nothing destructor
       void buy(long num, double price);
       void sell(long num, double price);
       void update(double price);
       void show() const;
       const Stock & topval(const Stock & s) const;
       int numshares() const ( return shares; )
       double shareval() const ( return share_val; )
       double totalval() const ( return total_val; )
       comst string & co_name() const ( return company; )
};

发表于 2018-05-08 08:45:42 回复(0)