Multiple Type Arguments-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Multiple, Type, Arguments, User, Function, Define, Object, Orient, Operator, Class, String, Overload, Char

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 33
Lecture No. 33
Recap
Recap
Templates are generic abstractions
Templates are generic abstractions
C++ templates are of two kinds
C++ templates are of two kinds
Function Templates
Function Templates
Class Templates
Class Templates
A general template can be specialized to
A general template can be specialized to
specifically handle a particular type
specifically handle a particular type
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Multiple Type Arguments-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object

Object

Oriented Programming

Oriented Programming

(OOP)

(OOP)

Lecture No. 33

Lecture No. 33

docsity.com

Multiple Type Arguments

Multiple Type Arguments

template< typename T, typename U >

template< typename T, typename U >

T my_cast( U u ) { T my_cast( U u ) {

return (T)u;

return (T)u;

int main() { int main() {

double d = 10.5674;

double d = 10.5674;

int j = my_cast( d ); int j = my_cast( d ); //Error//Error

int i = my_cast< int >( d );

int i = my_cast< int >( d );

return 0; return 0;

docsity.com

User

User

Defined Types

Defined Types

class String { class String {

char* pStr;

char* pStr;

friend bool operator ==(

friend bool operator ==(

const String&, const String& ); const String&, const String& );

docsity.com

User

User

Defined Types

Defined Types

template< typename T >

template< typename T >

bool isEqual( T x, T y ) { bool isEqual( T x, T y ) {

return ( x == y ); return ( x == y );

int main() {

int main() {

String s1 = String s1 = ““xyzxyz””, s2 =, s2 = ““xyzxyz””;;

isEqual( s1, s2 ); isEqual( s1, s2 ); // OK// OK

return 0; return 0;

docsity.com

Example

Example

Overloading vs Templates

Overloading vs Templates

String operator +( const char * str1, String operator +( const char * str1,

const String& y ) { const String& y ) {

String tmp; String tmp;

tmp.pStr = new char[ strlen(str1) +

tmp.pStr = new char[ strlen(str1) +

strlen(y.pStr) + 1 ]; strlen(y.pStr) + 1 ];

strcpy( tmp.pStr, str1 ); strcpy( tmp.pStr, str1 );

strcat( tmp.pStr, y.pStr );

strcat( tmp.pStr, y.pStr );

return tmp; return tmp;

} } docsity.com

Template Arguments as Policy

Template Arguments as Policy

Policy specializes a template for an

Policy specializes a template for an

operation (behavior)

operation (behavior)

docsity.com

Second Solution

Second Solution

int compare( char* str1, char* str2,

int compare( char* str1, char* str2,

bool caseSen )

bool caseSen )

for (int i = 0; i < strlen( str1 )

for (int i = 0; i < strlen( str1 )

&& i < strlen( str2 ); i++)

&& i < strlen( str2 ); i++)

if (if ( …… ))

return str1[i]

return str1[i]

str2[i];

str2[i];

return strlen(str1)

return strlen(str1)

strlen(str2);

strlen(str2);

docsity.com

Third Solution

Third Solution

class CaseSenCmp { class CaseSenCmp {

public: public:

static int isEqual( char x, char y )

static int isEqual( char x, char y )

return x == y;

return x == y;

docsity.com

Sample Output

Sample Output

Case Sensitive: 32 Case Sensitive: 32 // Not Equal// Not Equal

Non Non--case Sensitive: 0case Sensitive: 0 // Equal// Equal

docsity.com

Third Solution

Third Solution

int main() {

int main() {

int i, j; int i, j;

char *x = "hello", *y = "HELLO";

char *x = "hello", *y = "HELLO";

i = compare(x, y); i = compare(x, y);

j = compare< NonCaseSenCmp >(x, y);

j = compare< NonCaseSenCmp >(x, y);

cout << "Case Sensitive: " << i; cout << "Case Sensitive: " << i;

cout << "

cout << " \

\

nNon

nNon

Case Sensitive:

Case Sensitive: “

<< j << endl; << j << endl;

return 0;

return 0;

docsity.com