Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Write an array class template to realize data processing of at least two data types, one of which needs to be a custom type. Pay attention to the necessity of packaging.
Write an array class template to realize data processing of at least two data types, one of which needs to be a custom type. Pay attention to the necessity of packaging.
See if it meets your requirements:

# include & ltiostream & gt

# include & lt string & gt

Use namespace std

Template & ltT class & gt

MyArray level

{

Public:

T * data;

int len

Public:

Myanmar ()

{

Data = empty;

len = 0;

}

~ Myanmar ()

{

Delete [] data;

}

T& operator [] (int index);

Empty push (t d);

};

Template & ltT class & gt

T & myarray & ltt> * operator [] (integer index)

{

if(index & lt; 0 | | index & gt(len- 1))

{

Cout & lt& lt "Subscript error!" & lt& ltendl

Exit (1);

}

Return data [index];

}

Template & ltT class & gt

Void MyArray & ltT>* push (total days)

{

T *pdata = data;

data = new T[len+ 1];

if(pdata! = empty)

{

for(int I = 0; I & ltleni++)

{

data[I]= pdata[I];

}

Delete [] pdata;

pdata = NULL

}

data[len]= d;

len++;

}

Structural students

{

String name;

int age

};

//Test code

int main(int argc,char* argv[])

{

MyArray & ltint & gta; //Plastic testing;

A. push (11);

A. push (22);

a . push(33);

A. push (55);

a[0]= 44;

for(int I = 0; I<4; i++)

cout & lt& lta[I]& lt; & ltendl

MyArray & lt structural students & gt Stu; //Custom type test;

for(I = 0; I<2; i++)

{

Struct student stu1;

Cout & lt& lt "Enter student information:"<& ltendl

CIN & gt; & gtstu 1 . name & gt; & gtstu 1 . age;

stu . push(stu 1);

}

for(I = 0; I<2; i++)

Cout & lt& lt Stu [me]. Name & lt& lt" ""<& lt stu [me]. Age<& ltendl

Returns 0;

}

//Looking forward to more exchanges, hehe. . . Good luck: by sx_liang