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. "; } 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. "; } 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. "; } else if(num > shares){ cout << "You can't sell more than you have!" << "Transaction is aborted. "; } 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<<' ' << " Shares: $" << shares << ' ' << " Share Price: $" << share_val<<' ' << " Total Worth: $" << total_val << ' '; }
// 类的使用 // 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 "; 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 "; company = co; if (n < 0) { std::cout << "Number of shares can't be negative; " << company << " shares set to 0. "; shares = 0; } else { shares = n; } share_val = pr; set_tot(); } Stock::~Stock() { // 析构函数 std::cout << " Bye, " << company << "! "; } void Stock::buy(long num, double price) { if (num < 0) { std::cout << "Number of shares purchased can't be negative. " << "Transaction is aborted. "; } 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. "; } else if (num > shares) { cout << "You can't sell more than you have!" << "Transantion is aborted. "; } 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<<" " << "Shares: " << shares << ' '; cout << "Shares Price: $" << share_val<<" "; cout.precision(2); cout << "Total Worth: $" << total_val << " "; 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 "; Stock stock1("NanoSmart", 12, 20.0); cout << " Revised stock1: "; stock1.show(); cout << " Done "; } return 0; }