在某些场景下,继承和包含并不总是能满足重用代码的需要。比如,容器类。容器类设计用来存储其他对象或数据类型。
比如如下的Stack类:
Stack.h
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
using namespace std;
typedef unsigned long Type;
class Stack{
private:
enum {MAX = 10};
Type items[MAX];
int top;
public:
Stack();
bool isEmpty() const;
bool isFull() const;
bool push(const Type & item);
bool pop(Type & item);
};