operator overloading kotlin

class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }
}

fun main() {
    val point1 = Point(3, 4)
    val point2 = Point(1, 2)
    val sum = point1 + point2
    println("Sum of points: (${sum.x}, ${sum.y})")
}
  1. Define a Point class that takes x and y coordinates as parameters.
  2. Overload the plus operator within the Point class, allowing the addition of two Point objects by implementing the plus function.
  3. The plus function takes another Point object as a parameter and returns a new Point object with the sum of the respective x and y coordinates.
  4. In the main function, create two Point objects, point1 with coordinates (3, 4) and point2 with coordinates (1, 2).
  5. Add these two Point objects together using the + operator, storing the result in the sum variable.
  6. Print the sum of the points by accessing the x and y coordinates of the sum object.