#include<iostream>
using namespace std;
//基类
class MyArray{
public :
MyArray(int leng);
MyArray() {}
~MyArray() ;
void input();
void display();
protected:
int *alist;
int length;
};
MyArray::MyArray(int leng)
{ length=leng;
alist=new int[leng];
}
MyArray::~MyArray()
{ delete [] this->alist;}
void MyArray::input()
{ cout<<"\t\t\t\t" ;
for(int i=0;i<length;++i)
cin>>alist[i];
}
void MyArray::display()
{ cout<<"\t\t\t\t" ;
for(int i=0;i<length;++i)
cout<<alist[i]<<" ";
cout<<endl;
}
//排序派生类:
class SortArray:virtual public MyArray
{ public:
SortArray() {}
SortArray(int a):MyArray(a){}
void paixu();
};
void SortArray::paixu()
{ for(int i=0;i<length-1;++i)
for(int j=0;j<length-i-1;++j)
if(alist[j+1]<alist[j])
{int temp=alist[j];
alist[j]=alist[j+1];
alist[j+1]=temp;
}
}
//逆转派生类:
class ReArray:virtual public MyArray
{ public:
ReArray() {}
ReArray(int a):MyArray(a){}
void nizhuan();
};
void ReArray::nizhuan()
{
for(int i=0;i<length/2;++i)
{ int temp=alist[i];
alist[i]=alist[length-1-i];
alist[length-1-i]=temp;
}
}
//求平均值派生类:
class AverArray:virtual public MyArray
{ public:
AverArray() {}
AverArray(int a):MyArray(a){}
int Aver();
};
int AverArray::Aver()
{
int sum=0;
for(int i=0;i<length;++i)
sum+=alist[i];
return sum/length;
}
//定义一个new类
class NewArray:public SortArray,public ReArray,public AverArray
{ public :
NewArray() {}
NewArray(int a):MyArray(a) {}
};
int main()
{ //MyArray a(5);
//SortArray a(5);
// AverArray a(5);
NewArray a(5);
cout<<"\t\t\t\t输入原始数据"<<endl;
a.input();
cout<<"\t\t\t\t输出原始数据"<<endl;
a.display();
cout<<"\t\t\t\t进行排序"<<endl;
a.paixu();
cout<<"\t\t\t\t输出排序后的数据"<<endl;
a.display();
cout<<"\t\t\t\t进行逆转"<<endl;
a.nizhuan();
cout<<"\t\t\t\t输出逆转后的数据"<<endl;
a.display();
cout<<"\t\t\t\t输出其平均值"<<endl;
cout<<"\t\t\t\t" ;
cout<<a.Aver()<<endl;
system("pause");
//return 0;
}
实验四
最新推荐文章于 2012-11-07 12:53:32 发布