Question: Create a Vector class to represent two-dimensional vectors. Use the plus (+) operator to add them and display the result using the print() function to
Create a Vector class to represent two-dimensional vectors. Use the plus (+) operator to add them and display the result using the print() function to print the resultant vector in this format, Vector (a, b). You will need to use the following special methods to complete the objective: Define the __add__ method in your class to perform vector addition and then the plus operator would behave as per expectation. Define the __str__ method in your class to perform the print operation to return a string representation of the object. Example Code: v1 = Vector(5,12) v2 = Vector(7,-2) print(v1+v2) Output: Vector (12,10)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
