使用类和多文件编译
来源:互联网 发布:阿里云cdn审核 编辑:程序博客网 时间:2024/05/16 15:33
#define DUO_H_INCLUDED
#include <string>
#include <iostream>
class Stock
{
private:
string company;
long shares;
double share_val;
double total_val;
void set_tot()
{
total_val=shares*share_val;
}
public:
void acquire(const 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 // DUO_H_INCLUDED
#include <iostream>
#include <windows.h>
#include "duo.h"
using namespace std;
void Stock::acquire(const::string & co,long n,double pr)
{
company = co;
if(n<0)
{
std::cout<<"Number of shares can't be negative!"
<<company<<" shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(long num,double price)
{
if (num<0)
{
std::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)
{
using std::cout;
if (num<0)
{
cout<<"Number of shares purchased can'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()
{
std::cout<<"Company: "<<company
<<" Shares: "<<shares <<'\n'
<<" Share Price: $"<<share_val
<<" Total Worth: $"<<total_val<<'\n';
}
int main()
{
Stock a;
a.acquire("NanoSmart",20,12.50);
a.show();
a.buy(15,18.125);
a.show();
a.sell(400,20.00);
a.show();
a.buy(300000,40.125);
a.show();
a.sell(300000,0.125);
a.show();
system("PAUSE");
return 0;
}
- 使用类和多文件编译
- gcc和g++区别,使用g++编译多个文件
- 使用as和ld编译.s文件
- linux c 编译和使用库文件
- jni文件的编译和使用
- linux下编译和使用java文件
- libjson 编译和使用 - 2. 配置使用lib文件
- 《APUE》和《UNP》文件的编译和使用(转载)
- 使用javac编译java文件和使用javah生成C/C++头文件 遇到找不到类的问题
- gcc下.a库文件的编译生成和使用
- 如何使用cmd来编译和运行java文件
- 使用gulp-sass 和 gulp-livereload 自动编译sass文件
- 【Linux/C】如何编译和使用库文件
- android ndk第一步使用ndk和Cmake编译.so文件
- 多文件结构和编译预处理命令
- g++多文件编译和简单MakeFile文件写法
- g++多文件编译和简单MakeFile文件写法
- g++多文件编译和简单MakeFile文件写法
- 使用maven构建工具的好处
- mysql初始化出现:Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared
- Android数据库框架GreenDao使用详解
- 友盟崩溃日志分析
- JS nodeType返回类型
- 使用类和多文件编译
- C# .ToString() 格式化
- Linux命令大全
- JAVA 8函数式编程(四):JAVA支持闭包吗?
- 抽象类(abstract class | interface)
- HDU-2017 多校训练赛10-1002-Array Challenge
- CCF 俄罗斯方块(Java)
- 欢迎使用CSDN-markdown编辑器
- MSComm控件的使用方法