Back to Study material
OOP

UNIT-3Functions & Operator Overloading
  • Explain operator overloading?
  • Operator Function
  • Operator is overloaded with the help of operator function.
  • It defines the operations to be performed by overloaded operator.
  • This operator function can be of two types
  • 1. Member operator function2. Non Member operator function
  • Non member operator functions are usually friend functions of class. Operator functions are discussed in preceding section.
  •  Overloading using Member Operator Function   Syntax of an operator function
  • Member operator function is a member of the class.
  • It is declared in a class as follows :
  •  return-type operator OP(argument list); Where OP -> operator to be overloaded.
  • Number of parameters in an argument list depends on the type of operator.
  • Type of Operator

    No. of Arguments

    Unary

    0

    Binary

    1

  • Thus according to above table, overloading unary operators using member functions takes no argument and binary operator takes one argument.
  • It is defined as follows :
  • return-type class-name :: operator OP(argument list)

    {

    Operations that overloaded operator performs

    }

        2.    Write a program in C++ to overload unary minus(-).Solution : 

    #include<iostream>

    using namespace std ;

    class Example

    {

     int x,y,z;

     public:

     

    Example(int p,int q,int r)

    {

     x=p;

     y=q;

     z=r;

    }

    void display()

    {

     cout<<x<<” ”<<y<<” ”<<z;

    }

    void operator-()

    {

     x=-x;

     y=-y;

     z=-z;

    }

    };

    int main()

    {

    Example obj (10,-20,30);

    cout<<”Original numbers are”

    obj.display();

    - obj;                                  

    obj.display();

    return 0;

    } 

     

     

    //Defines class Example

     

     

     

     

    //Parameterized constructor

     

     

     

     

     

     

     

     

     

     // Step 1 Operator function to overload minus

     

     

     

     

     

     

     

     

     

     

     

    // Step 2 Invokes operator function

     

     

    cout<<”Numbers after applying unary minus”

    3.    Write a C++ program to overload binary +, - . #include<iostream>using namespace std ;class Example{int a,b;public:Example(int p, int q){a=p;b=q;}void display(){cout<<a<<” ”<<b<<endl;}Example operator+(Example X);Example operator-(Example X);};Example Example::operator+(Example X){Example E;E.a= a+X.a;E.b= b+X.b;return E}Example Example:: operator-(Example X){Example F;F.a= a-X.a;F.b= b-X.b;return F}int main(){Example obj1(10,20);Example obj2(5,5);Example obj3(0,0);cout<<”Addition is”obj3= obj1+ obj2;obj3.display();cout<<”Subtraction is”obj3= obj1- obj2;obj3.display();return 0 ;}4.    Write C++ program to demonstrate use of binary (“+”) operator overloading to add two complex numbers using member function.              #include<iostream>using namespace std ;class Demo{int real,imag;public:Demo(int p, int q){real=p;imag=q;}void display(){cout<<real<<” +j”<<imag<<endl;}Demo operator+(Demo X);};Demo Demo::operator+(Demo X) {Demo D;D.real=real+X.real;D.imag=imag+D.imag;return D}int main(){Demo obj1(10,20);Demo obj2(5,5);Demo obj3(0,0);cout<<”Addition is”obj3= obj1+ obj2;obj3.display();return 0 ;} 5.    Write a program to overload unary –, ++,--(prefix versions) using friend function. 

    #include<iostream>

    using namespace std ;

    class Example

    {

    int x,y,z;

    public:

    Example(int p,int q,int r)

    {

    x=p;

    y=q;

    z=r;

    }

    void display()

    {

    cout<<x<<” ”<<y<<” ”<<z;

    }

    friend void operator-(Example &O);

    friend void operator++(Example &O);

    friend void operator--(Example &O);

    };

    void operator-(Example & O)

    {

    O.x=-O.x;

    O.y=-O.y;

    O.z=-O.z;

     

    }

    void operator++(Example & O)

    {

    O.x=O.x++;

    O.y=O.y++;

    O.z=O.z++;

    }

     

    void operator--(Example & O)

    {

    O.x=O.x--;

    O.y=O.y--;

    O.z=O.z--;

    }

    };

    int main()

    {

    Example O(10,-20,30);

    cout<<”Original numbers are”;

    O.display();

    - O;

    cout<<”Numbers after applying unary minus”;

    O.display();

    ++ O;

    cout<<”Numbers after applying ++”;

    O.display();

    -- O;

    cout<<”Numbers after applying --”;

    O.display();

    return 0 ;

    }

     

     

    //Defines class Example

     

     

     

     

     

    //Parameterized constructor

     

     

     

     

     

     

     

    //Operator functions to overload
    “-”, “++”, “--”

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    //Invokes operator function for –

     

     

     

    //Invokes operator function for ++

     

     

    //Invokes operator function for --

     6.    Write a C++ program to subtract 2 complex numbers using concept of overloading using friends function. include<iostream>using namespace std ;class Example{int real, imag;public:Example(int p, int q){real=p;imag=q;}void display(){cout<<real<<”+j”<<imag<<endl;}friend Example operator-(Example X1,Example X2);}; Example operator-(Example X1, Example X2){Example E;E.real= X1.real-X2.real;E.imag= X1.imag-X2.imag;return E;}int main(){Example obj1(10,20);Example obj2(5,5);Example obj3(0,0);cout<<Subtraction is”obj3= obj1- obj2;obj3.display();return 0; 7.     Write a Program to overload new and delete operator.

    #include<iostream>

    using namespace std;

    class Employee

    {

    int id;

    int salary;

    public:

    Employee(int x, int y)

    {

    id=x;

    salary=y;

    }

    void display()

    {

    cout<<”Employee id:”<<id<<endl;

    cout<<”Employee salary:”<<salary<<endl;

    }

    void *operator new(size_t size );

    void operator delete(void *p);

    };

    void *Employee::operator new(size_t size )

    {

    void *p;

    cout<<”Overloading new”<<endl;

    p=malloc(size);

    if(!p)

    {

    bad_alloc b;

    throw b;

    }

     

    return p;

    }

    void Employee:: operator delete(void *p)

    {

    cout<<”Overloading delete”<<endl;

    free(p);

    }

    int main()

    {

    Employee *E1,*E2;

    try

    {

    E1= new Employee(10,50000);

    }catch(bad_alloc ba)

    {

    cout<<”Allocation error for E1”;

    return 1;

    }

    try

    {

    E2= new Employee(11,60000);

    }catch(bad_alloc ba)

    {

    cout<<”Allocation error for E2”;

    return 1;

    }

    E1->display();

    E2->display();

    delete E1;

    delete E2;

    return 0;

    }

    Output

    Overloading new

    Overloading new

    10 50000

    11 60000

    Overloading delete

    Overloading delete

     8.    Explain the rules for operator overloading?   Operators which cannot be overloaded All C++ operators can be overloaded except following
  • Class member access operator(.*)
  • Conditional operator(?:)
  • Scope resolution operator(::)
  • Size of operator(sizeof)
  •    Operators which cannot be overloaded by using Friend Function
    Following operators cannot be overloaded using friends
  • () Function call operator
  • =  Assignment operator
  • [] subscripting operator
  • -> class member access operator
  •    Rules for operator overloading 
  • Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
  • We cannot create new operators for overloading. Only built in operators can be overloaded.
  • We cannot redefine meaning of in built operators i.e we cannot change the meaning +, - etc.
  • Precedence and associativity of operators cannot be changed.
  • Overloaded operators cannot have default arguments.
  • Operators overloaded using member functions takes no argument for unary and one argument for binary operator.
  • Operators overloaded using friend functions takes one argument for unary and two arguments for binary operator.
  • 9.    Explain function overloading?
  • Function overloading is one of the way to achieve compile time polymorphism in c++.
  • Overloaded functions have same function name and different argument list.
  • F1 void accept(int x);F2 void accept(float y);F3 void accept(int x, int y);Here accept function is overloaded.
  • F1 and F2 are overloaded as they have same function name but different argument list.
  • Though argument list consists of single argument it is of different data type.
  • F1 and F3 are overloaded as they have same function name and number of arguments are different.
  • F2 and F3 are overloaded as they have same function name and number of arguments are different.
  • Argument list may have different number of arguments OR different data type of arguments OR both.
  • Return type of functions may be same or different.
  •    int add(int x, int y);

       float add(int x, int y);

     Here add functions have different return types but they have same argument list. So they are not overloaded functions. Thus return type does not play any role in function overloading.
  • Overloaded functions perform tasks of similar nature.
  • As all overloaded functions have same name compiler determines the function to be called on the basis of argument list.
  • Compiler searches for the exact match of function prototype and then executes corresponding function.
  • Table : Function prototypes and required function calls

    Sr. No.

    Function Protoypes

    Function calls

    1.

    void accept(int x);

    accept(10);

    2.

    void accept(float y);

    accept(1.5);

    3.

    void accept(int x, int y);

    accept(10,20);

     10. Write a program in C++ for bubble sort using function template.Solution :

    #include<iostream>

    using namespace std;

    template <class T>

    void bubblesort(T A[], int  n)

    {

    for(int i=0;i<=n-1;i++)

          for(int j=n-1;i<j;j--)

          if(A[j]<A[j-1])

         {

    swap (A[j], A[j-1])

    }

     template <class X>

    void swap(X &p, X &q)

    {

    X temp=p;

    p=q;

    q=temp;

    }

     

    int main()

    {

    int array[5];

    cout<<“Enter elements of array”;

    for(int i=0;i<5;i++)

    {

    cin>>a[i];

    }

    bubblesort(array,5);

    cout<<“Sorted array”;

    for(int i=0;i<5;i++)

    {

    cout<<array[i]<<“”;

    return 0;

    }

     

     

     

     //template function for bubble sort

     

     

     

     

     

     // calls template function for swap

     

    // template function for swap

     

     

     

     

     

     

     

     

     

     

     

     //accept array elements for sorting

     

     

     

     

     //calls template function for sorting

    Output

    Enter elements of array

    25 15 30 10 50

    Sorted array 10 15 25 30 50