Object-Oriented Programming (OOP) Lecture 33: Templates and User-Defined Types, Slides of Object Oriented Programming

This document from docsity.com covers the topic of templates in object-oriented programming (oop), specifically focusing on function templates and class templates, as well as user-defined types as arguments. It also discusses the differences between overloading and templates, and the use of templates as policies. Examples are provided to illustrate the concepts.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 33
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Object-Oriented Programming (OOP) Lecture 33: Templates and User-Defined Types and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 33

Recap

  • Templates are generic abstractions
  • C++ templates are of two kinds
    • Function Templates
    • Class Templates
  • A general template can be specialized to

specifically handle a particular type

User-Defined Types

  • Besides primitive types, user-defined

types can also be passed as type

arguments to templates

  • Compiler performs static type checking to

diagnose type errors

…User-Defined Types

  • Consider the String class without

overloaded operator “==“

class String {

char* pStr; … // Operator “==“ not defined

};

…User-Defined Types

class String {

char* pStr; … friend bool operator ==( const String&, const String& ); };

… User-Defined Types

bool operator ==( const String& x,

const String& y ) { return strcmp(x.pStr, y.pStr) == 0;

}

Overloading vs Templates

  • Different data types, similar operation

Needs function overloading

  • Different data types, identical operation

Needs function templates

Example

Overloading vs Templates

  • ‘+’ operation is overloaded for different

operand types

  • A single function template can calculate

sum of array of many types

…Example

Overloading vs Templates

String operator +( const char * str1, const String& y ) { String tmp; tmp.pStr = new char[ strlen(str1) + strlen(y.pStr) + 1 ]; strcpy( tmp.pStr, str1 ); strcat( tmp.pStr, y.pStr ); return tmp;

}

…Example

Overloading vs Templates

template< class T >

T sum( T* array, int size ) {

T sum = 0;

for (int i = 0; i < size; i++) sum = sum + array[i];

return sum;

}

Example – Policy

  • Write a function that compares two given

character strings

  • Function can perform either case-sensitive

or non-case sensitive comparison

First Solution

int caseSencompare( char* str1,

char* str2 )

{

for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); ++i) if ( str1[i] != str2[i] ) return str1[i] - str2[i];

return strlen(str1) - strlen(str2);

}

Second Solution

int compare( char* str1, char* str2, bool caseSen )

{

for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); i++) if ( … ) return str1[i] - str2[i];

return strlen(str1) - strlen(str2);

} Docsity.com

…Second Solution

// if condition:

(caseSen && str1[i] != str2[i])

|| (!caseSen &&

toupper(str1[i])

toupper(str2[i]))