, . , . ++ template . ( ). . , , .
.
:
template < >
Class
- , . .
#include "iostream.h"
#include "string.h"
template <class T>
Class vector
{ T *ms;
int size;
public:
vector(): size(0),ms(NULL) {}
~vector(){delete [] ms;}
void decrem(const T &t) // 1
{ T *tmp = ms;
ms=new T[size+1]; // ms -
if(tmp) memcpy(ms,tmp,sizeof(T)*size); // tmp -> ms
ms[size++]=t; //
if(tmp) delete [] tmp; //
}
void inkrem(void) // 1
{ T *tmp = ms;
if(size>1) ms=new T[--size];
if(tmp)
{ memcpy(ms,tmp,sizeof(T)*size); // .
delete [] tmp; //
}
}
T &operator[](int ind) //
{ // if(ind<0 || (ind>=size)) throw IndexOutOfRange; //
// IndexOutOfRange
return ms[ind];
}
};
void main()
{ vector <int> VectInt;
vector <double> VectDouble;
VectInt.decrem(3);
VectInt.decrem(26);
VectInt.decrem(12); // int- () 3
VectDouble.decrem(1.2);
VectDouble.decrem(.26);// double- () 2
int a=VectInt[1]; // a = ms[1]
cout << a << endl;
int b=VectInt[4]; //
cout << b << endl;
double d=VectDouble[0];
cout << d << endl;
VectInt[0]=1;
VectDouble[1]=2.41;
}
vector 2 : decrem , inkrem [] i- .
vector , new. , vector <int> VectInt, int. , -, .
|
|
VectInt.decrem(3);
VectInt.decrem(26);
VectInt.decrem(12);
() (3, 26 12).
, :
template vector<int>;
vector<int>, .
- , :
#include "iostream.h"
template <class T1,class T2>
T1 sm1(T1 aa,T2 bb) //
{ return (T1)(aa+bb); //
} //
template <class T1,class T2>
Class cls
{ T1 a;
T2 b;
public:
cls(T1 A,T2 B): a(A),b(B) {}
~cls(){}
T1 sm1() //
{ return (T1)(a+b); // obj_
}
T1 sm2(T1,T2); //
};
template <class T1,class T2>
T1 cls<T1,T2>::sm2(T1 aa,T2 bb) //
{ return (T1)(aa+bb); //
}
void main()
{ cls <int,int> obj1(3,4);
cls <double,double> obj2(.3,.4);
cout<<" 1 = "
<<obj1.sm1()<<endl;
cout<<" (int,int) = "
<<obj1.sm2(4,6)<<endl;
cout<<" (int,int) = "
<<sm1(4,.6)<<endl;
cout<<" 2 = "
<<obj2.sm1()<<endl;
cout<<" (double,double)= "
<<obj2.sm2(4.2,.1)<<endl;
}
, :
#include "iostream.h"
template <class T1,int i=0,class T2>
Class cls
{ T1 a;
T2 b;
public:
cls(T1 A,T2 B): a(A),b(B){}
~cls(){}
T1 sm() // -
{ // i+=3; // error member function 'int __thiscall cls<int,2>::sm(void)'
return (T1)(a+b+i);
}
};
void main()
{ cls <int,1,int> obj1(3,2); // const i 1
cls <int,0,int> obj2(3,2,1); // error 'cls<int,0>::cls<int,0>':no overloaded
// function takes 3 parameter s
cls <int,int,int> obj13(3,2,1); // error 'cls': invalid template argument for 'i',
// constant expression expected
cout<<obj1.sm()<<endl;
}
6.
template <class T1,int i=0,class T2> , cls , - (1 2), (int i=0) - . i cls <int, 1,int> obj1(3,2).
|
|
++, , (, -) . , . , .
#include "iostream.h"
#include "string.h"
template <class T1,class T2>
T1 sm(T1 a,T2 b) //
{
return (T1)(a+b); // c 2
}
template <class T1,class T2,class T3>
T1 sm(T1 a,T2 b,T3 c) //
{ return (T1)(a+b+c); // c 3
}
void main()
{cout<<" - sm(int,int) = "<<sm(4,6)<<endl;
cout<<" - sm(int,int,int) = "<<sm(4,6,1)<<endl;
cout<<" - sm(int,double) = "<<sm(5,3)<<endl;
cout<<" - sm(double,int,short)= " <<
sm(.4,6,(short)1)<<endl;
// cout<<sm(" "," ++")<<endl; error cannot add two pointers
}
sm(), 2, 3 . . 1, 2, 3 template <class T1,class T2,class T3>. 1, 2 3 . :
sm(int,int) = 10
sm(int,int,int) = 11
sm(int,double) = 8
sm(double,int,short)= 7.4
sm() , , , . , sm(), sm() . :
char *sm(char *a,char *b) //
{ char *tmp=a; //
a=new char[strlen(a)+strlen(b)+1];
strcpy(a,tmp);
strcat(a,b);
return a;
}
main() , ,
cout<<sm(" "," ++")<<endl;
:
++
, . .