程序清单10.1+10.2+10.3
头文件stock.h
#ifndef STOCK00_H_ //先测试x是否被宏定义过
#define STOCK00_H_ //如果没有宏定义,就宏定义x并编译下面的语句 #include <string>
class Stock //类声明
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val=shares*share_val; }
public:
void acquire(const std::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 //如果已经定义了则编译#endif后面的语句
stock.cpp
#include<iostream>
#include "stock.h" void Stock::acquire(const std::string &co,long n,double pr){
company=co;
if(n<){
std::cout<<company<<std::endl;
shares=;
}else
shares=n;
share_val=pr;
set_tot();
} void Stock::buy(long num,double price){
if(num<)
std::cout<<"Error!\n";
else{
shares+=num;
share_val=price;
set_tot();
}
} void Stock::sell(long num,double price){
using std::cout;
if(num<)
cout<<"Error!\n";
else if(num>shares)
cout<<"You can not sell more than you have!\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<<"\nShare Price: $"<<share_val<<", Total Worth: $"<<total_val<<'\n';
}
main.cpp
//用于测试Stock类的设计
#include<iostream>
#include "stock.h" void main(){
Stock cat;
cat.acquire("NanoSmart",,12.50);
cat.show();
cat.buy(,18.125);
cat.show();
cat.sell(,20.0);
cat.show();
cat.buy(,40.125);
cat.show();
cat.sell(,0.125);
cat.show();
system("pause");
}
修改stock.cpp文件中的show方法,使其数字格式统一
void Stock::show(){
using std::cout;
using std::ios_base;
ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);//设置cout对象的一个标记,命令cout使用定点表示法
std::streamsize prec=cout.precision();//cout使用定点表示法时,显示三位小数 cout<<"Company: "<<company<<", Shares:"<<shares<<"\nShare Price: $"<<share_val<<", Total Worth: $"<<total_val<<'\n';
cout.setf(orig,ios_base::floatfield);
cout.precision(prec);
}
小结
构造函数和析构函数
程序清单10.4+10.5+10.6
头文件
#ifndef STOCK10_H_ //先测试x是否被宏定义过
#define STOCK10_H_ //如果没有宏定义,就宏定义x并编译下面的语句
#include <string> class Stock //类声明
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val=shares*share_val; }
public:
Stock(); //默认构造函数
Stock(const std::string &co,long n=,double pr=0.0);
~Stock(); //析构函数
void buy(long num,double price);
void sell(long num,double price);
void update(double price);
void show();
}; //千万别忘了分号 #endif //如果已经定义了则编译#endif后面的语句
函数定义
#include<iostream>
#include "stock.h"
using namespace std; //构造函数
Stock::Stock(){
cout<<"Default constructor called\n";
company="no name";
shares=;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const std::string &co,long n,double pr){
cout<<"Constructor using "<<co<<" called\n";
company=co;
if(n<){
cout<<company<<endl;
shares=;
}else
shares=n;
share_val=pr;
set_tot();
}
//类的析构函数
Stock::~Stock(){
cout<<"Bye, "<<company<<endl;
} void Stock::buy(long num,double price){
if(num<)
std::cout<<"Error!\n";
else{
shares+=num;
share_val=price;
set_tot();
}
} void Stock::sell(long num,double price){
if(num<)
cout<<"Error!\n";
else if(num>shares)
cout<<"You can not sell more than you have!\n";
else{
shares-=num;
share_val=price;
set_tot();
}
} void Stock::update(double price){
share_val=price;
set_tot();
} void Stock::show(){
ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);//设置cout对象的一个标记,命令cout使用定点表示法
streamsize prec=cout.precision();//cout使用定点表示法时,显示三位小数
cout<<"Company: "<<company<<", Shares:"<<shares<<"\nShare Price: $"<<share_val;
cout.precision();//显示2位小数
cout<<", Total Worth: $"<<total_val<<endl;
cout.setf(orig,ios_base::floatfield);
cout.precision(prec);
}
主函数
#include<iostream>
#include "stock.h" void main(){
{
using std::cout;
cout<<"Using constructors to create new objects!!!\n";
Stock stock1("NanoSmart",,20.0);
stock1.show();
Stock stock2=Stock ("Boffo Object",,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",,50.0);
cout<<"Revised stock1:\n";
stock1.show();
cout<<"Done\n";
}//代码执行到此处后,调用析构函数。若没有这对括号,程序将在代码退出时调用(即我们无法观察到析构函数的调用)
system("pause");
}
最后两行输出是析构函数调用的结果。
栈先进后出
辨析三个const
如声明 const int func(const int & a) const
第一个表示返回值是个int型的常值
第二个代表const int的引用,a可读不可改
第三个代表这个函数类中的成员变量,不可修改
const Stock & topval(const Stock &s) const;//返回const Stock类型的引用:参数是引用,返回值也是引用
程序清单10.7+10.8+10.9(this)
#ifndef STOCK
#define STOCK
#include <string> class Stock //类声明
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val=shares*share_val; }
public:
Stock(); //默认构造函数
Stock(const std::string &co,long n=,double pr=0.0);
~Stock(); //析构函数
void buy(long num,double price);
void sell(long num,double price);
void update(double price);
void show() const; //const在后面表示不可修改函数内部的值
const Stock & topval(const Stock &s) const;//返回const Stock类型的引用:参数是引用,返回值也是引用
}; //千万别忘了分号 #endif
#include<iostream>
#include "stock.h"
using namespace std; //构造函数
Stock::Stock(){
company="no name";
shares=;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const std::string &co,long n,double pr){
company=co;
if(n<){
cout<<company<<endl;
shares=;
}else
shares=n;
share_val=pr;
set_tot();
}
//类的析构函数
Stock::~Stock(){
} void Stock::buy(long num,double price){
if(num<)
std::cout<<"Error!\n";
else{
shares+=num;
share_val=price;
set_tot();
}
} void Stock::sell(long num,double price){
if(num<)
cout<<"Error!\n";
else if(num>shares)
cout<<"You can not sell more than you have!\n";
else{
shares-=num;
share_val=price;
set_tot();
}
} void Stock::update(double price){
share_val=price;
set_tot();
} void Stock::show() const{
ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);//设置cout对象的一个标记,命令cout使用定点表示法
streamsize prec=cout.precision();//cout使用定点表示法时,显示三位小数
cout<<"Company: "<<company<<", Shares:"<<shares<<"\n Share Price: $"<<share_val;
cout.precision();//显示2位小数
cout<<", Total Worth: $"<<total_val<<endl;
cout.setf(orig,ios_base::floatfield);
cout.precision(prec);
} const Stock & Stock::topval(const Stock &s) const{
if(s.total_val>total_val)
return s;
else
return *this;
}
#include<iostream>
#include "stock.h" const int STKS=;
void main(){
Stock stocks[STKS]={
Stock("Nano",,20.0),
Stock("Boffo",,2.0),
Stock("Mono",,3.25),
Stock("Fleep",,6.5),
};
std::cout<<"Stock holdings:\n";
int st;
for (st = ; st < STKS; st++)
stocks[st].show();
const Stock * top=&stocks[];
for (st = ; st < STKS; st++)
top=&top->topval(stocks[st]);
std::cout<<"\nMost valuale holding:\n";
top->show();
system("pause");
}
程序清单10.10+10.11+10.12(栈)
#ifndef STACK
#define STACK typedef unsigned long Item; class Stack //类声明
{
private:
enum{MAX=};
Item items[MAX];
int top;
public:
Stack(); //默认构造函数
bool isempty() const;
bool isfull() const;
bool push(const Item & item);//压栈
bool pop(Item & item);//出栈
}; #endif
#include "stack.h" //构造函数
Stack::Stack(){
top=;
} bool Stack::isempty() const{
return top==;//top为0返回true,反之为false
}
bool Stack::isfull() const{
return top==MAX;
}
bool Stack::push(const Item &item){
if(top<MAX){
items[top++]=item;
return true;
}else
return false;
}
bool Stack::pop(Item &item){
if(top>){
item=items[--top];
return true;
}else
return false;
}
#include<iostream>
#include<cctype>
#include "stack.h"
using namespace std; void main(){
Stack st;
char ch;
unsigned long po;
cout<<"Enter A to add a purchase order, P to process a PO, Q to quit.\n";
while (cin>>ch&&toupper(ch)!='Q')
{
while(cin.get()!='\n')//如果不是换行就继续
continue;
if (!isalpha(ch))//如果不是字母就继续
{
cout<<'\a';
continue;
}
switch (ch)
{
case 'A':
case 'a':
cout<<"Enter a PO number to add: ";
cin>>po;
if(st.isfull())//该方法判断:如果满了(top等于MAX)返回true,没满返回false
cout<<"Stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p':
if(st.isempty())
cout<<"Stack already empty\n";
else{
st.pop(po);
cout<<"PO #"<<po<<" popped\n";
}
break;
}
cout<<"Enter A to add a purchase order, P to process a PO, Q to quit.\n";
}
system("pause");
}