示例一:?
<!-- stock.h -->
#ifndef STOCK1_H_
#define STOCK1_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:
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
<!-- stock.cpp -->
#include <iostream>
#include "stock.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);
}
<!-- usestock.cpp -->
// compile with stock.cpp
#include <iostream>
#include "stock.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);// Stock("Nifty...) is a temp object
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
}
return 0;
}
Using constructors to create new objects
Constructor using NanoSmart called
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Constructor using Boffo Objects called
Company: Boffo Objects Shares: 2
Share Price: $2.000 Total Worth: $4.00
Assigning stock1 to stock2:
Listing stock1 and stock2:
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Company: NanoSmart Shares: 12
Share Price: $20.000 Total Worth: $240.00
Using a constructor to reset an object
Constructor using Nifty Foods called
Bye, Nifty Foods!// 析构了临时对象!
Revised stock1:
Company: Nifty Foods Shares: 10
Share Price: $50.000 Total Worth: $500.00
Done
Bye, NanoSmart!// 析构了stock2(被赋值为stock1了)!
Bye, Nifty Foods!// 析构了新stock1!
为何没有打印Bye, Boffo Objects!??读懂源码及下述说明,即可理解。
在默认情况下,给类对象赋值时,将把一个对象的成员复制给另一个。在这个例子中,stock2原来的内容将被覆盖。
函数main()结束时,其局部变量(stock1和stock2)将消失。由于这种自动变量被放在栈中,因此最后创建的对象将最先被删除,最先创建的对象将最后被删除(“NanoSmart”最初位于stock1中,但随后被传输到stock2中,然后stock1被重置为“Nifty Food”)。
示例二:
<!-- stack.h -->
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack {
private:
enum {
MAX = 10
};// constant specific to class
Item items[MAX];// holds stack items
int top;// index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item& item);// add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item& item);// pop top into item
};
#endif
<!-- stack.cpp -->
#include "stack.h"
Stack::Stack() {// create an empty stack
top = 0;
}
bool Stack::isempty() const {
return top == 0;
}
bool Stack::isfull() const {
return top == MAX;
}
bool Stack::push(const Item& item) {
if (top < MAX) {
items[top++] = item;
return true;
}
return false;
}
bool Stack::pop(Item& item) {
if (top > 0) {
item = items[--top];
return true;
}
return false;
}
<!-- stacker.cpp -->
#include <iostream>
#include <cctype>// or ctype.h
#include "stack.h"
int main() {
using namespace std;
Stack st;// create an empty stack
char ch;
unsigned long po;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or 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())
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 << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|A
Enter a PO number to add: |17885
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|P
PO #17885 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|A
Enter a PO number to add: |17965
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|A
Enter a PO number to add: |18002
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|P
PO #18002 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|P
PO #17965 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|P
stack already empty
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
|Q
Bye
const成员函数:
const Stock land = Stock("Kludgehorn Properties");
land.show();←编译器拒绝
?对于当前的C++来说,编译器将拒绝第二行。这是什么原因呢?因为show( )的代码无法确保调用对象不被修改——调用对象和const一样,不应被修改。我们以前通过将函数参数声明为const引用或指向const的指针来解决这种问题。但这里存在语法问题:show( )方法没有任何参数。相反,它所使用的对象是由方法调用隐式地提供的。需要一种新的语法——保证函数不会修改调用对象。C++的解决方法是将const关键字放在函数的括号后面。也就是说,show()声明应像这样:
void show() const; // 不允许改变【调用对象】
同样,函数定义的开头应像这样:
void Stock::show() const {...}; // 不允许修改调用对象
以这种方式声明和定义的类函数被称为const成员函数。就像应尽可能将const引用和指针用作函数形参一样,只要类方法不修改调用对象,就应将其声明为const。
this:
一般来说,所有的类方法都将this指针设置为调用它的对象的地址。
所以,可以有如下代码:
const Stock & Stock::topval(const Stock & s) const {
if (s.total_val > total_val)
return s; // 参数对象
else
return *this; // 调用的对象
}
|