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})")
}
- Define a
Point
class that takes x
and y
coordinates as parameters. - Overload the
plus
operator within the Point
class, allowing the addition of two Point
objects by implementing the plus
function. - 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. - In the
main
function, create two Point
objects, point1
with coordinates (3, 4) and point2
with coordinates (1, 2). - Add these two
Point
objects together using the +
operator, storing the result in the sum
variable. - Print the sum of the points by accessing the
x
and y
coordinates of the sum
object.