Lab 9 C++

Header File (vector.h)

#ifndef VECTOR_H
#define VECTOR_H

using std::string;

struct MathVector{
  // data members, default public
  long x=0;
  long y=0;

  // 2 constructors
  // =default uses default values of data members (above).
  // no other work required. You're welcome!
  // you must write
  MathVector(long, long);

  // 4 function members you must write
  MathVector add(const MathVector&);
  MathVector mult(long);
  long mult(const MathVector&);
  double magnitude();

// a function! You must write
string vec_to_str(const MathVector&);


Vector Functions File (vector.cpp)

using std::string;
using std::ostringstream;

#include "vector.h"

// Constructor for values
MathVector::MathVector(long x_value, long y_value){
	x = x_value;
	y = y_value;

// Add Vector (overload the add)
MathVector MathVector::add(const MathVector & other_vec){

// Multiply Vector by scalars (overload multiply)
MathVector MathVector::mult(long scalar){
	return MathVector(x*scalar,y*scalar);

// Multiply Vector by another Vector
long MathVector::mult(const MathVector & vec){
	return x+y;

// Magnitude of a vector using Pythagorean Theorem
double MathVector::magnitude(){

// prints out the vector
string vec_to_str(const MathVector& v){
	ostringstream oss;
	oss << v.x << ":" << v.y;
	return oss.str();

Let’s now have a main file to run

Main File (main.cpp)

using std::string;
using std::endl;
using std::cout;

#include "lab09_vector.h"

int main(){

    MathVector v1;
    MathVector v2(3,2);
    MathVector v3(10,12);
    MathVector v4(9,2);
    MathVector v5(18,7);

    long a = 3;

    // Using the addition function to add two vectors
    v1 = v3.add(v2);
    cout << vec_to_str(v1) << endl;

    // Multiply Vector by a scaler

    v1 = v5.mult(a);

    cout << "Multiply by scalar output: " << vec_to_str(v1) << endl;

    cout <<vec_to_str(v3) << endl;

    // Multiply Vector by another Vector
    cout << v2.mult(v3) << endl;
    // Magnitude
    cout << v2.magnitude() << endl;

    // Vector to String
    cout << vec_to_str(v3) << endl;


Output from the main.cpp File

Running the add vector functions 
Running the Multiply by scalar vector functions 
Multiply by scalar output: 54:21
Running the Multiply by andother Vector Function
using the magnitude function !!!