Complete the code to overload the + operator for the Vector struct.
struct Vector {
var x: Int
var y: Int
static func + (lhs: Vector, rhs: Vector) -> Vector {
return Vector(x: lhs.x [1] rhs.x, y: lhs.y + rhs.y)
}
}The + operator is overloaded by defining a static function named + that takes two Vectors and returns their sum.
Complete the code to overload the == operator to compare two Vector instances.
struct Vector {
var x: Int
var y: Int
static func == (lhs: Vector, rhs: Vector) -> Bool {
return lhs.x [1] rhs.x && lhs.y == rhs.y
}
}The == operator checks if both x and y values of two Vectors are equal.
Fix the error in the operator overloading function to multiply a Vector by an integer scalar.
struct Vector {
var x: Int
var y: Int
static func * (vector: Vector, scalar: Int) -> Vector {
return Vector(x: vector.x [1] scalar, y: vector.y * scalar)
}
}The * operator multiplies each coordinate of the Vector by the scalar value.
Fill both blanks to overload the - operator to subtract two Vector instances.
struct Vector {
var x: Int
var y: Int
static func - (lhs: Vector, rhs: Vector) -> Vector {
return Vector(x: lhs.x [1] rhs.x, y: lhs.y [2] rhs.y)
}
}The - operator subtracts the x and y values of rhs from lhs to get the difference Vector.
Fill all three blanks to overload the += operator for Vector addition and assignment.
struct Vector {
var x: Int
var y: Int
static func += (lhs: inout Vector, rhs: Vector) {
lhs.x [1]= rhs.x
lhs.y [2]= rhs.y
}
static func + (lhs: Vector, rhs: Vector) -> Vector {
return Vector(x: lhs.x [3] rhs.x, y: lhs.y + rhs.y)
}
}The += operator adds rhs's x and y to lhs's x and y respectively. The + operator returns a new Vector with summed coordinates.