Stock类

头文件Stock.h

#ifndef STOCK_H
#define STOCK_H

#include <string>

class Stock{
public:
	Stock();
	Stock(std::string symbol, std::string name);

	std::string getSymbol() const;
	std::string getName() const;
	double getPreviousClosingPrice() const;
	double getCurrentPrice() const;

	void setPreviousClosingPrice(double newPreviousPrice);
	void setCurrentPrice(double newCurrentPrice);

	double getChangePercent() const;
	
private:
	std::string symbol;	//stock symbol
	std::string name;	//stock name
	double previousClosingPrice;	//stock previous price
	double currentPrice;	//stock current price
};

#endif // Stock.h

 

实现文件Stock.cpp

#include "Stock.h"
#include <string>

Stock::Stock() : symbol(""), name(""),
				previousClosingPrice(0),
				currentPrice(0){
	//nothing
}

Stock::Stock(std::string symbol, std::string name){
	this->symbol = symbol;
	this->name = name;
}

std::string Stock::getSymbol() const{
	return symbol;
}

std::string Stock::getName() const{
	return name;
}

double Stock::getPreviousClosingPrice() const{
	return previousClosingPrice;
}

double Stock::getCurrentPrice() const{
	return currentPrice;
}

void Stock::setPreviousClosingPrice(double newPreviousPrice){
	previousClosingPrice = newPreviousPrice;
}

void Stock::setCurrentPrice(double newCurrentPrice){
	currentPrice = newCurrentPrice;
}

double Stock::getChangePercent() const{
	return (currentPrice - previousClosingPrice) / currentPrice;
}

 

测试文件main.cpp

#include <iostream>
#include <iomanip>
#include "Stock.h"
using namespace std;

int main(){
	Stock stock("MSFT", "Microsoft Corporation");

	stock.setPreviousClosingPrice(27.5);
	stock.setCurrentPrice(27.6);

	cout << setw(30) << left << "symbol: " << stock.getSymbol() << endl;
	cout << setw(30) << left << "name: " << stock.getName() << endl;
	cout << setw(30) << left << "previous closing price: " << stock.getPreviousClosingPrice() << endl;
	cout << setw(30) << left << "current price: " << stock.getCurrentPrice() << endl;

	cout << setprecision(2);	//保留两位小数
	cout << setw(30) << left << "change percent: " << stock.getChangePercent()  * 100 << "%" << endl;
	return 0;
}

 运行结果:

转载于:https://www.cnblogs.com/mocuishle/p/7995066.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值