# סדנא תרגיל 3 שאלה 2

### תרגיל בנושא בנאים איך כותבים בנאי רגיל ובנאי העתקה תרגיל מאוד חשוב ובסיסי בכל הנושא של קונסטרקטורים

תכנית ראשית

```/******************************

targil: 3/2
lecturer: david Cohen
discription: operators on vectors

*******************************/

#include <iostream>
using namespace std;
#include "Vector.h"

enum options
{
//		0	1:=	   2:==	3:* 4:+ 5	6
};

int main()
{

Vector  v1(10),v2(10),v3;
int*pointer1=v1.get_data();        // keep the adress of first place in the array
int*pointer2=v2.get_data();

for(int i=1;i<=4;i++)
{
int*y=v1.get_data();

v1.insert(i);
v2.insert(i+4);
}

v1.set_data(pointer1);       // piont to the firsr data array adress
v2.set_data(pointer2);

int choice,val;
cout<<"enter your choice 0-6\n"; cin>>choice;
while(choice)
{
switch(choice)
{
case assignment: ; v3=v1;

break;
case isEqual:	if(v1==v2) cout<<"v1==v2\n"; else cout<<"v1!=v2\n";
break;
case mult:		cout<<"v1*v2="<<v1*v2<<endl;
break;

break;
case clear:		v1.clear();
break;
case delLast:	v2.delLast();
break;
default: cout<<"ERROR";
}
cout<<endl;
v1.print();	cout<<endl;	;v2.print();	cout<<endl;	v3.print();
cout<<endl<<"enter your choice 0-6\n"; cin>>choice;
}
//system ("pause");
return 0;
}
/**********output***************
enter your choice 0-6
2
v1!=v2

capacity: 10 size: 4 val: 1 2 3 4

capacity: 10 size: 4 val: 5 6 7 8

capacity: 10 size: 0 val:

enter your choice 0-6
1

capacity: 10 size: 4 val: 1 2 3 4

capacity: 10 size: 4 val: 5 6 7 8

capacity: 10 size: 4 val: 1 2 3 4

enter your choice 0-6
3
v1*v2=70

capacity: 10 size: 4 val: 1 2 3 4

capacity: 10 size: 4 val: 5 6 7 8

capacity: 10 size: 4 val: 1 2 3 4

enter your choice 0-6
4

capacity: 10 size: 4 val: 1 2 3 4

capacity: 10 size: 4 val: 5 6 7 8

capacity: 10 size: 8 val: 1 2 3 4 5 6 7 8

enter your choice 0-6
5

capacity: 10 size: 0 val:

capacity: 10 size: 4 val: 5 6 7 8

capacity: 10 size: 8 val: 1 2 3 4 5 6 7 8

enter your choice 0-6
6

capacity: 10 size: 0 val:

capacity: 10 size: 3 val: 5 6 7

capacity: 10 size: 8 val: 1 2 3 4 5 6 7 8

enter your choice 0-6
2
v1!=v2

capacity: 10 size: 0 val:

capacity: 10 size: 3 val: 5 6 7

capacity: 10 size: 8 val: 1 2 3 4 5 6 7 8

enter your choice 0-6
0
******************************/

```

מימוש פונקציות המחלקה

```#include <iostream>
#include "Vector.h"

using namespace std;

Vector::Vector() // defolt c-tor
{
capacity=10;
data=new int[capacity];
size=0;

}

Vector::Vector(int capacity)
{
this->capacity=capacity;
data=new int[capacity];
size=0;
}

Vector::Vector(const Vector&Temp)        //copy c-tor
{

this->capacity=Temp.capacity;
this->size=Temp.size;
this->data=new int [capacity];
for (int i = 0; i < size; i++)
{
data[i]=Temp.data[i];
}

}
Vector Vector::operator=(Vector a)
{
data= new int[a.capacity];
size=a.size;
for (int i = 0; i < a.size; i++)
{
data[i]=a.data[i];   // copy the element, now the value is same same.
}
return *this;

}
/**********set/get function******************/
int Vector::get_capacity()
{
return capacity;

}

int Vector::get_size()
{
return size;

}
int* Vector::get_data()
{
return data;

}
void Vector::set_data(int* p)
{
data=p;

}

Vector::~Vector(void)
{
if (data)
{
delete[]data;
}

}

void Vector::print()
{

cout<<"capacity: "<<capacity<<" size: "<<this->size<<" val: ";
for (int i = 0; i < size; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}

void Vector::insert(int i)
{

*data=i;                 // insert the value
data++;                  // promote the pointer (for the next insert)
size++;                 // num of element in the array is now ++

}

bool Vector::operator==(Vector a)
{
int count=0;
if (size==a.get_size())    // if the arrays size are even
{
for (int i = 0; i < size; i++)
{
if (data[i]==a.get_data()[i])
{
count++;     // count the element that is even
}
}
if (count==size)    // if all the element is even
{
return true;
}
return false;
}
return false;
}

int Vector::operator*(Vector a)
{
int result=0;

if (size==a.get_size())     // can't multiplicate vectors that are not even (in size)
{

for (int i = 0; i < size; i++)
{
result+=data[i]*a.get_data()[i];   // skalar multiplicate
}

}
else
{
cout<<"ERROR\n";    // can't multiplicate vectors that are not even (in size)
}
return result;

}

int& Vector::operator[](int a)
{
if (a<=size)
{
return data[a];
}
cout<<"ERROR\n";

}

Vector Vector::operator+(Vector a)
{

Vector A;
int *result=new int[capacity];

for (int i = 0; i < size; i++)
{
A.data[i]=data[i];           // copy the exsist vector in the begining .....
A.data[i+size]=a.data[i];    //... add the new vector after the first one
}

A.size=(a.size)*2;         // update the array size

return A;
}

void Vector::clear()
{
for (int i = 0; i < size; i++)
{
data[i]=0;   // delete the value

}
size=0;

}

void Vector::delLast()
{
int count=0;
for (int i = 0; i < size; i++)
{
if(data[i]==0)
count++;

}
if (count==size)
{
cout<<"ERROR\n";
}
else
{
data[size]=0;     // delete ta last one
size--;
}

}

```

המחלקה

```

#pragma once

class Vector
{
private:
int*data;
int capacity;
int size;
public:

Vector();  // float is just to distinguish between the constructors
Vector(int);
Vector(const Vector&temp);

int get_capacity();
int get_size();
void insert(int);

int*get_data();
void set_data(int*);
void print();
// operators
Vector operator=(Vector a);

void clear();
void delLast();
bool operator==(Vector a);
int operator*(Vector a);
Vector operator+(Vector a);

int& operator[](int a);

~Vector(void);

};

```