C++ Premier Plus 6th edition - Programming excercise - Chapter12 - 3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42787086/article/details/84503021

stock20.h

// stock20.h -- augmented version
#ifndef STOCK20_H_
#define STOCK20_H_
#include <string>
#include<iostream>

class Stock
{
private:
    char* str;
    int shares;
    double share_val;
    double total_val;
    void set_tot()
    {
        total_val = shares * share_val;
    }
public:
    Stock();        // default constructor

    // Need to reconstruct constructor
    // based on usestock2.cpp, neither need to overload operator=,
    // nor create copy constructor
    Stock(const char* c, long n = 0, double pr = 0.0);
    ~Stock();       // do-nothing destructor
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    const Stock & topval(const Stock & s) const;

    friend std::ostream& operator<<(std::ostream& os, const Stock& obj);
};

#endif

stock20.cpp

// stock20.cpp -- augmented version
#include <iostream>
#include "stock20.h"
using namespace std;
// constructors
Stock::Stock()        // default constructor
{
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}

Stock::Stock(const char* c, long n, double pr)
{
    // Newly added:
    int len = strlen(c) + 1;
    str = new char[len];
    strcpy_s(str, len, c);

    if (n < 0)
    {
        std::cout << "Number of shares can't be negative; "
                  << *str << " shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}

// class destructor
Stock::~Stock()        // quiet class destructor
{
}

// other methods
void Stock::buy(long num, double price)
{
    if (num < 0)
    {
        std::cout << "Number of shares purchased can't be negative. "
                  << "Transaction is aborted.\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(long num, double price)
{
    using std::cout;
    if (num < 0)
    {
        cout << "Number of shares sold can't be negative. "
             << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout << "You can't sell more than you have! "
             << "Transaction is aborted.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val = price;
    set_tot();
}

const Stock & Stock::topval(const Stock & s) const
{
    if (s.total_val > total_val)
        return s;
    else
        return *this;
}

// Newly added
std::ostream& operator<<(std::ostream& os, const Stock& obj)
{
    using std::ios_base;
    // set format to #.###
    ios_base::fmtflags orig =
        os.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = os.precision(3);

    os << "Company: " << obj.str
       << "  Shares: " << obj.shares << '\n';
    os << "  Share Price: $" << obj.share_val;
    // set format to #.##
    os.precision(2);
    os << "  Total Worth: $" << obj.total_val << '\n';

    // restore original format
    os.setf(orig, ios_base::floatfield);
    os.precision(prec);
    return os;
}

main

// usestok2.cpp -- using the Stock class
// compile with stock20.cpp
#include <iostream>
#include "stock20.h"

const int STKS = 4;
int main()
{
    {
// create an array of initialized objects
        Stock stocks[STKS] =
        {
            Stock("NanoSmart", 12, 20.0),
            Stock("Boffo Objects", 200, 2.0),
            Stock("Monolithic Obelisks", 130, 3.25),
            Stock("Fleep Enterprises", 60, 6.5)
        };

        std::cout << "Stock holdings:\n";
        int st;
        for (st = 0; st < STKS; st++)
            std::cout << stocks[st];
// set pointer to first element
        const Stock * top = &stocks[0];
        for (st = 1; st < STKS; st++)
            top = &top->topval(stocks[st]);
// now top points to the most valuable holding
        std::cout << "\nMost valuable holding:\n";
        std::cout << *top;
    }
    std::cin.get();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42787086/article/details/84503021