ExempleTemplateInteger

Aller à : navigation, rechercher

<source lang="c++">

1. include <iostream>
2. include <sstream>
3. include <string>
4. include <gmp.h>

using namespace std;

class Integer {

``` mpz_t m_v;
```

public:

``` ~Integer()
{
cout << "~Integer( )" << endl;
mpz_clear( m_v );
}
Integer()
{
cout << "Integer( )" << endl;
mpz_init( m_v );
}
Integer( int x )
{
cout << "Integer( int )" << endl;
mpz_init_set_si( m_v, x );
}
Integer( const Integer & other )
{
cout << "Integer( Integer & )" << endl;
mpz_init_set( m_v, other.m_v );
}
Integer operator*( const Integer & other ) const
{
cout << "Integer Integer::operator*( Integer & )" << endl;
Integer i;
mpz_mul( i.m_v, m_v, other.m_v );
return i;
}
void selfDisplay( ostream & that_stream ) const
{
char s[ 1000 ];
mpz_get_str( s, 10, m_v );
that_stream << s;
}
```

};

ostream & operator<<( ostream & that_stream, const Integer & object ) {

``` object.selfDisplay( that_stream );
return that_stream;
```

}

template <typename Tint> Tint multiply( const Tint & a, const Tint & b ) {

``` Tint t = a * b;
cout << "[" << sizeof( Tint ) << "]"
<< endl;
cout << a << "*" << b << "=" << t << endl;
return t;
```

}

int main( int argc, char** argv ) {

``` Integer a( 987654321 );
Integer b( 123456789 );
int c = multiply( 5, 12 );
int d = multiply( 17LL, 18LL );
Integer e = multiply( a, b );
Integer f = multiply( e, e );
```

} </source>