








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
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!









docsity.com
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
class String { class String {
char* pStr;
char* pStr;
friend bool operator ==(
friend bool operator ==(
const String&, const String& ); const String&, const String& );
docsity.com
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
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
►
►
docsity.com
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
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
Case Sensitive: 32 Case Sensitive: 32 // Not Equal// Not Equal
Non Non--case Sensitive: 0case Sensitive: 0 // Equal// Equal
docsity.com
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