565 pdfsam Cplusplus Primer Plus 4th Edition

This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks List...

0 downloads 9 Views 2KB Size
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks

Listing 10.3 The Full stocks.cpp Program

// stocks.cpp -- the whole program #include #include // --or string.h--for strncpy() using namespace std; class Stock // class declaration { private: char company[30]; int shares; double share_val; double total_val; void set_tot() { total_val = shares * share_val; } public: void acquire(const char * co, int n, double pr); void buy(int num, double price); void sell(int num, double price); void update(double price); void show(); }; // note semicolon at the end void Stock::acquire(const char * co, int n, double pr) { strncpy(company, co, 29); // truncate co to fit if needed company[29] = '\0'; if (n < 0) { cerr << "Number of shares can't be negative; " << "shares set to 0.\n"; shares = 0; } else shares = n; share_val = pr;