WEB DEVELOPER SITE
PYTHONPHP7JAVA
 

C++ Overloading


C++ allows a function and operator in the same scope to specify multiple definitions, called function overloading and Operator overloading.

An overloaded declaration is a declaration that has the same name as a function or method that has been previously declared within the scope, but their parameter list and definition (implementation) are not the same.

When you call a overloaded function or overloaded operator, the compiler compares the type of parameter you are using with the type of parameter in the definition. Decided to choose the most appropriate definition. The process of choosing the most appropriate overloaded function or overloaded operator is called overload decision.

Function overloading in C++

In the same scope, several functions with the same name can be declared, but the formal parameters of the same-named function (the number, type, or order of parameters) must be different. You cannot override a function by simply returning a different type.

In the example below, the same name function print() is used to output different data types:

example

#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Integer is: " << i << endl; } void print(double f) { cout << "Floating point number is: " << f << endl; } void print(char c[]) { cout << "String is: " << c << endl; } }; int main(void) { printData pd; // Output integer pd.print(5); // Output floating point number pd.print(500.263); // Output string char c[] = "Hello C++"; pd.print(c); return 0; }

When the above code is compiled and executed, it produces the following results:

Integer is: 5
The floating point number is: 500.263
The string is: Hello C++

Operator overloading in C++

You can redefine or override most of the C++ built-in operators. This way, you can use operators of custom types.

The overloaded operator is a function with a special name, and the function name consists of the keyword operator and the operator symbol to be overloaded later. Like other functions, overloaded operators have a return type and a list of arguments.

Box operator+(const Box&);

The declaration addition operator is used to add two Box objects and return the final Box object. Most overloaded operators can be defined as ordinary non-member functions or as class member functions. If we define the above function as a non-member function of the class, then we need to pass two parameters for each operation, as follows:

Box operator+(const Box&, const Box&);

The following example demonstrates the concept of operator overloading using member functions. Here, the object is passed as a parameter, and the object's properties are accessed using the this operator, as shown below:

example

#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload + operator for adding two Box objects Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // length double breadth; // width double height; // height }; // Main function of the program int main( ) { Box Box1; // statement Box1,Type is Box Box Box2; // statement Box2,Type is Box Box Box3; // statement Box3,Type is Box double volume = 0.0; // Store the volume in this variable // Box1 Detailed Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 Detailed Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Box1 volume of volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // Box2 volume of volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // Add two objects together to get Box3 Box3 = Box1 + Box2; // Box3 volume of volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }

When the above code is compiled and executed, it will produce the following results:

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

Overloadable operator/non-overloadable operator

The following is a list of overloadable operators:

Binocular arithmetic operator + (plus), - (minus), * (multiply), / (division),% (modulo)
Relational operator ==(equal),!= (not equal),< (Less than),> (more than the >,<=(Less than or equal to),>=(greater or equal to)
Logical operator ||(Logical OR),&&(Logic and),! (Logical)
Monocular operator + (positive), - (negative), * (pointer),&(Take the address)
Self-increasing decrement operator ++ (self-increase), -- (self-reduction)
Bit operator | (Bitwise or),& (by bit and ), ~ (inverted by bit), ^ (bitwise XOR), << (Shift left),>>(Right shift)
Assignment operator =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
Space application and release New, delete, new[ ] , delete[]
Other operators () (function call), ->(Member visit), , (comma), [](subscript)

The following is a list of operators that cannot be overloaded:

  • .: member access operator
  • .*, ->*: member pointer access operator
  • ::: domain operator
  • sizeof:length operator
  • ?:: Conditional Operators
  • #: Preprocessing symbols

operator overload example

The following examples of various operator overloads are provided to help you better understand the concept of overloading.

serial number operator and example
1Unary operator overload
2Binary operator overloading
3Relational operator overloading
4Input/Output operator overloading
5 ++And -- operator overloading
6Assignment operator overloading
7Function call operator () Overload
8Subscript operator [] Overload
9Class member access operator -> Overload