Site icon DataFlair

Function and Operator Overloading in C++ | Become an Expert in 7 Min

Free C++ course with real-time projects Start Now!!

As the name itself suggests, the term “overloading” refers to assigning multiple operations to a single entity. So, overloading in C++ refers to the capability of an identifier to be used in order to define multiple methods that differ in terms of parameters.

Today, we’ll explore every aspect of Function and Operator Overloading in C++. This article would help you gain the essence of polymorphism by learning in detail two of its implementation techniques called function overloading and operator overloading.

In order to avoid any confusion between function overloading and polymorphism in C++, refer to this link in which we have made clear the differences between the two.

Significance of Overloading in C++

It is a healthy practice to use function overloading and operator overloading in C++ because if any errors are present in the code, they are resolved before runtime as function and operator overloading in C++ are types of compile-time polymorphism.

If any errors persist during compile-time, they can easily be rectified and hence any scope of runtime errors are eliminated and the runtime performance of the program increases.

What is Function Overloading in C++

C++ offers the use of the concept of function overloading to make the code less redundant by assigning a similar name to different entities by giving various sets of parameters or arguments.

In simple words, two or more functions have the same name but are used for different purposes.

Consider a situation where you want to perform a function display() to display the value of the “2012” using the same name for the function ‘display’ and print it in the form of an integer value and a string.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

C++ allows us to overload operators in 3 ways. They are:

Enhance your fundamentals skills with Functions in C/C++

C++ Function Overloading Example

Let us better understand how we can implement function overloading in C++ with the help of a program with reference to the above-stated problem:

#include <iostream> 
using namespace std; 

void display(int integer) 
{ 
cout<<"The value in integer form is: "<<integer<<endl; 
} 
void display(string word) 
{ 
cout<<"The value in string form is: "<<word<<endl; 
} 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

display(2012); 
display("Two thousand twelve"); 
return 0; 
}

Code-

Output-

What is Operator Overloading in C++?

Just like function overloading, operator overloading in C++ follows the similar concept of using the same name for different methods.

Operator overloading is achieved with the help of Classes and Objects in C++. C++ gives you the provision to re-define inbuilt operators. In simple words, a C++ programmer can use operators with user-defined types. The keyword ‘operator’ is of immense importance when dealing with operator overloading.

This is how you would declare your own operator:

class_name operator operator_symbol (const class_name&);

We can define most of the overloaded operators as ordinary non-member functions or as class member functions in C++.

In order to define an operator as a non-member function of class, we need to pass two parameters for each operand:

class_name operator operator_symbol (const class_name&, const class_name&);

Refer Operators in C/C++ for more clear understanding 

There are certain operators that the programmer does not have permission to overload. They are as follows:

C++ allows the overloading of only certain operations. Here is a list of operators that can be overloaded:

+ & < << += |= new
| <b>= &= , *= new []
* > <= >= ++ <<= delete
/ >> <b>== != && -> delete []
% -= || %= ^= >>= ()
/= ~ ! ->* [] ^

Implementation

C++ lets us implement operator overloading in three ways:

Its basic syntax is:

friend return_type operator operator_symbol ( variable1, variable2 )
{
// Statement(s)
}

Rules for Operator Overloading in C++

In addition to that, there are a couple of more boundaries while overloading an operator:

C++ Operator Overloading Example

One of the most popular programs that can be implemented using operator overloading in C++ is the addition or subtraction of two complex numbers.

Let us consider a C++ program to better understand how operator overloading by adding two complex numbers with each other:

#include<iostream> 
using namespace std; 

class Complex 
{ 
private: 
int real, imaginary; 
public: 
Complex(int r = 0, int i =0) // Use of a constructor to initialize and store the values of both the variables
{
real = r; 
imaginary = i;
}

Complex operator + (Complex const &c) 
{ 
Complex result; 
result.real = real + c.real; 
result.imaginary = imaginary + c.imaginary; 
return result; 
}

void display() 
{ 
cout << real << " + " << imaginary <<" i"<<endl; 
} 
}; 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Complex c1(3, 5), c2(2, 8); 
Complex c3 = c1 + c2; 
c3.display(); 
return 0;
}

