main.cpp
#include <iostream>
#include "stock00.h"
int main()
{
{
using std::cout;
cout << "Using constructors to create new objects\n";
Stock stock1("NanoSmart", 12, 20.0); // syntax 1
stock1.show();
Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // syntax 2
stock2.show();
cout << "Assigning stock1 to 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); // temp object
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
}
// std::cin.get();
return 0;
}
stock00.cpp
//
// Created by sunshine on 19-4-30.
//
#include <iostream>
#include "stock00.h"//将从源文件所在的目录中搜索它
Stock::Stock()
{
std::cout<<"Default constructor called\n";
company="no name";
shares=0;
share_val=0.0;
total_val=0.0;
}
//void Stock::acquire(const std::string & co, long n, double pr)
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.\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 sold 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';
}
stock00.h
//
// Created by sunshine on 19-4-30.
//
#ifndef STOCK00_H
#define STOCK00_H
#include <string>
class Stock // class declaration
{
private:
std::string company;//公司的名称
long shares;//股票的数量
double share_val;//每股的价格
double total_val;//股票总价格
void set_tot() { total_val = shares * share_val; }
public:
Stock();//默认构造函数
//void acquire(const std::string & co, long n, double pr);//管理某一个公司的首次购买
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();//显示
}; // note semicolon at the end
#endif //STOCK00_H
程序输出结果:
Using constructors to create new objects
Constructor using NanoSmartcalled
Company: NanoSmart Shares: 12
Share Price: $20 Total Worth: $240
Constructor using Boffo Objectscalled
Company: Boffo Objects Shares: 2
Share Price: $2 Total Worth: $4
Assigning stock1 to stock2:
Listing stock1 and stock2:
Company: NanoSmart Shares: 12
Share Price: $20 Total Worth: $240
Company: NanoSmart Shares: 12
Share Price: $20 Total Worth: $240
Using a constructor to reset an object
Constructor using Nifty Foodscalled
Bye, Nifty Foods!
Revised stock1:
Company: Nifty Foods Shares: 10
Share Price: $50 Total Worth: $500
Done
Bye, NanoSmart!
Bye, Nifty Foods!