C++模板编程完善程序

完善程序

#include  <iostream>
#include  <iomanip>
using  namespace  std;
template  <typename  ElemType>
class  myArrayList
{
private:
int  mSize;
int  mLen;
ElemType  *mpArr;
public:
myArrayList(int  n);
myArrayList(ElemType  *a,  int  n);
void  show();
ElemType  getMax();
void  sort();
//*************************1
myArrayList(myArrayList<ElemType>  &other); 

};
//*****************************2
template  <typename  ElemType>
void  myArrayList<ElemType>::sort()
{
for  (int  i  =  0;  i  <mLen;  i++)
{
	for(int j=0;j<mLen-1;j++)
	{
		if(mpArr[j]>mpArr[j+1])
		{ElemType temp;
		temp=mpArr[j];
		mpArr[j]=mpArr[j+1];
		mpArr[j+1]=temp;
		}
	}
}
}
template<typename  ElemType>
myArrayList<ElemType>::myArrayList(ElemType  *a,  int  n)

{
this->mSize  =  n;
this->mLen=n;this->mpArr  =  new  ElemType[this->mLen];
for  (int  i  =  0;  i  < n;  i++)
this->mpArr[i]  =  a[i];
}

template<typename  ElemType>
myArrayList<ElemType>::myArrayList(myArrayList<ElemType>  &other)

{

this->mLen  =  other.mLen;

this->mSize  =  other.mSize;

this->mpArr  =  new  ElemType[this->mLen];

for  (int  i  =  0;  i  <  this->mLen;  i++)

this->mpArr[i]  =  other.mpArr[i];

}

template<typename  ElemType>

myArrayList<ElemType>::myArrayList(int  n)

{

this->mSize  =  n;

this->mLen  =  0;

this->mpArr  =  new  ElemType[mSize];

}
//*************************************

template  <typename  ElemType>

void  myArrayList<ElemType>::show()

{

for  (int  i  =  0;  i  <  mLen;  i++)

cout  <<  mpArr[i]<<"      ";//三个空格

cout  <<  endl;

}

template  <typename  ElemType>
ElemType  myArrayList<ElemType>::getMax()
{
ElemType    max;
max  =  mpArr[0];
for  (int  i  =  1;  i  <  mLen;  i++)
if  (max  <  mpArr[i])
max  =  mpArr[i];
return  max;
}



//Student.h

class  Student
{
private:
int  mId;
float  height;
int  score;
public:
Student(int  id  =  0,  float  h  =  0,  int  s  =  0)  :height(h),  mId(id),  score(s)
{
}

//********************************3
bool operator<(Student& a2){return this->score < a2.score; }
bool operator>(Student& a2){return this->score > a2.score; }
friend ostream &operator <<(ostream &out,const Student &s)//重载输出运算符 
	{
		out << "ID:" << s.mId << "  Height:" << s.height << " Score:" << s.score << endl;
		return out;
	}
};



//主程序

int  main()
{
int  a[]  =  {  1,  2,  3,  5,  7,  9,  12,  8  };
double    b[]  =  {  1,  2.5,  3.6,  5,  7,  9,  12.8,  8  };
myArrayList  <int>  list1(a,  8);
list1.sort();
list1.show();
cout  <<  "max="  <<  list1.getMax()  <<  endl;
myArrayList  <double>  list2(b,  8);
list2.sort();
list2.show();
cout  <<  "max="  <<  list2.getMax()  <<  endl;
Student  s[3]  =  {  Student(1,  175,  80),  Student(2,  178,  90),  Student(3,  195,  83)  },  s1;
myArrayList  <Student>  list3(s,  3);
list3.sort();
list3.show();
cout  <<  "max="<<list3.getMax()<<  endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值