Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Class Template Specialization - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

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

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(42)

214 documents

1 / 25

Toggle sidebar

Related documents


Partial preview of the text

Download Class Template Specialization - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 35 Docsity.com Member Templates • A class or class template can have member functions that are themselves templates Docsity.com Because class Complex<double> { double real, imag; public: Complex( double r, double im ) : real(r), imag(im) {} Complex(const Complex<double>& c) : real( c.real ), imag( c.imag ) {} … }; Docsity.com …Member Templates template<typename T> class Complex { T real, imag; public: Complex( T r, T im ) : real(r), imag(im) {} template <typename U> Complex(const Complex<U>& c) : real( c.real ), imag( c.imag ) {} … }; Docsity.com …Member Templates int main() { Complex< float > fc( 0, 0 ); Complex< double > dc = fc; // OK return 0; } Docsity.com Class Template Specialization • Like function templates, a class template may not handle all the types successfully • Explicit specializations are provided to handle such types Docsity.com …Class Template Specialization int main() { Vector< int > iv1( 2 ); iv1[0] = 15; iv1[1] = 27; Vector< int > iv2( iv1 ); Vector< int > iv3( 2 ); iv3 = iv1; return 0; } Docsity.com …Class Template Specialization int main() { Vector< char* > sv1( 2 ); sv1[0] = "Aamir"; sv1[1] = "Nasir"; Vector< char* > sv2( sv1 ); Vector< char* > sv3( 2 ); sv3 = sv1; return 0; } Docsity.com …Class Template Specialization template<> Vector<char*>::Vector(int s) { size = s; if ( size != 0 ) { ptr = new char*[size]; for (int i = 0; i < size; i++) ptr[i] = 0; } else ptr = 0; } Docsity.com …Class Template Specialization template<> Vector< char* >::Vector( const Vector<char*>& copy ) { size = copy.getSize(); if ( size == 0 ) { ptr = 0; return; } Docsity.com …Class Template Specialization 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; } Docsity.com …Class Template Specialization template<> const Vector<char*>& Vector<char*>:: operator=(const Vector<char*>& right) { if ( this == &right ) return *this; for (int i = 0; i < size; i++) delete [] ptr[i]; delete [] ptr; Docsity.com …Class Template Specialization size = right.size; if ( size == 0 ) { ptr = 0; return *this; } ptr = new char*[size]; Docsity.com …Class Template Specialization for (int i = 0; i < size; i++) if ( right.ptr[i] != 0 ) { ptr[i] = new char[strlen( right.ptr[i] ) + 1]; strcpy( ptr[i], right.ptr[i] ); } else ptr[i] = 0; } Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved