Challenge: Implement opEquals Function for a Class

Let’s try a coding challenge using the opEquals function.

We'll cover the following

Problem statement

Start with the following class, which represents colored points:

enum Color { blue, green, red }
class Point {
    int x;
    int y;
    Color color;
    this(int x, int y, Color color) { 
        this.x = x;
        this.y = y;
        this.color = color; 
    }
}

Input

Implement opEquals for this class in a way that ignores colors. When implemented in that way, the following writeln statement should display true:

// Different colors
auto bluePoint = new Point(1, 2, Color.blue); 
auto greenPoint = new Point(1, 2, Color.green);
// They are still equal
writeln(bluePoint == greenPoint);

Output

true

Challenge

This problem is designed for you to practice, so try to solve it on your own first. If you get stuck, you can always refer to the explanation and solution provided in the next lesson. Good luck!

Get hands-on with 1200+ tech skills courses.