Question

In: Computer Science

C++ Please read the question carefully and match the output example given at the end! Question...

C++

Please read the question carefully and match the output example given at the end!

Question

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 (Vector.h) contains only the declaration of the class found on the final page. Vector.cpp implements 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)
  
#include <iostream>
using namespace std;

class Vector { public: /* Default constructor, sets x and y to zero */
Vector(); /* Parameterized constructor, sets x to Xval, and y to yVal */
Vector(float xVal, float yVal);
/* Returns the floating point values of the x and y */
float getX() const;
float getY() const;
/* * Inserts into the stream the string "(<x>, <y>)" where   * <x> is the floating point value for x
* <y> is the floating point value for y
*/ friend ostream& operator<<(ostream& os, const Vector &v);
/*   * Sums two Vectors v1 and v2 together returning a new vector v3
* Where addition is defined as   *   v3.x = v1.x + v2.x
*   v3.y = v1.y + v2.y
*/ Vector operator+(const Vector &other) const;
/* * Subtracts v2 from v1 returning a new vector v3
* Where subtraction is defined as   *   v3.x = v1.x - v2.x
*   v3.y = v1.y - v2.y
*/ Vector operator-(const Vector &other) const;
private: float x, y; };

Solutions

Expert Solution

Editable code:

Main.CPP:

#include<iostream>

#include "Vector.h"

using namespace std;

int main()

{

    

     double x, y;

     cout << "Enter Vector 1 x and y value";

     cin >> x>>y;

     Vector v1(x, y);

     cout << "Enter Vector 2 x and y value";

     cin >> x>> y;

     Vector v2(x, y);

     cout << "\nVector v1 :"<<v1;

     cout << "\nVector v2 :"<<v2;

     cout << "\nv1+v2 :"<<v1 + v2;

     cout << "\nv1-v2 :" << v1 - v2;

     cout << "\n\n";

     system("pause");

     return 0;

}

Vector.CPP:

#include "Vector.h"

#include <cmath>

Vector::Vector()

{

     x = 0.0;

     y = 0.0;

}

Vector::Vector(float xVal, float yVal)

{

     x = xVal;

     y = yVal;

}

float Vector::getX() const

{

     return x;

}

float Vector::getY() const

{

     return y;

}

Vector Vector::operator+(const Vector &v) const

{

     return Vector(x + v.x, y + v.y);

}

Vector Vector::operator-(const Vector &v) const

{

     return Vector(x - v.x, y - v.y);

}

ostream& operator<<(ostream& os, const Vector &v)

{

     os << v.getX()<<" ";

     os << v.getY();

     return os;

}

Vector.h:

#pragma once

#include <iostream>

using namespace std;

class Vector {

public: /* Default constructor, sets x and y to zero */

     Vector(); /* Parameterized constructor, sets x to Xval, and y to yVal */

     Vector(float xVal, float yVal);

     /* Returns the floating point values of the x and y */

     float getX() const;

     float getY() const;

     /* * Inserts into the stream the string "(<x>,< y>)" where   * <x> is the floating point value for x

     * <y> is the floating point value for y

     */ friend ostream& operator<<(ostream& os, const Vector &v);

     /*   * Sums two Vectors v1 and v2 together returning a new vector v3

     * Where addition is defined as   *   v3.x = v1.x + v2.x

     *   v3.y = v1.y + v2.y

     */ Vector operator+(const Vector &other) const;

     /* * Subtracts v2 from v1 returning a new vector v3

     * Where subtraction is defined as   *   v3.x = v1.x - v2.x

     *   v3.y = v1.y - v2.y

     */ Vector operator-(const Vector &other) const;

private: float x, y;

};


Related Solutions

C++ Please read the question carefully and make sure that the function prototypes given are used...
C++ Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part. The output example is provided at the end of the question. First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array. Part 1: Write a function...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
Please read the question carefully and relate the situation to the type of antagonist with reasoning...
Please read the question carefully and relate the situation to the type of antagonist with reasoning During anaphylaxis, massive quantities of histamine are released, causing vasodilation that leads to bronchoconstriction. The emergency treatment for anaphylaxis is adrenaline, which causes vasoconstriction that leads to bronchodilation. What type of antagonist/antagonism does this represent? (Physiological, pharmacokinetic, chemical, irreversible, competitive). The thing that confuses me is how to come to the conclusion of what the drug is doing? I'm wondering does adrenaline react with...
Case Study Analysis Read carefully the following case/scenario and answer the questions given at the end....
Case Study Analysis Read carefully the following case/scenario and answer the questions given at the end. A manufacturing company, involved in the business of food processing, faces a technical problem at one of their major plants. Recently they faced a technical issue which resulted in loss of production and was fixed by engaging their mechanical staff. Now this technical problem can result in even bigger loss of production and if it gains attention of public through social or electronic media,...
In python! please be thorough and read question carefully :) if possible, give explanation for code...
In python! please be thorough and read question carefully :) if possible, give explanation for code We’re going to create a class which stores information about product ratings on a website such as Amazon. The ratings are all numbers between 1 and 5 and represent a number of “stars” that the customer gives to the product. We will store this information as a list of integers which is maintained inside the class StarRatings. You will be asked to write the...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
c++ Please read the instructions carefully. You must put the proper items in the proper file...
c++ Please read the instructions carefully. You must put the proper items in the proper file (eitehr SSandwich.h or SSandwich.cpp. Do NOT include any main function in your submission. You are encouraged to write your own main function to test what you are submitting. You are submit two files via BlackBoard: A file named SSandwich.h, a header file defining the class SSandwich. No inline methods are permitted. Any enum classes should be defined here also. Your header file should have...
I paid to be able to post question here. Please read carefully and give quality answer....
I paid to be able to post question here. Please read carefully and give quality answer. there is nothing to clarify. just do what is required. Do not mess up my question as there are limited. Thanks. Choose at least two concepts from the following: equity method, fair value method, partnership, corporation, bankruptcy chapter 7, bankruptcy chapter 11, subsidiary, parent company, consolidation of financial statements, trust, estates, liquidation, partnerships termination, estates tax, acquisition, variable interest entity, EPS, income tax, reorganization....
Please read the question carefully and then provide the answer thanks. Discussion Topic Choose a TCP/IP...
Please read the question carefully and then provide the answer thanks. Discussion Topic Choose a TCP/IP service, such as a web browser, email, file transfer, remote shell, DHCP, DNS, network time protocol, network address translation, etc. Important! Everyone should pick a different service, if possible, to keep the discussion fresh. In your original post, answer the following about the service you have chosen: How would you determine the IP addresses of your devices on your network? Success Hints You can...
Please read through the article below and answer the question at the end of the article....
Please read through the article below and answer the question at the end of the article. Strategy as a Wicked Problem Over the past 15 years, I’ve been studying how companies create strategy—the most important responsibility of senior executives. Many corporations, I find, have replaced the annual top-down planning ritual, based on macroeconomic forecasts, with more sophisticated processes. They crunch vast amounts of consumer data, hold planning sessions frequently, and use techniques such as competency modeling and real-options analysis to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT