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的更多相关文章

  1. C++基础——类封装简单示例

    一.前言 在IC前端设计/验证领域,只会HDL远远不够.目前大多数项目使用已开发好的系统架构和IP Core,因此设计部分的工作量慢慢向系统集成和验证方向转移.而在集成和验证过程中,往往以各种脚本和面 ...

  2. C++报错集锦

    (一)invalid initialization of non-const reference of type 'float&' from a temporary of type 'floa ...

  3. 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...

  4. Json CPP 中文支持与入门示例

    在每一个Json Cpp自带*.cpp文件头加上: #include "stdafx.h" 将Json Cpp对自带的头文件的引用修改为单引号方式,例如json_reader.cp ...

  5. cpp 调用python

    在用cpp调用python时, 出现致命错误: no module named site  ,  原因解释器在搜索路径下没有找到python库.可以在调用Py_Initialize前,调用 Py_Se ...

  6. nginx+fastcgi+c/cpp

    参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/ 跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多 ...

  7. APM程序分析-ArduCopter.cpp

    该文件是APM的主文件. #define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(Copter, &copter ...

  8. APM程序分析-AC_WPNav.cpp

    APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...

  9. Dev Cpp 输出中文字符问题

    最近 c++ 上机作业,vc++6.0 挂了没法用,只好用 Dev Cpp 先顶替一下,然而在遇到输出中文字符的时候出现了乱码的情况,但这种情况又非常诡异.于是简单了解了一下写成此博客. [写在前面] ...

随机推荐

  1. jQuery输入框回车添加标签特效

    效果如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  2. 新手安装Ubuntu操作系统

    新手安装 Ubuntu 操作系统 版权声明:未经博主授权,内容严禁转载分享! 最近学习linux编程,需要安装一个 Ubuntu 操作系统,由于虚拟机的体验不是很好,所以便在电脑上试下装双系统.嘿嘿. ...

  3. python简说(十二)time模块

    1.时间戳 print(int(time.time())) 2.取当前格式化好的时间 time.strftime('%Y-%m-%d %H:%M:%S') 3.时间戳转为格式化好的时间 time1 = ...

  4. C++max的使用方法

    #include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> ...

  5. topcoder srm 710 div1 -23

    1.给定两个长度都为$n$的数组$A,B$,给出一个操作序列将$A$变成$B$.每个操作可以是以下两种之一:(1)选择一个$i,0\leq i <n$且$A_{i} \neq 0$,令$t=A_ ...

  6. Eclipse的Servers视图中无法添加Tomcat

    问题:Eclipse的Servers视图中无法添加Tomcat,其中ServerName是被置为灰色的,无法编辑,如下图所示: 解决步骤: 关闭Eclipse 找到Eclipse的工作区间,这里假设命 ...

  7. TCPCopy 使用方法

    TCPCopy 使用方法 TCPCopy是一种请求复制(所有基于tcp的packets)工具,可以把在线请求导入到测试系统中去.目前此工具已经广泛应用于国内各大互联网公司. TCPCopy七大功能 1 ...

  8. linux内核中的hisi_sas是什么?

    答: 是一个HISILICON SAS 控制器驱动(HISILICON SAS controller driver)

  9. ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

    题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...

  10. 【ContextLoaderListener】Web项目启动报错java.lang.ClassNotFoundException: ContextLoaderListener

    错误原因: 进入到tomcat的部署路径.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\下检查了一下,发现工程部署后在WE ...