P10.3 usestock0.cpp
stock.h
#ifndef STOCK_H
#define STOCK_H
#include <string>
class Stock //类声明
{
private:
std::string company;
long shares; //
double share_val;
double total_val;
void set_tot(){total_val=share_val*shares;}
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 // STOCK_H
main.cpp
#include <iostream>
#include "stock.h"
using namespace std;
//对某个公司股票的首次购买
void Stock::acquire(const string &co, long n, double pr)
{
company=co;
if(n<0)
{
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)
{
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)
{
if(num<0)
{
cout<<"Number of shares sold cna'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()
{
ios_base::fmtflags orig=
cout.setf(ios_base::fixed,ios_base::floatfield);
std::streamsize prec=cout.precision(3);
cout<<"Company:"<<company
<<" Shares:"<<shares<<'\n';
cout<<" Shares Price:$"<<share_val;
cout.precision(3);
cout<<" Total Worth:$"<<total_val<<'\n';
//show()应重置格式信息,使其恢复到自己被调用前的状态
cout.setf(orig,ios_base::floatfield);
cout.precision(prec);
}
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
Stock fluffy_the_cat;
fluffy_the_cat.acquire("NanoSmart",20,12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15,18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(400,20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000,40.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(300000,0.125);
fluffy_the_cat.show();
return 0;
}
运行结果如下

P10.3 usestock0.cpp的更多相关文章
- C++基础——类封装简单示例
一.前言 在IC前端设计/验证领域,只会HDL远远不够.目前大多数项目使用已开发好的系统架构和IP Core,因此设计部分的工作量慢慢向系统集成和验证方向转移.而在集成和验证过程中,往往以各种脚本和面 ...
- C++报错集锦
(一)invalid initialization of non-const reference of type 'float&' from a temporary of type 'floa ...
- 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...
- Json CPP 中文支持与入门示例
在每一个Json Cpp自带*.cpp文件头加上: #include "stdafx.h" 将Json Cpp对自带的头文件的引用修改为单引号方式,例如json_reader.cp ...
- cpp 调用python
在用cpp调用python时, 出现致命错误: no module named site , 原因解释器在搜索路径下没有找到python库.可以在调用Py_Initialize前,调用 Py_Se ...
- nginx+fastcgi+c/cpp
参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/ 跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多 ...
- APM程序分析-ArduCopter.cpp
该文件是APM的主文件. #define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(Copter, &copter ...
- APM程序分析-AC_WPNav.cpp
APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...
- Dev Cpp 输出中文字符问题
最近 c++ 上机作业,vc++6.0 挂了没法用,只好用 Dev Cpp 先顶替一下,然而在遇到输出中文字符的时候出现了乱码的情况,但这种情况又非常诡异.于是简单了解了一下写成此博客. [写在前面] ...
随机推荐
- XX-net
环境:win10企业版 #停用“ip helper”服务 net stop "ip helper" #启用“ip helper”服务 net start "ip help ...
- 标准库 svc—程序及服务控制
对于程序及服务的控制,本质上而言就是正确的启动,并可控的停止或退出.在go语言中,其实就是程序安全退出.服务控制两个方面.核心在于系统信号获取.Go Concurrency Patterns.以及基本 ...
- java中线程的停止以及LockSupport工具类
看jstack输出的时候,可以发现很多状态都是TIMED_WAITING(parking),如下所示: "http-bio-8080-exec-16" #70 daemon pri ...
- oracle RAC如何正确地删除ASM磁盘组
1.登录到命令行 切换到grid用户 [grid@swnode1 ~]$ sqlplus / as sysasm SQL*Plus: Release Production on Wed May :: ...
- sentos7为例添加python3和python2共存
转载:https://www.cnblogs.com/JahanGu/p/7452527.html 1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命 ...
- Centos 7 安装 Supervisor 及使用
Supervisor官网链接:http://supervisord.org/installing.html 安装与设置开机启动: http://blog.csdn.net/fenglailea/art ...
- (2编写网络)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署
基于<神经网络和深度学习>这本绝好的教材提供的相关资料和代码,我们自己动手编写"随机取样的梯度下降神经网络".为了更好地说明问题,我们先从简单的开始: 1.sigmod ...
- batchGetAnchorLevel(dubbo接口)
一.编写脚本前的准备工作 1.安装idea,安装本地maven库,并在idea里面配置maven 2.导入git源码(目的在于下载所依赖的基础包)-->File-new-Project from ...
- 关于python hashlib模块的使用
hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, sha512等算法 #!/usr/bin/ ...
- linux内核中的fuse是什么?
答: 一个用户态文件系统框架,属于内核的一种特性. 1.组成部分 fuse.ko(内核模块) + libfuse.*(用户空间库) + fusemount(挂载工具) 2.参考资料 fuse.txt