#include “stdafx.h”
#include
#include “stock10.h”//usestock.cpp
int main()
{ {using std::cout;
cout << “using constructors to create new objects\n”;
Stock stock1(“nanosmart”, 12, 20.0);
stock1.show();
Stock stock2 = Stock(“boffo object”, 2, 2.0);
stock2.show();
cout << “assigning stock1 and stock2:\n”;
stock2 = stock1;
cout << “listing stock1 and stock2:\n”;
stock1.show();
stock2.show();
cout << “using a constructor to reset an object\n”;
stock1 = Stock(“nifty foods”,10,50.0);
cout << “revised stock1:\n”;
stock1.show();
cout << “done”;
stock2.show();
}
system(“pause”);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef STOCK10_H//stock10.h
#define STOCK10_H
// !STOCK10_H
#include
class Stock
{
private:std::string company;
long shares;
double total_val;
double share_val;
void set_tot() { total_val = shares*share_val; }
public:Stock();
Stock(const std::string &co, long n = 0, double pr = 0.0);
~Stock();
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include “stdafx.h”
#include//stock10.cpp
#include “stock10.h”
Stock::Stock()
{
std::cout << “default constructor called\n”;
company = "no name ";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string&co, long n, double pr)
{
std::cout << "Constructor using " << co << “called\n”;
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();
}
Stock::~Stock()
{
std::cout << “bye,” << company << “!\n”;
}
void Stock ::buy(long num,double price)
{
if (num < 0) { std::cout << “number of shares purchased can’t be negative.”<<“transaction is aborted”;
}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 sold can’t be negtive.” << “transactionis 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()
{
using std::cout;
using std::ios_base;
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 .setf(orig, ios_base::floatfield);
cout.precision(prec);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
和昨天一样,我发现,同一个类的对象赋值,其实就是传递了值而已,和地址没有半毛钱关系,看最后的stock2.show()就可知了,其实so easy ,但是打的时候不小心赋值时候=,Stock都漏掉过,怪不得会报错说double 类型不能赋值给stock类型,亲爱的你,可不要犯错哦,其实犯了也无所谓,反正还能改,我们可是编程的粉刷匠