Starting from:
$30

$24

Lab 15 Solution




Objectives:







1. Implement operator overloading.







Question 1:







In this project, you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (<<), addition (+), and subtraction (-).




insertion (<<):




The insertion operator is used to send the values of a vector to an ostream object, if a vector has two values 2.0 and 3.1 it must insert into the string “(2.0, 3.1)”. For example, code Segment A will produce the Output A




Segment A




Vector v(2.2, 3.1)




cout << “Vector v1: “ << v << endl;




Output A




Vector v1: (2.2, 3.1)




addition (+):




The addition operator is used to sum two vectors together. Addition is pairwise, x is added to x, and y to y. The result of the operation is a new object that contains the sum of the two Vectors as a single vector. Code given in Segment B produces the Output B Segment B

Vector v1(2.0, 3.1), v2(4.3, 5.0);




cout << “v1 + v2: “ << v1 + v2 << endl;




Output B




v1 + v2: (6.3, 8.1)

subtraction(-):




The subtraction operator is used to reduce one vector by another. Subtraction is pairwise, x is reduced x, and y by y. The result of the operation is a new object that contains the difference of the two Vectors as a single vector. Code given in Segment C produces the Output C




Segment C




Vector v1(2.0, 3.1), v2(4.3, 5.0);




cout << “v1 - v2: “ << v1 - v2 << endl;




Output B




v1 - v2: (-2.3, -1.9)










Your task is to create a program divided into three files: main.cpp, Vector.cpp, and Vector.h. The header file and the driver program are given to you. You need to write Vector.cpp to implement the methods and operators of the Vector. The program, main.cpp uses the Vector class to produce the program output below. Note, there are two vectors supplied by the user.




Output D




Enter the space separated x and y values for the first vector (v1): 23.1 15.3




Vector v1: (23.1, 15.3)




Enter the space separated x and y values for the second vector (v2): 1.7 2.3




Vector v2: (1.7, 2.3)




v1 + v2: (24.8, 17.6)




v1 - v2: (21.4, 13)

More products