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

Recommended Answers

All 2 Replies

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
}

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;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.