

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Class Template Specialization, Explicit specializations, Float Instantiation, Member Templates, Member functions, Class, Object Oriented Programming, Class template are points you can learn in this Object Oriented Programming lecture.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















int main() {
Complex< float > fc( 0, 0 ); Complex< double > dc = fc; // Error return 0;
}
class Complex {
double real, imag;
public:
Complex( double r, double im ) : real(r), imag(im) {} Complex(const Complex& c) : real( c.real ), imag( c.imag ) {} …
};
int main() {
Complex< float > fc( 0, 0 ); Complex< double > dc = fc; // OK return 0;
}
class Complex {
double real, imag;
public:
Complex( double r, double im ) : real(r), imag(im) {} template Complex(const Complex& c) : real( c.real ), imag( c.imag ) {} …
};
int main() {
Vector< int > iv1( 2 ); iv1[0] = 15; iv1[1] = 27; Vector< int > iv2( iv1 ); Vector< int > iv3( 2 ); iv3 = iv1; return 0;
}
template<>
class Vector< char* > {
private:
int size; char** ptr;
public:
// Vector< char* >( int = 10 ); Vector( int = 10 ); Vector( const Vector< char* >& ); virtual ~Vector();
int getSize() const; const Vector< char* >& operator =( const Vector< char* >& ); const char& operator ; void insert( char, int );
};
template<>
Vector< char* >::Vector(
const Vector& copy ) {
size = copy.getSize(); if ( size == 0 ) { ptr = 0; return; }
ptr = new char*[size]; for (int i = 0; i < size; i++) if ( copy.ptr[i] != 0 ) { ptr[i] = new char[ strlen( copy.ptr[i] ) + 1 ]; strcpy(ptr[i], copy.ptr[i]); } else ptr[i] = 0;
}
template<>
int Vector::getSize() const {
return size;
}
template<>
const Vector& Vector::
operator=(const Vector& right)
{
if ( this == &right ) return *this; for (int i = 0; i < size; i++) delete [] ptr[i]; delete [] ptr;