欢迎来到天天文库
浏览记录
ID:41054148
大小:32.00 KB
页数:3页
时间:2019-08-15
《类和对象的实现和实例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、类和对象的实现和实例下面是关于类和对象的实际应用:#include#includeclassStock{private:charcompany[30];intshares;doubleshare_val;doubletotal_val;voidset_tot(){total_val=shares*share_val;}public:voidacquire(constchar*co,intn,doublepr);voidbuy(intnum,doublepri
2、ce);voidsell(intnum,doubleprice);voidupdate(doubleprice);voidshow();};voidStock::acquire(constchar*co,intn,doublepr){std::strncpy(company,co,29);company[29]=' ';if(n<0){std::cerr<<"Numberofsharescan'tbenegative."<3、;}else{shares=n;share_val=pr;set_tot();}}voidStock::buy(intnum,doubleprice){if(num<0){usingstd::cout;cout<<"Numberofsharespurchasedcan'tbenegative."<<"Transactionisaborted.";}else{shares+=num;share_val=price;set_tot();}}voidStock::sell(intnum,doubl4、eprice){if(num<0){usingstd::cout;cout<<"Numberofsharessoldcan'tbenegative."<<"Transactionisaborted.";}elseif(num>shares){usingstd::cout;cout<<"Youcan'tsellmorethanyouhave!"<<"Transactionisaborted.";}else{shares-=num;share_val=price;set_tot();}}vo5、idStock::update(doubleprice){share_val=price;set_tot();}voidStock::show(){usingstd::cout;usingstd::endl;cout<<"Company:"<6、:ios_base;Stockstock1;stock1.acquire("NanoSmart",20,12.50);cout.setf(ios_base::fixed);cout.precision(2);cout.setf(ios_base::showpoint);stock1.show();stock1.buy(15,18.25);stock1.show();stock1.sell(400,20.00);stock1.show();system("pause");return0;}这段C+7、+代码是实现股票的买卖过程。
3、;}else{shares=n;share_val=pr;set_tot();}}voidStock::buy(intnum,doubleprice){if(num<0){usingstd::cout;cout<<"Numberofsharespurchasedcan'tbenegative."<<"Transactionisaborted.";}else{shares+=num;share_val=price;set_tot();}}voidStock::sell(intnum,doubl
4、eprice){if(num<0){usingstd::cout;cout<<"Numberofsharessoldcan'tbenegative."<<"Transactionisaborted.";}elseif(num>shares){usingstd::cout;cout<<"Youcan'tsellmorethanyouhave!"<<"Transactionisaborted.";}else{shares-=num;share_val=price;set_tot();}}vo
5、idStock::update(doubleprice){share_val=price;set_tot();}voidStock::show(){usingstd::cout;usingstd::endl;cout<<"Company:"<6、:ios_base;Stockstock1;stock1.acquire("NanoSmart",20,12.50);cout.setf(ios_base::fixed);cout.precision(2);cout.setf(ios_base::showpoint);stock1.show();stock1.buy(15,18.25);stock1.show();stock1.sell(400,20.00);stock1.show();system("pause");return0;}这段C+7、+代码是实现股票的买卖过程。
6、:ios_base;Stockstock1;stock1.acquire("NanoSmart",20,12.50);cout.setf(ios_base::fixed);cout.precision(2);cout.setf(ios_base::showpoint);stock1.show();stock1.buy(15,18.25);stock1.show();stock1.sell(400,20.00);stock1.show();system("pause");return0;}这段C+
7、+代码是实现股票的买卖过程。
此文档下载收益归作者所有