January 13, 2021

C++ Overloading

In this lesson we would cover C++ overloading. Precisely, you’ll learn about Function Overloading and Operator Overloading.

  1. Function Overloading
  2. Operator Overloading

 

1. Function Overloading

In function overloading, two functions have the same name and exist in the same scope. However, both functions have they must have different arguments specification. In this case, we say that they are overloaded functions.

For example, all these are overloaded functions

// Example of Overloaded functions
int getValue() { }

int getValue(int x) { }

float getValue(double x) { }

int getValue(int x, double y) { }

 

About return type: notice from the examples that not all overloaded functions have the same return time. The key is this: overloaded functions must have different argument. This is regardless of whether they have different return types or not.

So the code below would result in error.

//Error: different return types, but same parameters
double getSum(int a, int b) { }

float getSum(int a, int b) { }

Let’s write a program to demonstrate how function overloading works.

//Demonstrating function overriding
#include <iostream>

using namespace std;

int getTotal(int x, int y, int z) {
  return x + y + z;
}

double getTotal(double x, double y, int z) {
  return x + y;
}

int main() {
  int total1 = getTotal(3, 5, 2);

  double total2 = getTotal(4.3, 7.25, 10);

  cout << "First total: " << total1 <<endl;

  cout << "Second total: " << total2;

  return 0;
}

Output:

First total: 10
Second total: 11.55

This code is quite clear, so I’m not going to go over it. However, I recommend you run it to understand how it works

 

2. Operator Overloading

Basically, the concept of overloading is about ‘one thing being able to perform different operations’. For example, we can make existing operator perform different action.

To overload an operator, we use the keyword ‘operator’ followed by the operator we want to overload. Then in curly braces, we define the operation that need to be done. In the code below, we overload the + operator to add two Square objects.

#include <iostream>
using namespace std;

class Square {
   public:
      double getArea(void) {
         return length * height;
      }

      void setLength( double len ) {
         length = len;
      }

      void setHeight( double hei ) {
         height = hei;
      }

      // Overload + operator to add two Square objects.
      Square operator+(const Square& s) {
    	 Square square;
    	 square.length = this->length + s.length;
    	 square.height = this->height + s.height;
         return square;
      }

   private:
      double length;      // Length of a square
      double height;      // Height of a square
};

int main() {
   Square square1;    // Declare square1
   Square square2;    // Declare square2
   Square square3;    // Declare square3
   double area = 0.0; // Store area of a square

   // square 1 specification
   square1.setLength(9.0);
   square1.setHeight(12.0);

   // square 2 specification
   square2.setLength(11.0);
   square2.setHeight(6.0);

   // area of square 1
   area = square1.getArea();
   cout << "Area of sueare1 : " << area <<endl;

   // area of square 2
   area = square2.getArea();
   cout << "Area of square2 : " << area <<endl;

   // Add two square as follows:
   square3 = square1 + square2;

   // area of square 3
   area = square3.getArea();
   cout << "Area of square3 : " << area <<endl;

   return 0;
}

The program yields the following output:

Area of sueare1 : 108
Area of square2 : 66
Area of square3 : 360

From the program above, we have overloaded the + operator to be able to add two Squares. The operation produces a third square whose length is the sum of the two given squares. Similarly, the height is the sum of the two squares being summed.

However, the following operators cannot be overloaded:

  • scope resolution(::)
  • pointer (*)
  • dot (.)
  • ternary (?:)

Leave a Reply

Your email address will not be published. Required fields are marked *