Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Overloaded Operators and Overloaded Functions

C++ Overloaded Operators and Overloaded Functions

C++ allows multiple definitions within the same scope for afunctionandoperatorto be specified, called respectivelyfunction overloadingandoperator overloading.

An overloaded declaration is a declaration that has the same name as a previously declared function or method within the same scope, but their parameter lists and definitions (implementations) are different.

When you call aoverloaded functionoroverloaded operator, the compiler decides the most appropriate definition by comparing the parameter types you use with those in the definitions. The process of selecting the most appropriate overloaded function or overloaded operator is calledoverload resolution.

Function Overloading in C++

Within the same scope, you can declare several functions with the same name that have similar functionality, but their formal parameters (in terms of number, type, or order) must be different. You cannot overload a function solely by differing return types.

In the example below, the function with the same name 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

<<

"

Float is:

"

<<

f

<<

endl

;

}

void

print

(

char

c

[

]

)

{

cout

<<

"

String is:

"

<<

c

<<

endl

;

}

}

;

int

main

(

void

)

{

printData

pd

;

// Output integer

pd

.

print

(

5

)

;

// Output float

pd

.

print

(

500

.263

)

;

// Output string

char

c

[

]

=

"

Hello C++

"

;

pd

.

print

(

c

)

;

return

0

;

}

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


整数为: 5
浮点数为: 500.263
字符串为: Hello C++

Operator Overloading in C++

You can redefine or overload most of the built-in C++ operators. This allows you to use operators with custom types.

An overloaded operator is a function with a special name, formed by the keyword operator followed by the operator symbol being overloaded. Like other functions, an overloaded operator has a return type and a parameter list.


Box operator+(const Box&);

Declares the addition operator 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 shown below:


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

The following example uses a member function to demonstrate the concept of operator overloading. Here, the object is passed as a parameter, and the object's attributes are accessed using this the 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 to add 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

;

// declare Box1 of type Box

Box

Box2

;

// declare Box2 of type Box

Box

Box3

;

// declare Box3 of type Box

double

volume

=

0

.0

;

// store volume in this variable

// details of Box1

Box1

.

setLength

(

6

.0

)

;

Box1

.

setBreadth

(

7

.0

)

;

Box1

.

setHeight

(

5

.0

)

;

// details of Box2

Box2

.

setLength

(

12

.0

)

;

Box2

.

setBreadth

(

13

.0

)

;

Box2

.

setHeight

(

10

.0

)

;

// volume of Box1

volume

=

Box1

.

getVolume

(

)

;

cout

<<

"

Volume of Box1 :

"

<<

volume

<<

endl

;

// volume of Box2

volume

=

Box2

.

getVolume

(

)

;

cout

<<

"

Volume of Box2 :

"

<<

volume

<<

endl

;

// add the two objects to get Box3

Box3

=

Box1

Box2

;

// volume of Box3

volume

=

Box3

.

getVolume

(

)

;

cout

<<

"

Volume of Box3 :

"

<<

volume

<<

endl

;

return

0

;

}

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


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

Overloadable / Non-overloadable Operators

The following is a list of overloadable operators:

Binary arithmetic operators + (add), -(subtract), * (multiply), / (divide), % (modulo)
Relational operators == (equal to), != (not equal to),< (less than), > (greater than),<= (less than or equal to), >= (greater than or equal to)
Logical operators || (logical OR), && (logical AND), ! (logical NOT)
Unary operators + (unary plus), - (unary minus), * (pointer), & (address-of)
Increment and decrement operators ++ (increment), -- (decrement)
Bitwise operators | (bitwise OR), & (bitwise AND), ~ (bitwise NOT), ^ (bitwise XOR),<< (left shift), >>(right shift)
Assignment operators =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
Space allocation and deallocation new, delete, new[ ] , delete[]
Other operators () (function call), -> (member access), , (comma),

The following is a list of non-overloadable operators:

Operator Overloading Examples

The following provides various examples of operator overloading to help you better understand the concept of overloading.

No. Operators and Examples
1 Unary Operator Overloading
2 Binary Operator Overloading
3 Relational Operator Overloading
4 Input/Output Operator Overloading
5 ++ and -- Operator Overloading
6 Assignment Operator Overloading
7 Function Call Operator () Overloading
8 Subscript Operator [] Overloading
9 Class Member Access Operator -> Overloading