C++001类

C++中通常将类的定义、类的成员函数实现和类的使用三个类容放在三个单独的文件中。

// 类定义
// stock00.h

#ifndef STOCH00_H_
#define STOCH00_H_

#include<string>

class Stock {
    private:
        std::string company;
        long shares;
        double share_val;
        double total_val;

        void set_tot() { total_val = shares * share_val; } // 内联函数
    public:
        void acquire(const std::string & co, long n, double pr);
        void buy(long num, double price);
        void sell(long num, double price);
        void update(double price);
        void show();
};

#endif
// 成员函数实现
// stock00.cpp

#include<iostream>

#include "stock00.h"


void Stock::acquire(const std::string & co, long n, double pr) {
    company = co;
    if (n < 0) {
        std::cout << "Number of shares can't be negative;"
            << company << "shares set to 0.\n";
    }
    else {
        shares = n;
    }
    share_val = pr;
    set_tot();
}
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();
}
void Stock::show() {
    std::cout << " Company: " << company<<'\n'
        << " Shares: $" << shares << '\n'
        << " Share Price: $" << share_val<<'\n'
        << " Total Worth: $" << total_val << '\n';
}
// 类的使用
// usestock0.cpp

#include<iostream>

#include "stock00.h"

int main() {
    Stock fluffy_the_cat;
    fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
    fluffy_the_cat.show();

    Stock fluffy_the_yim;
    fluffy_the_yim.acquire("NanoSmartSmart", 30, 10.00);
    fluffy_the_yim.show();
    fluffy_the_yim.buy(15, 18.125);
    fluffy_the_yim.show();
    fluffy_the_yim.sell(400, 20.00);
    fluffy_the_yim.show();

    return 0;
}

改进上面的程序

// 类定义
// stock00.h

#ifndef STOCK00_H_
#define STOCK00_H_

#include<string>

class Stock {
    private:
        std::string company;
        long shares;
        double share_val;
        double total_val;
        void set_tot() { total_val = shares * share_val; }
    public:
        Stock();// 默认构造函数
        Stock(const std::string & co, long n = 0, double pr = 0.0);// 构造函数
        ~Stock(); // 析构函数

        void buy(long num, double price);
        void sell(long num, double price);
        void update(double price);
        void show();
};



#endif

 

// 成员函数实现
// stock00.cpp

#include<iostream>

#include "stock00.h"


Stock::Stock() {
    // 默认构造函数
    std::cout << "Default constructor called\n";
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;

}
Stock::Stock(const std::string & co, long n, double pr) {
    // 构造函数
    std::cout << "Constructor using "<< co << " called\n";
    company = co;

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

}
Stock::~Stock() {
    // 析构函数
    std::cout << " Bye,  " << company << "!\n";

}

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. "
            << "Transantion is aborted.\n";
    }
    else if (num > shares) {
        cout << "You can't sell more than you have!"
            << "Transantion is aborted.\n";
    }
    else {
        shares -= num;
        share_val = price;
        set_tot();
    }


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


}
void Stock::show() {
    using std::cout;
    using std::ios_base;
    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);

    cout << "Company: " << company<<"\n"
        << "Shares: " << shares << '\n';
    cout << "Shares Price: $" << share_val<<"\n";

    cout.precision(2);
    cout << "Total Worth: $" << total_val << "\n";

    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);



}

 

// 类的使用
// usestock0.cpp

#include<iostream>

#include "stock00.h"

int main() {
    {
        using std::cout;
        cout << "Using constructors to create new objects\n";
        Stock stock1("NanoSmart", 12, 20.0);
        cout << "    Revised stock1:\n";
        stock1.show();
        cout << "    Done\n";
    }

    return 0;
}

 

posted @ 2020-11-21 17:05  谢义xieyi521149  阅读(75)  评论(0编辑  收藏  举报