IDE Qt Creator 4.0.3

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:
Stock(); //默认构造函数
Stock(const std::string &co,long n=0,double pr=0.0); //构造函数
~Stock(); //析构函数
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;
//默认构造函数
Stock::Stock()
{
cout<<"Default constructor called\n";
company="no name";
shares=0;
share_val=0.0;
total_val=0.0;
}
//构造函数
Stock::Stock(const string &co, long n, double pr)
{
cout<<"Construcor using"<<co<<" called\n";
company=co;
if(n<0)
{
cout<<"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()
{
cout<<"bye,"<<company<<endl;
}
//对某个公司股票的首次购买
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<<"Using constructos to create new objects\n";
Stock stock1("NanoSmart",12,20.0);
stock1.show();
Stock stock2=Stock("Boffo Objects",2,2.0);
stock2.show(); cout<<"Assigning stock1 to stock2:\n";
stock2=stock1;
stock1.show();
stock2.show(); cout<<"Using a constructor to reset an object\n";
stock1=Stock("Nifty Foods",10,50.0); //临时对象
stock1.show();
cout<<"Done\n";
}
return 0;
}

运行结果如下

P359 usestock2.cpp的更多相关文章

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

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

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

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

  3. cpp 调用python

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

  4. nginx+fastcgi+c/cpp

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

  5. APM程序分析-ArduCopter.cpp

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

  6. APM程序分析-AC_WPNav.cpp

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

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

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

  8. 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument

    这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...

  9. Identify Memory Leaks in Visual CPP Applications —— VLD内存泄漏检测工具

    原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于 ...

随机推荐

  1. VS 域名绑定IIs 调试

    一排致敬老师傅:https://www.cnblogs.com/woxpp/p/5805624.html 问题: 域名www.jinhuang.com,网站的ip地域跳转出问题,需要测试. ①修改Ho ...

  2. Java 判断字符串是否为空的四种方法、优缺点与注意事项

    以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: ...

  3. [c/c++] programming之路(11)、顺序分支

    一.模块化设计 #include<stdio.h> #include<stdlib.h> #include<windows.h> void openbaidu(){ ...

  4. Codeforces 700B Connecting Universities - 贪心

    Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...

  5. 【Python043-魔法方法:算术方法2】

    一. 反运算符 当对应的操作数不支持调用时,反运算数被调用(参考资料Lhttps://fishc.com.cn/thread-48793-1-1.html ) 1.对象(a+b)相加,如果对象a有__ ...

  6. TFS 报错解决方案:tf400324

    同事的解决方案没报这个问题将他的C:\Windows\System32\drivers\etc\hosts文件覆盖自己的文件,主要备份自己的文件不行了替换掉

  7. 播放器smplayer的各种键盘快捷键

    smplayer的很多键盘快捷键都是 "单字母"命令. 如: f, m命令等 有主工具栏, 是通过F5来进行切换的, 但是单击f5后"可能"会使窗口失去焦点, ...

  8. What are the differences between Flyweight and Object Pool patterns?

    What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...

  9. Derek解读Bytom源码-启动与停止

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  10. Introducing GitFlow

    Introducing GitFlow What Is GitFlow? GitFlow is a branching model for Git, created by Vincent Driess ...