Skip to content

Commit 3d7b51d

Browse files
Update Operators Topic
1 parent f1856dc commit 3d7b51d

9 files changed

Lines changed: 141 additions & 9 deletions

File tree

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```C
2-
// TODO
2+
// No Native Support.
3+
// Use named functions instead.
4+
5+
typedef struct Vector2
6+
{
7+
float x;
8+
float y;
9+
} Vector2;
10+
11+
Vector2 add(Vector2 first, Vector2 second)
12+
{
13+
return (Vector2) {
14+
.x = first.x + second.x,
15+
.y = first.y + second.y
16+
};
17+
}
318
```
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Cpp
2-
// TODO
2+
class Vector2
3+
{
4+
public:
5+
float x;
6+
float y;
7+
8+
Vector2 operator+(Vector2 const& other) const
9+
{
10+
return Vector2 { x + other.x, y + other.y };
11+
}
12+
13+
bool operator==(Vector2 const& other) const
14+
{
15+
return x == other.x && y == other.y;
16+
}
17+
};
18+
19+
Vector2 result = first + second;
320
```
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Cs
2-
// TODO
2+
public struct Vector2
3+
{
4+
public float X { get; }
5+
public float Y { get; }
6+
7+
public Vector2(float x, float y)
8+
{
9+
X = x;
10+
Y = y;
11+
}
12+
13+
public static Vector2 operator +(Vector2 first, Vector2 second)
14+
{
15+
return new Vector2(first.X + second.X, first.Y + second.Y);
16+
}
17+
}
18+
19+
Vector2 result = first + second;
320
```
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```Go
2-
// TODO
2+
// No Native Support.
3+
// Use named methods or functions instead.
4+
5+
type Vector2 struct {
6+
X float64
7+
Y float64
8+
}
9+
10+
func (first Vector2) Add(second Vector2) Vector2 {
11+
return Vector2{
12+
X: first.X + second.X,
13+
Y: first.Y + second.Y,
14+
}
15+
}
16+
17+
result := first.Add(second)
318
```
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```Java
2-
// TODO
2+
// No Native Support.
3+
// Use named methods instead.
4+
5+
record Vector2(float x, float y) {
6+
public Vector2 add(Vector2 other) {
7+
return new Vector2(x + other.x, y + other.y);
8+
}
9+
}
10+
11+
Vector2 result = first.add(second);
312
```
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
```Javascript
2-
// TODO
2+
// No Native Support for custom operator overloading.
3+
// Use named methods instead.
4+
5+
class Vector2 {
6+
constructor(x, y) {
7+
this.x = x;
8+
this.y = y;
9+
}
10+
11+
add(other) {
12+
return new Vector2(this.x + other.x, this.y + other.y);
13+
}
14+
}
15+
16+
const result = first.add(second);
317
```
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
```Python
2-
# TODO
2+
class Vector2:
3+
def __init__(self, x: float, y: float) -> None:
4+
self.x = x
5+
self.y = y
6+
7+
def __add__(self, other: "Vector2") -> "Vector2":
8+
return Vector2(self.x + other.x, self.y + other.y)
9+
10+
def __eq__(self, other: object) -> bool:
11+
if not isinstance(other, Vector2):
12+
return False
13+
return self.x == other.x and self.y == other.y
14+
15+
result = first + second
316
```
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
```Rust
2-
// TODO
2+
use std::ops::Add;
3+
4+
struct Vector2 {
5+
x: f32,
6+
y: f32,
7+
}
8+
9+
impl Add for Vector2 {
10+
type Output = Vector2;
11+
12+
fn add(self, other: Vector2) -> Vector2 {
13+
Vector2 {
14+
x: self.x + other.x,
15+
y: self.y + other.y,
16+
}
17+
}
18+
}
19+
20+
let result = first + second;
321
```
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
```Typescript
2-
// TODO
2+
// No Native Support for custom operator overloading.
3+
// Use named methods instead.
4+
5+
class Vector2 {
6+
public constructor(
7+
public readonly x: number,
8+
public readonly y: number,
9+
) {}
10+
11+
public add(other: Vector2): Vector2 {
12+
return new Vector2(this.x + other.x, this.y + other.y);
13+
}
14+
}
15+
16+
const result = first.add(second);
317
```

0 commit comments

Comments
 (0)