Computer Science, asked by akshithaakkuanu26323, 4 months ago

what is operator overloading​

Answers

Answered by sadhanaumesh87
1

Answer:

hdhdhxkslsjdhdjskxjfjdjdnffj

Answered by jadhavdivya785
3

In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.

Rationale Edit

Operator overloading is syntactic sugar, and is used because it allows programming using notation nearer to the target domain[1] and allows user-defined types a similar level of syntactic support as types built into a language. It is common, for example, in scientific computing, where it allows computing representations of mathematical objects to be manipulated with the same syntax as on paper.

Operator overloading does not change the expressive power of a language (with functions), as it can be emulated using function calls. For example, consider variables a, b and c of some user-defined type, such as matrices:

a + b * c

In a language that supports operator overloading, and with the usual assumption that the '*' operator has higher precedence than the '+' operator, this is a concise way of writing:

Add(a, Multiply(b, c))

However, the former syntax reflects common mathematical usage.

Examples Edit

In this case, the addition operator is overloaded to allow addition on a user-defined type Time in C++:

Time operator+(const Time& lhs, const Time& rhs) {

Time temp = lhs;

temp.seconds += rhs.seconds;

temp.minutes += temp.seconds / 60;

temp.seconds %= 60;

temp.minutes += rhs.minutes;

temp.hours += temp.minutes / 60;

temp.minutes %= 60;

temp.hours += rhs.hours;

return temp;

}

Addition is a binary operation, which means it has two operands. In C++, the arguments being passed are the operands, and the temp object is the returned value.

The operation could also be defined as a class method, replacing lhs by the hidden this argument; However, this forces the left operand to be of type Time:

// This "const" means that |this| is not modified.

// \

// ------------------------------------\

// |

// V

Time Time::operator+(const Time& rhs) const {

Time temp = *this; // |this| should not be modified, so make a copy.

temp.seconds += rhs.seconds;

temp.minutes += temp.seconds / 60;

temp.seconds %= 60;

temp.minutes += rhs.minutes;

temp.hours += temp.minutes / 60;

temp.minutes %= 60;

temp.hours += rhs.hours;

return temp;

}

Note that a unary operator defined as a class method would receive no apparent argument (it only works from this):

bool Time::operator!() const {

return hours == 0 && minutes == 0 && seconds == 0;

}

The less-than(<) operator is often overloaded to sort a structure or class:

class Pair {

public:

bool operator<(const Pair& p) const {

if (x_ == p.x_) {

return y_ < p.y_;

}

return x_ < p.x_;

}

private:

int x_;

int y_;

};

Like with the previous examples, in the last example operator overloading is done within the class. In C++, after overloading the less-than operator (<), standard sorting functions can be used to sort some classes.

Similar questions