0% found this document useful (0 votes)
6 views1 page

Vector

The document contains the implementation of a Vector class in C++, which includes constructors, destructors, and overloaded operators for vector operations. It provides functionalities such as memory allocation, copying, accessing elements with both zero-based and one-based indexing, and performing arithmetic operations like addition, subtraction, and scalar multiplication. Additionally, it includes a method to calculate the norm of the vector.

Uploaded by

ikaif0312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Vector

The document contains the implementation of a Vector class in C++, which includes constructors, destructors, and overloaded operators for vector operations. It provides functionalities such as memory allocation, copying, accessing elements with both zero-based and one-based indexing, and performing arithmetic operations like addition, subtraction, and scalar multiplication. Additionally, it includes a method to calculate the norm of the vector.

Uploaded by

ikaif0312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <cmath>#include <iostream>#include <cassert>#include "Vector.

hpp"//
Constructor for vector of a given size// Allocates memory, and initialises
entries// to zeroVector::Vector(int size){assert(size > 0);mSize = size;mData = new
double [mSize];for (int i=0; i<mSize; i++){mData[i] = 0.0;}}// Overridden copy
constructor// Allocates memory for new vector, and copies// entries of other vector
into itVector::Vector(const Vector& otherVector){mSize =
otherVector.GetSize();mData = new double [mSize];for (int i=0; i<mSize; i++)
{mData[i] = otherVector.mData[i];}}// Overridden destructor to correctly free
memoryVector::~Vector() {delete[] mData;}// Method to get the size of a vectorint
Vector::GetSize() const{return mSize;}// Overloading square brackets// Note that
this uses ‘zero-based’ indexing,// and a check on the validity of the
indexdouble& Vector::operator[](int i){assert(i > -1);assert(i < mSize);return
mData[i];}// Read-only variant of []// Note that this uses ‘zero-based’
indexing,// and a check on the validity of the indexdouble Vector::Read(int i)
const{assert(i > -1);assert(i < mSize);return mData[i];}// Overloading round
brackets// Note that this uses ‘one-based’ indexing,// and a check on the
validity of the indexdouble& Vector::operator()(int i){assert(i > 0);assert(i <
mSize+1);return mData[i-1];}// Overloading the assignment operatorVector&
Vector::operator=(const Vector& otherVector){assert(mSize == otherVector.mSize);for
(int i=0; i<mSize; i++){mData[i] = otherVector.mData[i];}return *this;}//
Overloading the unary + operatorVector Vector::operator+() const{Vector
v(mSize);for (int i=0; i<mSize; i++){v[i] = mData[i];}return v;}// Overloading the
unary - operatorVector Vector::operator-() const{Vector v(mSize);for (int i=0;
i<mSize; i++){v[i] = -mData[i];}return v;}// Overloading the binary +
operatorVector Vector::operator+(const Vector& v1) const{assert(mSize ==
v1.mSize);Vector v(mSize);for (int i=0; i<mSize; i++){v[i] = mData[i] +
v1.mData[i];}return v;}// Overloading the binary - operatorVector Vector::operator-
(const Vector& v1) const{assert(mSize == v1.mSize);Vector v(mSize);for (int i=0;
i<mSize; i++){v[i] = mData[i] - v1.mData[i];}return v;}// Overloading scalar
multiplicationVector Vector::operator*(double a) const{Vector v(mSize);for (int
i=0; i<mSize; i++){v[i] = a*mData[i];}return v;}// Method to calculate norm (with
default value p=2)// corresponding to the Euclidean normdouble
Vector::CalculateNorm(int p) const{double norm_val, sum = 0.0;for (int i=0;
i<mSize; i++){sum += pow(fabs(mData[i]), p);}norm_val = pow(sum, 1.0/((double)
(p)));return norm_val;}// MATLAB style friend to get the size of a vectorint
length(const Vector& v){return v.mSize;}

You might also like