Hey guys! I hope what I'm about to do is not frowned upon :)
I just would like some input/critics/heads up on my class definition and implementation I did for my project. I am by no means a trained programmer and I doubt my coding style is very good so I thought it might be a good idea to get some inputs on something I put some time and effort in to create with my best knowledge.
I created a class PNVector that allows to me to create an N-sized vector on which I overloaded several operations. Apart from an assessment I also have one question. As you can see I overloaded * and / so I can multiply/divide the vector by a scalar. Now naturally only PNVector*double works but double*PNVector does not. Is the only solution to make this work to overload the binary * and / respectively?
For any answer/input I'm thankful!
Definitions here:
#ifndef RK4_project_PNVector_hpp
#define RK4_project_PNVector_hpp
#include <vector>
class PNVector {
public:
PNVector(int N){
m_vector = new std::vector<double>(N);
for (unsigned int i = 0; i<m_vector->size(); ++i) {
m_vector->at(i) = 0;
}
}
~PNVector(){
delete m_vector;
}
/* copy constructor */
PNVector(const PNVector &vSource)
{
m_vector = new std::vector<double>(vSource.m_vector->size());
for (unsigned int i = 0; i<vSource.m_vector->size(); ++i) {
m_vector->at(i) = vSource.m_vector->at(i);
}
}
PNVector operator+(const PNVector &rhs);
PNVector operator+(const double h);
PNVector operator*(const double h);
PNVector operator/(const double h);
PNVector operator=(const PNVector &other);
double &operator[](unsigned int index);
PNVector operator*(PNVector &rhs);
void Print();
private:
std::vector<double> *m_vector;
protected:
};
#endif
Implementations here:
#include <iostream>
#include "PNVector.hpp"
PNVector PNVector::operator+(const PNVector &rhs) {
PNVector vector(*this);
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
vector.m_vector->at(i) += rhs.m_vector->at(i);
}
return vector;
}
PNVector PNVector::operator+(const double h) {
PNVector vector(*this);
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
vector.m_vector->at(i) += h;
}
return vector;
}
double &PNVector::operator[](unsigned int index) {
return this->m_vector->at(index);
}
PNVector PNVector::operator*(const double h) {
PNVector vector(*this);
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
vector.m_vector->at(i) *= h;
}
return vector;
}
// Inner product
PNVector PNVector::operator*(PNVector &rhs) {
PNVector vector(*this);
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
vector.m_vector->at(i) *= this->m_vector->at(i);
}
return vector;
}
PNVector PNVector::operator/(double h) {
PNVector vector(*this);
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
vector.m_vector->at(i) /= h;
}
return vector;
}
PNVector PNVector::operator=(const PNVector &other) {
if (this!= &other) {
std::vector<double>* new_m_vector = new std::vector<double>(other.m_vector->size());
for (unsigned int i = 0; i<other.m_vector->size(); ++i) {
new_m_vector->at(i) = other.m_vector->at(i);
}
delete m_vector;
m_vector = new_m_vector;
}
return *this;
}
void PNVector::Print() {
for (unsigned int i = 0; i<this->m_vector->size(); ++i) {
std::cout << m_vector->at(i) << std::endl;
}
}