OOPS - [C++ Operator Overloading]

♠ Posted by Unknown in at 03:35

Operator Overloading

To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called “operator overloading”, which describes the task. The general form of an operator function is :

                        return_type classname :: operator op(arglist)
                        {
                          function_body  //task defined
                        }

where return type is a type of value is returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword “operator”. “operator op” is the function name.

Operator functions must be either member functions (non-static) or friend functions. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators and only one argument for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or by reference.

The process of overloading involves the following steps:

1.     Creates a class that defines the data type that is to be used in the overloading operations.
2.     Declares the operator function operator op() in the public part of the class. It may be either a member function or a friend function.
3.     Define the operator function to implement the required operations.

Example of Operator Overloading :

1.     Unary Member Operator Function.

#include<iostream.h>
#include<conio.h>
class Example
{
            int cnt;
 public:
            Example(int x)
            { cnt = x; }
            void operator -()//Unary Member Operator Function
            {
             cnt = -cnt;
            }
            void display()
            {
             cout<<cnt<<endl;
            }
};
void main()
{
 Example S1(-10), S2(45);
 clrscr();
 -S1;
 S1.display();
 -S2;
 S2.display();
 getch();
}

2.     Unary Friend Operator Function.

#include<iostream.h>
#include<conio.h>
class Example
{
            int cnt;
 public:
            Example(int x)
            { cnt = x; }
            friend void operator -(Example &ob)//Unary Friend Operator Function
            void display()
            {
             cout<<cnt<<endl;
            }
};

void operator -(Example &ob)//Unary Friend Operator Function
{
 ob.cnt = -ob.cnt;
}
void main()
{
 Example S1(-10), S2(45);
 clrscr();
 -S1;
 S1.display();
 -S2;
 S2.display();
 getch();
}

3.     Binary Member Operator Function.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class Example
{
            char *p;
            int length;
 public:
            Example()
            {
                        length = 0;
                        p = new char[length + 1];
            }
            Example(char *str)
            {
                        length = strlen(str);
                        p = new char[length + 1];
                        strcpy(p, str);
            }
            void operator +(Example &);
            void display();
};
void Example::operator +(Example &ob)
{
 length = strlen(p);
 ob.length = strlen(ob.p);
 Example tmp;
 tmp.p = new char[length + 1];
 strcpy(tmp.p, p);
 delete p;
 p = new char[length + ob.length + 1];
 strcpy(p, tmp.p);
 strcat(p, ob.p);
 delete tmp.p;
}
void Example::display()
{
            cout<<"The String is :"<<p<<endl;
}

void main()
{
 Example S1("Vakratund"), S2("Computer");
 clrscr();
 S1 + S2;
 S1.display();
 getch();
}

4.     Define a class string. Use overloaded + operator to concatenate two strings.
                                                                                          
  #include<iostream.h>
#include<conio.h>
#include<string.h>
class Example
{
            char *p;
            int length;
 public:
            Example()
            {
                        length = 0;
                        p = new char[length + 1];
            }
            Example(char *str)
            {
                        length = strlen(str);
                        p = new char[length + 1];
                        strcpy(p, str);
            }
            friend Example operator +(Example &, Example &);
            void display();
};

Example operator +(Example &ob1, Example &ob2)
{
 ob1.length = strlen(ob1.p);
 ob2.length = strlen(ob2.p);
 Example tmp;
 tmp.p = new char[ob1.length + 1];
 strcpy(tmp.p, ob1.p);
 delete ob1.p;
 ob1.p = new char[ob1.length + ob2.length + 1];
 strcpy(ob1.p, tmp.p);
 strcat(ob1.p, ob2.p);
 delete tmp.p;
 return ob1;
}

void Example::display()
{
            cout<<"The String is :"<<p<<endl;
}

void main()
{
 Example S1("Vakratund"), S2("Computer"),S3;
 clrscr();
 S3 = S1 + S2;
 S3.display();
 getch();
}

0 comments:

Post a Comment