0% found this document useful (0 votes)
50 views

10.2 Operator Overloading

Operator overloading allows users to define their own behavior for operators like + and - when used with user-defined types. It is useful for tasks like adding integers, doubles, floats, or strings using + or performing vector addition by overloading + to add the x and y components of pairs of points. To overload an operator, use the operator keyword in the function signature. For example, to overload + for a Complex class, the function would be declared as Complex operator+(const Complex& addend).

Uploaded by

Divyanshu Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

10.2 Operator Overloading

Operator overloading allows users to define their own behavior for operators like + and - when used with user-defined types. It is useful for tasks like adding integers, doubles, floats, or strings using + or performing vector addition by overloading + to add the x and y components of pairs of points. To overload an operator, use the operator keyword in the function signature. For example, to overload + for a Complex class, the function would be declared as Complex operator+(const Complex& addend).

Uploaded by

Divyanshu Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Operator Overloading

classroom.udacity.com/nanodegrees/nd213/parts/f9fffe8e-1984-4045-92b6-
64854de4df2b/modules/70db33ed-8e7e-45e9-ab1d-3d0b257fc196/lessons/15cd39a7-3fda-495d-af24-
c5ccd45826a8/concepts/96e470f1-e48a-40da-9558-e8babf36b184

. In this exercise you'll see how to achieve polymorphism with operator overloading. You
can choose any operator from the ASCII table and give it your own set of rules!

Operator overloading can be useful for many things. Consider the + operator. We can
use it to add int s, double s, float s, or even std::string s.

In order to overload an operator, use the operator keyword in the function signature:

Complex operator+(const Complex& addend) {

Imagine vector addition. You might want to perform vector addition on a pair of points to
add their x and y components. The compiler won't recognize this type of operation on its
own, because this data is user defined. However, you can overload the + operator so it
performs the action that you want to implement.

Instructions
1. Define class Point .
2. Declare a prototype of overload method for + operator.
3. Confirm the tests pass.

1/1

You might also like