1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class Vect
{
protected:
int size;
double *data;
public:
Vect(): size(0), data(NULL) {}
Vect(int sz): size(sz) { data = new double[size]; }
Vect(double *v, int sz): size(sz), data(*v) {}
Vect(int sz, const double *v)
{
size = sz;
data = new double[size];
for (int i = 0; i < size; i++)
data[i] = v[i];
}
~Vect() { if (data) delete[] data; }
public:
int GetSize() { return size; }
double& operator[](int index) { return data[index]; }
};
class Sort: public Vect
{
public:
Sort(int sz, const double *v): Vect(sz, v) {}
protected:
void Swap(int i, int j) { double t = data[i]; data[i] = data[j]; data[j] = t; }
virtual Sort& DoSort() = 0;
void Print(ostream& out)
{
for (int i = 0; i < size; i++)
out << ' ' << data[i];
}
friend ostream& operator<<(ostream& out, Sort & v)
{
out << "original:";
v.Print(out);
out << "\nsorted:";
v.DoSort().Print(out);
return (out);
}
};
class SelectionSort: public Sort
{
public:
SelectionSort(int sz, const double *v): Sort(sz, v)
private:
Sort& DoSort()
{
return (*this);
}
};
void main()
{
double a[] = {1, 3, 5, 2, 4};
cout << new SelectionSort(4, A) << endl;
}
|