Question

In: Computer Science

Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...

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.

Solutions

Expert Solution

# 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 :

  • For overloading operators, we use the "operator" keyword and define it as a normal function.

    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 :

  • The definition of the Vector Vector::operator + (Vector addMe) and Vector Vector::operator - (Vector subtractMe) are same as the definitions of the functions Vector Vector::AddVector(Vector addMe) and Vector Vector::SubtractVector(Vector subtractMe) respectively.
  • Function void Vector::operator = (Vector v)  is used for setting the value of x and y of the first vector before assignment operator (=) with the value of x and y of the second vector after assignment operator.
  • Function ostream & operator <<(ostream &out, Vector &v) is used for making an object of ostream class with the data of x and y values of the vector which calls it and then returning that object for printing in the provided format.

# 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 :

  • First, we have created two vectors v and q using zero and two parameters constructor respectively.
  • Then we have set values of the X and Y in the vector v and displayed the vector v.
  • Then we have created vector a by assigning it the vector q and displayed it.
  • Then we created vectors b and c by addition and subtraction of vectors v and q respectively and then displayed them.
  • Finally, we displayed the magnitude of the b vector.  

Output :

​​​​​​​


Related Solutions

Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT