#include <iostream>
#include <string>
using namespace std;
class MyArray
{
public:
MyArray(int leng);
~MyArray();
void Input();
void Display(string);
protected:
int* alist;
int length;
};
MyArray::MyArray(int leng)
{
if (leng <= 0)
{
cout << "error length";
exit(1);
}
alist = new int[leng];
length = leng;
if (alist == NULL)
{
cout << "assign failure";
exit(1);
}
cout << "MyArray类对象已构建。" << endl;
}
MyArray::~MyArray()
{
delete [] alist;
cout << "MyArray类对象被撤销。" << endl;
}
void MyArray::Input()
{
cout << "请从键盘键入" << length << "个整数:";
int i;
int* p = alist;
for (i = 0;i < length;i++, p++)
cin >> *p;
}
void MyArray::Display(string str)
{
int i;
int* p = alist;
cout << str << length << "个整数";
for (i = 0;i < length;i++, p++)
cout << *p << " ";
cout << endl;
}
class SortArray : public MyArray
{
public:
void Sort();
SortArray(int leng) :MyArray(leng)
{
cout << "SrotArray类对象已创建。" << endl;
}
virtual ~SortArray();
};
void SortArray::Sort()
{
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - 1 - i; j++)
{
if (alist[j] > alist[j + 1])
{
int t = alist[j];
alist[j] = alist[j + 1];
alist[j + 1] = t;
}
}
}
}
SortArray::~SortArray()
{
cout << "SrotArray类对象被撤销。" << endl;
}
int main()
{
SortArray s(5);
s.Input();
s.Display("原始数值");
s.Sort();
s.Display("排序后的数组");
cout << "wsq36" << endl;
system("pause");
return 0;
}