C++ Primer Plus 第十章知识点简化
1. this指针
归纳点:
this
的含义与使用- 习惯加
const
每个成员函数都有一个 this
指针,该指针指向调用对象,若需要引用整个对象则使用*this
(因为this
是对象的地址,所以返回对象用*this
),这一部分内容是怎么来的呢?先思考一个问题:
假如有两个对象需要比较大小之类,假定为stock1与stock2,这时候需要一个成员函数来实现这个功能,假定为topval()
函数,该函数返回大的那个对象,那就有:
const Stock& Stock::topval(const Stock& object) const{}
所以使用的时候就有:
top = stock1.topval(stock2);
// 或者
top = stock2.topval(stock1);
接下来问题就来了,怎么写这个成员函数?看下问号处???怎么填写,我先打个样 -->
const Stock& Stock::topval(const Stock& object) const{
if (object.m_total_val > m_total_val)
return object;
else
return ????
}
//Answer:
// return *this
程序示例:
#ifndef STOCK_H_
#define STOCK_H_
#include <string>
using namespace std;
class Stock {
private: // 默认private(可不写)
string m_company;
long m_shares;
double m_share_val;
double m_total_val;
void set_tot() {
m_total_val = m_shares * m_share_val;
}
public:
Stock();
Stock(const string& company, long shares = 0, double share_val = 0.0);
~Stock();
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show() const;
const Stock& Stock::topval(const Stock& object) const;
};
#endif
#include <iostream>
#include "Stock.h"
using namespace std;
Stock::Stock(){}
Stock::Stock(const string& company, long shares, double share_val){}
Stock::~Stock(){}
void Stock::buy(long num, double price){}
void Stock::sell(long num, double price){}
void Stock::update(double price){}
void Stock::show() const{}
const Stock& Stock::topval(const Stock& object) const{
if (object.m_total_val > m_total_val)
return object;
else
return *this
}
笔记:
2. 抽象数据类型
简要介绍堆栈,这部分来写下堆栈功能:
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long UL;
class Stack{
private:
enum { MAX = 10 }; // constant specific to class
UL items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const UL& item);
bool pop(UL& item);
};
#endif
#include <iostream>
#include "stack.h"
Stack::Stack(){
top = 0;
}
bool Stack::isempty() const{
return top == 0;
}
bool Stack::isfull() const{
return top == MAX;
}
bool Stack::push(const UL& item){
if (top < MAX){
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::pop(UL& item){
if (top > 0){
item = items[--top];
return true;
}
else
return false;
}
#include <iostream>
#include <cctype>
#include "stack.h"
using namespace std;
int main(){
Stack stack;
char ch;
UL num;
cout << "Please enter A to add, P to pop, 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 number to add: ";
cin >> num;
if (stack.isfull())
cout << "stack already full\n";
else
stack.push(num);
break;
case 'P':
case 'p':
if (stack.isempty())
cout << "stack already empty\n";
else {
stack.pop(num);
cout << "NUM #" << num << " popped\n";
}
break;
}
cout << "Please enter A to add, P to pop, or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
输出:
Please enter A to add, P to pop, or Q to quit.
A
Enter a number to add: 1
Please enter A to add, P to pop, or Q to quit.
A
Enter a number to add: 2
Please enter A to add, P to pop, or Q to quit.
P
NUM #2 popped
Please enter A to add, P to pop, or Q to quit.
P
NUM #1 popped
Please enter A to add, P to pop, or Q to quit.
P
stack already empty
Please enter A to add, P to pop, or Q to quit.
Q
Bye
笔记:
Reference:
- 《C++ Primer Plus》第六版 Stephen Prata 著