附录A 利用C++编写的若干通用程序实践
vector.cpp
#include "stdafx.h"
#include <iostream>//源程序采用#include <iostream.h>
using namespace std;
#include <stdlib.h>
class vector {
int size; float *p;
public:
int ub;//上界(upper bound)=size-1
vector() {
size = 10;
p = new float[size];//开辟大小为size的实数空间,并用指针p指向它
ub = size - 1;}
vector(int n)
{
if (n < 0) { cerr << "illegal vector size" << n << "\n"; exit(1); }
size = n; p = new float[size]; ub = size - 1;
}/*vector(n)*/
~vector() { delete p; }
float&operator[](int iv)
{
if (iv >= 0 && iv <= ub)return p[iv];
else {
cerr << "illegal vector element\n"; exit(1);}
}/*float&*/
};/*class vector*/
//测试class vector
void main()
{
vector a(3); vector b;
vector &c = b;//对象c引用b,即c和b完全相同
a[0] = 4.5; b[0] = a[0]; cout << b[0];
b[1] = 6.7; cout << " " << c[1] << endl;
cin >> b[3];
cout <&l