Overloading ++ and --, Condition States, and Strings


Overloading ++ and --

Prefix

Postfix

Overloading

In header (.h) file

class MyClass {
    ...
    const MyClass & operator++();  // prefix form
    MyClass operator++(int);       // postfix form
    ...
};

In source (.cpp) file

const MyClass & MyClass::operator++() { // prefix form
    // code to increment data members
    return *this;
}

MyClass MyClass::operator++(int) { // postfix form
    MyClass temp = *this;
    // code to increment data members
    return temp;
}

Condition States

States:

Useful member functions (where in is a stream):

Note:


Strings

C++ string Class

In string library ( #include <string> )

Overloaded support for relational operators ( == != > >= < <= = )

C++ strings are mutable:

 

 

 

 

Useful member functions:

 

 

 

 

 

 

Recommendation:

C-Style Strings

C-style string = an array of characters ending in '\0'

 

 

 

 

 

 

Library support in cstring (need to #include <cstring>):

int    strlen( const char *str )

char * strcpy( char *LHS, char *RHS )

char * strcat( char *LHS, char *RHS )

int    strcmp( const char *LHS, const char *RHS )