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

C++ OOP: Member Templates, Specializations, Function Templates, Non-type Parameters, Slides of Object Oriented Programming

This document from docsity.com covers various advanced topics in object-oriented programming (oop) using c++. It explains member templates, their parameterization, template specializations, partial and complete specializations for classes and functions, and non-type parameters with default values. The document also includes examples and usage of these concepts.

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 C++ OOP: Member Templates, Specializations, Function Templates, Non-type Parameters and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 36 Docsity.com Recap – Member Templates • A class template may have member templates • They can be parameterized independent of the class template Docsity.com … Member Templates Revisited int main() { Complex< int > ic( 10, 5 ); Complex< float > fc( 10.5, 5.7 ); Complex< double > dc( 9.567898, 5 ); ComplexSet cs; cs.insert( ic ); cs.insert( fc ); cs.insert( dc ); return 0; } Docsity.com Partial Specialization • A partial specialization of a template provides more information about the type of template arguments than that of template • The number of template arguments remains the same Docsity.com Example – Partial Specialization template< class T > class Vector { }; template< class T > class Vector< T* > { }; Docsity.com Example – Complete Specialization template< class T, class U, class V > class A {}; template< > class A< int, char*, double > {}; Docsity.com Function Templates • A function template may also have partial specializations Docsity.com Example – Partial Specialization template< class T, class U, class V > void func( T, U, V ); template< class T, class V > void func( T, T*, V ); template< class T, class U, int I > void func( T, U ); template< class T > void func( int, T, 7 ); Docsity.com Partial Specialization • Following partial specialization deals with pointers to objects template< typename T > bool isEqual( T* x, T* y ) { return ( *x == *y ); } Docsity.com Using Different Specializations int main() { int i, j; char* a, b; Shape *s1 = new Line(); Shape *s2 = new Circle(); isEqual( i, j ); // Template isEqual( a, b ); // Complete Sp. isEqual( s1, s2 ); // Partial Sp. return 0; } Docsity.com Non-type Parameters • Template parameters may include non- type parameters • The non-type parameters may have default values • They are treated as constants • Common use is static memory allocation Docsity.com Example – Non-type Parameters int main() { Array< char > cArray( 10 ); Array< int > iArray( 15 ); Array< double > dArray( 20 ); return 0; } Docsity.com Example – Non-type Parameters template< class T, int SIZE > class Array { private: T ptr[SIZE]; public: Array(); … }; Docsity.com …Example – Non-type Parameters int main() { Array< char, 10 > cArray; Array< int, 15 > iArray; Array< double, 20 > dArray; return 0; } Docsity.com
Docsity logo



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