Code-

Output-

Quiz on Operator Overloading in C++

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <iostream>

    #include <string>

    using namespace std;

     

    void print(int i) {

      cout <<i <<” “;

    }

    void print(string  s) {

      cout<< s << ” “;

    }

     

    int main() {

      print(10);

      print(“Hello”);

      return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <iostream>    

    using namespace std;    

    class addition {    

        public:    

    static int add(int a,int b){      

            return a + b;      

        }      

    static int add(int a, int b, int c)      

        {      

            return a + b + c;      

        }      

    };     

    int main(void) {    

        addition C; 

        cout<<C.add(1, 2)<<” “;      

        cout<<C.add(1, 2, 3);     

       return 0;    

    }  

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    #include<iostream>  

    using namespace std; 

      

    class op{

        public:

        int i;

        op(){

            i=1;

        }

        void operator ++()

        {

            i=i+i;

        }

    }; 

    int main()  

    {  

        op O;

        O++;

        cout<<O.i;

        return 0;  

    }

     

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include<iostream>  

    using namespace std; 

      

    class op{

        public:

        int i;

        op(){

            i=1;

        }

        void operator ++()

        {

            i=i+i;

        }

    }; 

    int main()  

    {  

        op O;

        ++O;

        cout<<O.i;

        return 0;  

    }   

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include<iostream>  

    using namespace std; 

      

    class op{

        public:

        int i;

        op(){

            i=1;

        }

        void operator ++(int i)

        {

            i=i+i;

        }

    }; 

    int main()  

    {  

        op O;

        O++;

        cout<<O.i;

        return 0;  

    }   

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <iostream>  

    using namespace std;  

    class base  

    {  

        

        int x;  

        public: 

        base(int i)  

        {  

           x=i;  

        }  

        void operator+(base);  

        void display();  

    };  

      

    void base :: operator+(base a)  

    {  

         

        cout<<x<<” ” <<a.x;  

      

    }  

    int main()  

    {  

        base a1(5);  

        base a2(4);  

        a1+a2;  

        return 0;  

     

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <iostream>  

    using namespace std;  

    class base  

    {  

        

        int x;  

        public: 

        base(int i)  

        {  

           x=i;  

        }  

        void operator+(base);  

        void display();  

    };  

      

    void base :: operator+(base a)  

    {  

         

        int m = x+a.x;

        cout<<m;  

      

    }  

    int main()  

    {  

        base a1(5);  

        base a2(4);  

        a1+a2;  

        return 0;  

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <iostream>

    using namespace std;

     

    class B {

       public:

        void display() {

            cout << “B”;

        }

    };

     

    class D : public B {

       public:

        void display() {

            cout << “D” << endl;

        }

    };

     

    int main() {

        D d1;

        d1.B::display();

        return 0;

    }

     

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include <iostream>

    using namespace std;

     

    class B {

       public:

        void display() {

            cout << “B”;

        }

    };

     

    class D : public B {

       public:

        void display() {

            cout << “D” << endl;

        }

    };

     

    int main() {

        D d1;

        B *p=&d1;

        p->display();

        return 0;

    }

     

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <iostream>

    using namespace std;

     

    class B {

       public:

        virtual void display() {

            cout << “B”;

        }

    };

     

    class D : public B {

       public:

        void display() {

            cout << “D” << endl;

        }

    };

     

    int main() {

        D d1;

        B *p=&d1;

        p->display();

        return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    Which among the following is not true with respect to function overloading?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

     Which among the following is not true with respect to function overriding?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    What is true with respect to overloading and overriding both.

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    Which of the following can occur without inheritance

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    Which among the following comes under run time polymorphism?

    Correct
    Incorrect

Summary

Overloading is the widely used concept of OOP. You can find many questions related to operator and functional overloading in interviews and Quizzes. If you are master in C++ overloading, you can easily land your dream job. Now, it’s your turn to practice and showcase your talent. Hope, it will help you in taking a step forward towards your success.

It’s time to move on the next article – Exception Handling in C++ 

Enjoyed this article?? Share your experience in the comment section!

Exit mobile version