Operator overloading assignment

My assignment is to be able to add, sub, mult and div 2 vectors etc

However, I dont know how to do it with arrays.

The main should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
Vector vec1(20, 2.0) ;     	// creates a 20-element vector, initializing every one of its elements to 2.0
Vector vec2(20,-4.0) ;     	// creates a 20-element vector, initializing every one of its elements to -4.0

Vector vec3 = vec1 + vec2 ;  	// creates vec3 and set it equal to the summation of vec1 and vec2 
vec3 = vec1 - vec2 ;   	   	// vec3 := the difference between vec1 and vec2 
vec3 = vec1 * vec2 ; 	   	// vec3 := the cross product of vec1 and vec2
vec3 = vec1 ^ vec2 ;         	// vec3 := the dot product of vec1 and vec2
vec3.scale(8);	 		// vector scaling; every element in vec3 is multiplied by 8 

vec3(0) = 9;			// sets to 9 the first element of vec3
vec3.reset(0);			// sets every element of vec3 to 0


This what I have done so far. And I am stuck on using operator+ function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83



#include<iostream>
#include<iomanip>

using namespace std;

class Vector{
	
public:
	
	Vector(const int=0,float=0.0);
	void print ()const;
	Vector operator+(Vector);
	~Vector();
protected:
	const int vectorSize;
	float elementValue;
	float *vectorArray;
	
	};


Vector::Vector(const int vSize,float eVal):vectorSize(vSize),elementValue(eVal)
{
	vectorArray=new float[vectorSize];

	for(int i=0;i<vectorSize;++i)
	{
	 vectorArray[i]=elementValue;

	}

}

//NEED HELP BELOW!!!!!!!! 

Vector Vector::operator+(Vector add)  //1
{
	Vector sum;
	for(int i=0;i<vectorSize;++i)
	{
	sum.vectorArray[i]=vectorArray[i]+add.vectorArray[i]; //2
	
	}
	return sum; //3
	
}

void Vector::print ()const
{
cout<<fixed<<setprecision(1)<<"The vector is: [";
for(int i=0;i<vectorSize;++i)
	{
		cout<<" "<<vectorArray[i];
	 	}
cout<<" ]"<<endl;


}

Vector::~Vector()
{

	
delete []vectorArray;
delete []sum;

}

int main()
{
	
	Vector vec1(20,2.0); //20 is the array size and the value of the every elements is 2
	vec1.print(); //output  [2.0, 2.0, ... ,2.0]
	Vector vec2(20,-4.0);
	vec2.print(); //output  [-4.0, -4.0, ... ,-4.0]
	Vector vec3=vec1+vec2;



}
Last edited on
On line 41 you create an empty vector. Before you can add both vectors you need to check whether both vector sizes are equal. Then you need to create Vector sum(vectorSize);.

Remove line 68.

Thanks for the reply,

I don't know if I follow but I add this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Implementation
bool Vector::check(Vector& size)
{
 return(vectorSize==size.vectorSize);	
	
}


Vector Vector::operator+(Vector& add)
{
	Vector sum(vectorSize);
	for(int i=0;i<vectorSize;++i)
	{
	sum.vectorArray[i]=vectorArray[i]+add.vectorArray[i];
	
	}

return sum;
	
	}

//in main
cout<<(vec1.check(vec2)?"vectorSize are equal":"vectorSize are not equal");



When I run it, I encountered another problem, I have to abort the program.

It gives me an error; it says something like Debug Assertion Failed, BLOCK_TYPE IS VALID(pHead->nBlockUse)

Oh man, got it. I did not know about the rule of three. Thank you very much.
Topic archived. No new replies allowed.