/************************************************************************/
/*
1、一般来说,包含类声明的头文件和包含方法定义的源代码文件的文件名应相同,以便跟踪哪些文件与那些文件是一起的。
将类声明和成员函数放在不同的文件中,可以将接口的抽象定义(类声明)和实现细节(成员函数定义)分开。
*/
/************************************************************************/
#ifndef STOCK1_H_
#define STOCK1_H_
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() {total_val = share_val;}
public:
Stock();
Stock(const char * co, int n = 0, double pr = 0.0);
~Stock();
void buy (int num, double price);
void sell (int num, double price);
void update(double price);
void show();
};
#endif
/************************************************************************/
/*
1、构造函数:
a)构造函数初始化对象两种方式:
第一种,显式的调用构造函数:
Stock food = Stock("World Cabbage", 250, 1.25);
第二种,隐式调用构造函数:
Stock garment("Furry Mason", 50, 2.5);
b)构造函数与new一起使用:
Stock *pstock = new Stock("Electroshock Games", 18, 19.0);
这条语句创建一个Stock对象,将其初始化为参数提供的值,并将该对象的地址赋给pstock指针。
c)因为在构造函数构造出对象之前,对象是不存在的,因此构造函数被用来创建对象,而不能通过对象来调用;
2、析构函数:
与构造函数不同的是析构函数没有对象
*/
/************************************************************************/
#include <iostream>
// 将文件名放在双引号而不是方括号中意味着编译器将原文件所在的目录中搜索
#include "stock1.h"
// 构造函数
Stock::Stock()
{
std::cout << "Default constructor called \n";
std::strcpy(company, "no name");
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const char * co, int n, double pr)
{
std::cout << "Constructor using " << co << " called \n";;
std::strncpy(company, co, 29);
company[29] = '\n';
if (n < 0)
{
std::cerr << "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(int num,double price)
{
if(num < 0)
{
std::cerr << "Number of shares purchased can't be negative."
<< "Transation is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(int num, double price)
{
using std::cerr;
if(num < 0)
{
std::cerr << "Number of shares purchased can't be negative."
<< "Transation is aborted.\n";
}
else if(num > shares)
{
cerr << "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::endl;
cout << "Company: " << company
<< " Shares: " << shares << endl
<< " Share Price: $" << share_val
<< " Total Worth: $" << total_val << endl;
}
#include <iostream>
#include "stock1.h"
int main()
{
using std::cout;
using std::ios_base;
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout << "Using constructors 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;
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\n";
return 0;
}