Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````// ZoomBA
// define Point
def Point : {
\$\$ : def(x=0,y=0){  \$.x = x ; \$.y = y },
\$add : def (o) { new ( Point , \$.x + o.x , \$.y + o.y ) },
\$str : def(){ str('(%s,%s)', \$.x, \$.y)}
}

a = new ( Point, 1,2)
b = new ( Point, 3,4)
c = a + b
println(c)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````class Point{
private:
int x, y;
public:
Point(int xval, int yval):x(xval),y(yval){}
Point operator+(const Point& other)
{
this->x += other.x;
this->y += other.y;
return *this;
}
void print()
{
std::cout << "x: " << x << "\n";
std::cout << "y: " << y << "\n";
}

};

int main()
{
Point p1(1,2);
Point p2(3,4);
Point p4(4,5);
Point p3 = p1+p2+p4;
p3.print();
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

:: - steephen
====
Your + operator is *mutable*. It should never be, definitely NO for a arithmetic type.
What you did is this:
wikipedia.org/wiki/Immutable_object

Comment hidden because of low score. Click to expand.
0
of 0 vote

Found a Ruby implementation:

``````class Point
attr_accessor :x, :y

def initialize(x,y)
@x, @y = x, y
end

def +(other)
Point.new(@x + other.x, @y + other.y)
end

def to_s
"(#{@x}, #{@y})"
end
end``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.