class, operator overloading, ex. on vector

Simple example of vector class. The present example illustrates how to use the operator overloading and the vector class is used here as an example. A programmer who wish to use a vector class should make a more complete vector class or use one of the many scientific libraries for vectors and matrices.

Main.cpp

//============================================================================
// Name        : Vector.cpp
// Author      : chd
// Version     :
// Copyright   :
// Description : Operator overloading in vector class,
//				C++ Ansi-style
//============================================================================


#include "Vector.h"

int main() {

	int n=10;

	double* v=new double [n];

	for (int i = 0; i < n; ++i) {
		v[i]=2;
	}

	Vector v1(n,v);
	Vector v2=v1;
	v2=v1;
	cout<<v2;
	cout<<dotProduct(v1,v2);

}



Vector.h

/*
 * Vector.h
 *
 *  Created on: Mar 19, 2016
 *      Author: christos
 */

#include <math.h>
#include <iostream>
using namespace std;

#ifndef VECTOR_H_
#define VECTOR_H_

class Vector{
private:

	int size;
	double* vector;

public:
	//constructor
	Vector(int,double*);
	Vector (int = 0,double a=0);
	//copy constructor T(const T&);
	Vector(const Vector&);
	//destructor
	~Vector(){
		delete[] vector;
	}


	//member function to return vector length
	int length() const{
		return size;
	}

	//member operator
	//T& operator$(const T&)
	Vector& operator=(const Vector&);
	Vector& operator+=(const Vector&);
	Vector& operator-=(const Vector&);

	double twonorm() const;

	//friend binary operator
	//friend T operator$(const T&,const T&)
	friend Vector operator+(const Vector&,const Vector&);
	friend Vector operator-(const Vector&,const Vector&);

	friend Vector operator*(double,const Vector&);

	friend Vector operator*(const Vector&, const Vector&);

	friend double dotProduct(const Vector&, const Vector&);

	friend ostream& operator<<(ostream&, const Vector&);







};



#endif /* VECTOR_H_ */



Vector.cpp


/*
 * Vector.cpp
 *
 *  Created on: Mar 19, 2016
 *      Author: christos
 */


#include "Vector.h"


Vector::Vector(int n,double* vec){
	vector=new double [size=n];
	for (int i = 0; i < size; ++i) {
		vector[i]=vec[i];
	}

}

Vector::Vector(int n, double a){

	vector=new double[size=n];
	for (int i = 0; i < size; ++i) {
		vector[i]=a;
	}

}

Vector::Vector(const Vector& vec){
	vector=new double [size=vec.size];
	for (int i = 0; i < size; ++i) {

		vector[i]=vec.vector[i];
	}

}

Vector& Vector::operator =(const Vector& vec){

	for (int i = 0; i < vec.size; ++i) {
		vector[i]=vec.vector[i];
	}
	return *this;
}

Vector& Vector::operator+=(const Vector& vec){

	for (int i = 0; i < vec.size; ++i) {
			vector[i]+=vec.vector[i];
		}
		return *this;
}

Vector& Vector::operator-=(const Vector& vec){

	for (int i = 0; i < vec.size; ++i) {
			vector[i]-=vec.vector[i];
		}
		return *this;
}

double Vector::twonorm() const{

	double norm=vector[0]*vector[0];
	for (int i = 0; i < size; ++i) {
		norm+=vector[i]*vector[i];
	}

	return sqrt(norm);

}

Vector operator+(const Vector& vec1,const Vector& vec2){

	Vector res=vec1;
	res+=vec2;
	return res;

}

Vector operator-(const Vector& vec1,const Vector& vec2){

	Vector res=vec1;
	res-=vec2;
	return res;

}

Vector operator*(double a, const Vector& vec){
//calling constructor to create vector object
Vector res(vec.size);
for (int i = 0; i < res.size; ++i) {
	res.vector[i]=a*vec.vector[i];
}
return res;

}

/*Vector operator*(const Vector& v1, const Vector& v2){

	int n=v1.size;
	Vector res(n);



}*/

double dotProduct(const Vector& v1, const Vector& v2){

	int n=v1.size;
	double res=0;
	for (int i = 0; i < n; ++i) {
		res+=v1.vector[i]*v2.vector[i];
	}
	return res;
}


ostream& operator<<(ostream& c, const Vector& v){

	for (int i = 0; i < v.size; ++i) {
		c<<"|"<<v.vector[i]<<"|";
	}

	return c;
}