Network + course work: 5 hours
cf: 2 Xiaoshi
Knowledge Learning Tree Hash: 1 hour
English words: 1 hour
Recalling c ++ Chapter 10: 1 hour
#include "the iostream" #include "CString" class Stock { Private: char Company [30]; int shares; Double share_val; Double Total_val; void set_tot () {/// The class declaration is often short member functions as inline * = shares share_val Total_val; } public: /// function defined in the class declaration are automatically inline function Stock (); Stock (CO.'s const char *, int n-, Double PR); /// constructor ~ Stock (); /// destructor void Buy (int NUM, Double. price); void Sell (int NUM, Double. price); void Update (Double. price); void Show (); const & TopVal Stock (const & Stock S) const; }; Stock :: Stock(const char * co,int n,double pr) { // std::cout << "Constructor using " ; std::strncpy(company,co,29); company[29] = '\0'; if(n < 0) { std :: cerr << "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 <<" Default constructor called\n"; std :: strcpy(company,"no name"); shares = 0; share_val = 0.0; total_val = 0.0; } Stock :: ~Stock() { std :: cout << "Bye, " << company << "!\n"; } void Stock :: buy(int num,double price) { if(num < 0) { std:: cerr<<"Number of shares purchased can't be negative. " << "Transaction is aborted.\n"; } else { shares += num; share_val = price; set_tot(); } } void Stock::sell(int num,double price) { using std::cerr; if(num < 0) { cerr << "Number of shares sold can't be negative." << "Transaction is aborted.\n"; } else if(num > shares) { cerr << "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() { using std :: cout; using std :: endl; cout << "Company: " << company << " Shares: " << shares << endl <<" Share Price: $" << share_val << " Total Worth: $" << total_val << endl; } const Stock & Stock :: topval(const Stock & s)const { if(s.total_val > total_val) return s; else return *this; } int main() { using std :: cout; using std :: ios_base; cout.precision(2); cout.setf(ios_base :: fixed,ios_base :: floatfield); cout.setf(ios_base::showpoint); cout << "Using constructors to create new objects\n"; Stock stock1("NanoSmark",12,20.0); stock1.show(); Stock stock2 = Stock("Boffo Objects",2,2.0); stock2.show(); cout << "Assigning stock1 to stock2: \n"; stock2 = stock1; cout << "Listing stock1 ans stock2: \n"; stock1.show (); stock2.show(); cout << "Using a constructor to reset an object\n"; stock1 = Stock ( "Nofty Foods", 10,50.0); /// attention here, because there is a value of the stock1 /// so here stock1 not call the constructor again, but there is a assigned to anonymous objects /// stock1, after the assignment is completed, the implementation of the anonymous objects destructor COUT << "Revised stock1: \ n-"; stock1.show (); COUT << "the Done \ n-"; const = Stock Land Stock ( "Kludgehorn the Properties"); //land.show();/// Note this statement to be wrong, because there is no guarantee, show function, no land to change /// unless defined time show () function definition is: void show () const; function does not modify the call object to ensure return 0; }