0

can anybody pls tell me the full examples of address operator overloading,assignment operator overloading stream insertion operator overloading and stream extraction operator overloading?

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by cerf_machine
0

They all are coded the same way, pick one and you have all the others, just change the operator

int operator=(<parameter list here>)
{
  // blabla
}
0

the full +=, = and + operators overloading examples:

class str
{
    char * data;
    int length;

    public:

        str ( const char * _data )
        {
            length = strlen ( _data );

            data = new char[ length + 1 ];
            strcpy ( data, _data );
        }

        ~str ()
        {
            delete [] data;
        }

        str operator + ( const str & );
        void operator += ( const str & );
        void operator = ( str );
};

str str::operator + ( const str & s )
{
    str s0 ( data );
    s0 += s;

    return s0;
}

void str::operator += ( const str & s )
{
    if ( s.length > 0 )
    {
        char * buf = new char [ length + 1 ];

        strcpy ( buf, data );

        delete [] data;
        data = new char [ length + s.length + 1 ];

        strcpy ( data, buf );
        strcat ( data, s.data );

        length = length + s.length;

        delete [] buf;
    }
}

void str::operator = ( str s )
{
    if ( s.length > 0 )
    {
        delete [] data;
        data = new char[ s.length + 1 ];
        strcpy ( data, s.data );

        length = s.length;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.