In: Computer Science
Programming II: C++ - Programming Assignment
Vector Overloads
Overview
In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object.
When completing this assignment, the student should demonstrate mastery of the following concepts:
· Object-oriented Paradigm
· Operator Overloading - Internal
· Operator Overloading - External
· Mathematical Modeling
Assignment
In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector.
Use the following code that declares a vector class as a starting point:
// CLASS SPECIFICATION
#ifndef __VECTOR__
#define __VECTOR__
#include <iostream>
#include <math.h>
using namespace std;
class Vector {
private:
// DATA MEMBERS
double _v_x;
double _v_y;
double _magnitude;
// HELPER METHODS
void CalculateMagnitude();
public:
// CONSTRUCTOR(S)
Vector();
Vector(double _v_x, double _v_y);
// SETTERS
void SetVX(double _v_x);
void SetVY(double _v_y);
// GETTERS
double GetVX();
double GetVY();
double GetMagnitude();
// OPERATIONS
Vector AddVector(Vector addMe);
Vector SubtractVector(Vector subtractMe);
// DISPLAY METHODS
void Display();
};
#endif
// CLASS METHOD IMPLEMENTATIONS
#include "Vector.h"
// helper methods
void Vector::CalculateMagnitude() {
this->_magnitude = sqrt( pow(abs(this->_v_x), 2) + pow(abs(this->_v_y), 2) );
}
// constructors
Vector::Vector() {
this->_v_x = 0.0;
this->_v_y = 0.0;
CalculateMagnitude();
}
Vector::Vector(double _v_x, double _v_y) {
this->_v_x = _v_x;
this->_v_y = _v_y;
CalculateMagnitude();
}
// setters
void Vector::SetVX(double _v_x) {
this->_v_x = _v_x;
CalculateMagnitude();
}
void Vector::SetVY(double _v_y) {
this->_v_y = _v_y;
CalculateMagnitude();
}
// getters
double Vector::GetVX() {
return this->_v_x;
}
double Vector::GetVY() {
return this->_v_y;
}
double Vector::GetMagnitude() {
return this->_magnitude;
}
// operations
Vector Vector::AddVector(Vector addMe) {
// create a temp vector
Vector returnMe;
// add corresponding vector components
returnMe.SetVX(this->_v_x + addMe.GetVX());
returnMe.SetVY(this->_v_y + addMe.GetVY());
// return the tempVector
return returnMe;
}
Vector Vector::SubtractVector(Vector subtractMe) {
// create a tempVector
Vector returnMe;
// subtract corresponding vector components
returnMe.SetVX(this->_v_x - subtractMe.GetVX());
returnMe.SetVY(this->_v_y - subtractMe.GetVY());
// return the tempVector
return returnMe;
}
// display methods
void Vector::Display() {
cout << "<" << this->_v_x << ", " << this->_v_y << ">";
}
_____________________
Rewrite the Vector class so that it properly utilizes overloaded operators to perform basic operations. Specifically, overload the +, -, =, and << operators. The = operator should be overloaded to allow the user to make a direct assignment to an already existing vector. The following code should work after the overload: Vector myFirstVector(4, 5); Vector mySecondVector; mySecondVector = myFirstVector; The + and - operators should be overloaded to allow the user to intuitively add and subtract two vectors and store the result with the overloaded = operator. The following code should work after the overload:
resultVector = myFirstVector + mySecondVector; resultVector = myFirstVector - mySecondVector; The << operator should be overloaded to cause the vector to neatly display on the screen using vector notation. Use the format illustrated in the example and mimic those results when writing this operator overload. The following code would cause the vector to display on the screen followed by a newline: cout << myFirstVector << endl;
After you have written your class, write a driver that demonstrates each of the overloaded operations. Declare some Vectors and put arbitrary data in them. Make assignments, add the vectors, subtract them, and display them on the screen with cout.
# Vector.h :
// CLASS SPECIFICATION
#ifndef __VECTOR__
#define __VECTOR__
#include <iostream>
#include <math.h>
using namespace std;
class Vector {
private:
// DATA MEMBERS
double _v_x;
double _v_y;
double _magnitude;
// HELPER METHODS
void CalculateMagnitude();
public:
// CONSTRUCTOR(S)
Vector();
Vector(double _v_x, double _v_y);
// SETTERS
void SetVX(double _v_x);
void SetVY(double _v_y);
// GETTERS
double GetVX();
double GetVY();
double GetMagnitude();
// OPERATIONS
Vector operator + (Vector addMe);
Vector operator - (Vector subtractMe);
void operator = (Vector v);
// DISPLAY METHODS
friend ostream & operator << (ostream &out, Vector &v);
};
#endif
Explanation :
Vector operator + (Vector addMe);
Vector operator - (Vector subtractMe);
void operator = (Vector v);
For overloading the "<<" operator we have used the
"friend" function.
friend ostream & operator << (ostream &out,
Vector &v);
"<<" is a private member variable of the ostream class. So, to access "<<" we have to define the "friend" function.
We have changed function declaration of functions Vector AddVector(Vector addMe) and Vector SubtractVector(Vector subtractMe) to Vector operator + (Vector addMe) and Vector operator - (Vector subtractMe) respectively for adding and subtracting vectors.
We have also changed function declaration of the function void Display() to friend ostream & operator << (ostream &out, Vector &v) for displaying vector.
void operator = (Vector v) is declared for assigning one vector to another one.
# Vector.cpp :
// CLASS METHOD IMPLEMENTATIONS
#include "Vector.h"
// helper methods
void Vector::CalculateMagnitude() {
this->_magnitude = sqrt( pow(abs(this->_v_x), 2) + pow(abs(this->_v_y), 2) );
}
// constructors
Vector::Vector() {
this->_v_x = 0.0;
this->_v_y = 0.0;
CalculateMagnitude();
}
Vector::Vector(double _v_x, double _v_y) {
this->_v_x = _v_x;
this->_v_y = _v_y;
CalculateMagnitude();
}
// setters
void Vector::SetVX(double _v_x) {
this->_v_x = _v_x;
CalculateMagnitude();
}
void Vector::SetVY(double _v_y) {
this->_v_y = _v_y;
CalculateMagnitude();
}
// getters
double Vector::GetVX() {
return this->_v_x;
}
double Vector::GetVY() {
return this->_v_y;
}
double Vector::GetMagnitude() {
return this->_magnitude;
}
// operations
void Vector::operator = (Vector v){
// set corresponding vector components
this->SetVX(v.GetVX());
this->SetVY(v.GetVY());
}
Vector Vector::operator + (Vector addMe) {
// create a temp vector
Vector returnMe;
// add corresponding vector components
returnMe.SetVX(this->_v_x + addMe.GetVX());
returnMe.SetVY(this->_v_y + addMe.GetVY());
// return the tempVector
return returnMe;
}
Vector Vector::operator - (Vector subtractMe) {
// create a tempVector
Vector returnMe;
// subtract corresponding vector components
returnMe.SetVX(this->_v_x - subtractMe.GetVX());
returnMe.SetVY(this->_v_y - subtractMe.GetVY());
// return the tempVector
return returnMe;
}
// display methods
ostream & operator <<(ostream &out, Vector &v) {
out << "<" << v._v_x<< ", " << v._v_y << ">";
return out;
}
Explanation :
# Main.cpp :
#include<iostream>
#include "Vector.h"
#include<conio.h>
int main(){
// Creating Vector v by default constructor (zero parameter)
Vector v;
// Creating Vector q by parameterized constructor (two parameters)
Vector q(1.2,1.4);
// Setting the values of X and Y in Vector v
v.SetVX(1.0);
v.SetVY(2.0);
// Displaying the X and Y values of Vector v
cout<<"v : ";
cout << v << endl;
// Assigning the value of Vector q to Vector a
Vector a = q;
// Displaying the X and Y values of Vector a
cout<<"a : ";
cout << a << endl;
// Assigning the value of addition of Vectors v and q to Vector b
Vector b = v + q;
// Displaying the X and Y values of Vector b
cout<<"b : ";
cout << b << endl;
// Assigning the value of subtraction of Vectors v and q vector to Vector c
Vector c = v - q;
// Displaying the X and Y values of Vector c
cout<<"c : ";
cout << c << endl;
// Displaying magnitude of Vector b
cout << b.GetMagnitude();
return 0;
}
Explanation :
Output :