四、模板
1.函数模板
funcTemplate.cpp
# include <iostream>
using namespace std;
# if 0
template < class T > T add ( T x, T y)
{
return x + y;
}
# else
template < class T = int > T add ( T x, T y) ;
# endif
int main ( int argc, char * argv[ ] )
{
int a, b;
cin >> a >> b;
cout << add ( a, b) << endl;
cout << add ( 3.1 , 2.1 ) << endl;
cout << add< > ( 12 , 34 ) << endl;
cout << add< float > ( 1.1 , 2.1 ) << endl;
}
# if 0
# else
template < class B > B add ( B x, B y)
{
return x + y;
}
# endif
funcTemplate.cpp
# include <iostream>
using namespace std;
template < class T , class B > B add ( T x, B y) {
return x+ y;
}
int main ( )
{
cout << add ( 1 , 2 ) << endl;
cout << add ( 1 , 1.2 ) << endl;
cout << add< > ( 2 , 3 ) << endl;
cout << add< > ( 2.1 , 4 ) << endl;
cout << add< int , double > ( 1 , 3.1 ) << endl;
cout << add< int > ( 2 , 3.1 ) << endl;
cout << add< double > ( 2 , 3.1 ) << endl;
cout << add< double , int > ( 2 , 3.1 ) << endl;
return 0 ;
}
2.非类型函数模板
noTypeTemplate.cpp
# include <iostream>
using namespace std;
# define MAX 12
template < class T , int max = MAX> bool getmem ( T * * p)
{
char buf[ max] = { 0 } ;
cout << sizeof ( buf) << endl;
* p = new T[ max] ;
if ( p)
return true ;
return false ;
}
int main ( int argc, char * argv[ ] )
{
int * p = NULL ;
if ( getmem< int , 32 > ( & p) ) {
for ( int i = 0 ; i < MAX; i++ )
p[ i] = i;
for ( int i = 0 ; i < MAX; i++ )
cout << p[ i] << " " ;
cout << endl;
}
}
3.类模板
classTemplate.cpp
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;
template < class T = int >
class Demo {
public :
Demo ( T) ;
T getValue ( ) ;
void setValue ( T) ;
private :
T x;
} ;
template < class T >
Demo < T> :: Demo ( T x) : x ( x) { }
template < class T >
T Demo < T> :: getValue ( ) {
return x;
}
template < class T >
void Demo < T> :: setValue ( T x) {
this -> x = x;
}
int main ( )
{
Demo < int > obj ( 12 ) ;
cout << obj. getValue ( ) << endl;
Demo < > obj1 ( 15 ) ;
return 0 ;
}
4.非类型类模板
ClassTemplate.cpp
# include <iostream>
using namespace std;
template < class T , int max>
class Demo {
public :
Demo ( T) ;
private :
T buf[ max] ;
} ;
template < class T , int max>
Demo < T, max> :: Demo ( T x) {
for ( int i= 0 ; i< max; i++ ) {
buf[ i] = x+ i;
}
}
int main ( )
{
Demo< int , 10 > obj ( 3 ) ;
return 0 ;
}
5.友元模板函数
friendClassTemplate.cpp
# include <iostream>
using namespace std;
template < class T >
class Base {
public :
Base ( T x) : x ( x) { }
# if 0
T operator + ( Base< T> & obj) {
return x + obj. x;
}
# elif 0
T operator + ( Base< T> & ) ;
# elif 0
friend T operator + ( Base< T> & obj1, Base< T> & obj2) {
return obj1. x + obj2. x;
}
# else
template < class B >
friend B operator + ( Base< B> & , Base< B> & ) ;
# endif
private :
T x;
} ;
# if 0
# elif 0
template < class B > B Base< B> :: operator + ( Base< B> & obj)
{
return x + obj. x;
}
# else
template < class T >
T operator + ( Base< T> & obj1, Base< T> & obj2)
{
return obj1. x + obj2. x;
}
# endif
int main ( int argc, char * argv[ ] )
{
Base< int > a ( 2 ) , b ( 3 ) ;
cout << a + b << endl;
Base< double > c ( 2.1 ) , d ( 3.1 ) ;
cout << c + d << endl;
}
C++自学笔记(day6) C++自学笔记(目录)