我是个菜鸟,还在学习 C++ 语言。问题是,从书中做一个练习,我遇到了我不理解的编译器行为。
头文件。
// stock10.h -- Stock class declaration with constructors, destructor added
#ifndef STOCK10_H_
#define STOCK10_H_
#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:
// two constructors
Stock(); // default constructor
Stock(const std::string & co, long n = 0, double pr = 0.0);
~Stock(); // noisy destructor
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif
类实现。
// stock10.cpp -- Stock class with constructors, destructor added
#include <iostream>
#include "stock10.h"
// constructors (verbose versions)
Stock::Stock() // default constructor
{
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();
}
// class destructor
Stock::~Stock() // verbose class destructor
{
std::cout << "Bye, " << company << "!\n";
}
// other methods
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()
{
using std::cout;
using std::ios_base;
// set format to #.###
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 << " Share Price: $" << share_val;
// set format to #.##
cout.precision(2);
cout << " Total Worth: $" << total_val << '\n';
// restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
主文件。
// usestok1.cpp -- using the Stock class
// compile with stock10.cpp
#include <iostream>
#include "stock10.h"
int main()
{
{
using std::cout;
cout << "Using (non default) 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;
}
您可能已经猜到了,Stock 是类,我创建了非默认构造函数和析构函数来显示消息以查看它们何时“行动”。
这是程序执行的输出:
使用(非默认)构造函数创建新对象
使用 NanoSmart 的构造函数,名为
Company:NanoSmart 股票:12
股价:20.000 美元 总价值:240.00 美元
使用Boffo 对象的构造函数,名为 Company:Boffo
Objects 股票:2
股价:2.000 美元 总价值:4.00 美元
分配股票1 to stock2:
上市 stock1 和 stock2:
公司 NanoSmart 股票:12
股价:20.000 美元 总价值:240.00 美元
公司 NanoSmart 股票:12
股价:20.000 美元 总价值:240.00 美元
使用构造函数重置对象
使用 Nifty Foods 的构造函数,称为
再见,NanoSmart!//为什么?不应该是再见,漂亮的食物吗?
修订后的股票 1:
公司:Nifty Foods 股票:10
股价:50.000 美元 总价值:500.00 美元再见,
NanoSmart
!
再见,漂亮的食物!
在此特定行中:
stock1 = Stock("Nifty Foods", 10, 50.0); // temp object
编译器不应该:
1. 使用构造函数创建一个临时对象
2. 将该对象分配给 stock1 对象
3. 销毁临时对象
消息不应该说 Nifty Foods 而不是 NanoSmart 吗?
我不明白。有什么帮助吗?