In my code there's operator+ overloading. In this scope, I define object ans, which I want to build and return, but it seems that the destructor distructs ans before I can return it, so this method returns some undefined value.
I don't understand where I am wrong? Is it the destructor, the builder, or in my overload of the operator+?
Here is my code:
class vector1{
    int * arr;
int size;
public:
//constructors
vector1(){}
vector1(int n){
    size=n;
    arr= new int[size];
}
//functions
int get_size(){return size;}
void init(){  //initialize all array cells to 0
    for(int i=0;i<size;i++)
        arr[i]=0;
}
int get_value_in_index(int index){
    return arr[index];
}
void set_value_in_index(int i, int num){
    arr[i]=num;
}
int & operator[](int i){
    int default_val=0;
    if (i<0 || i>size){
        cout<<"index not valid"<<endl;
        return default_val;
    }
    return arr[i];
}
vector1 operator+(vector1 & ob);
//destructor
~vector1(){
    delete [] arr;
}
};
vector1 vector1:: operator+(vector1 & ob){
vector1 ans(size);
if (ob.get_size()!=size){  //if the arrays are not the same size return array of     '0', this array size
    cout<<"vectors not the same length. can't sum them"<<endl;
    //test
    //exit(1);
    ans.init();
}
else{
    for (int i=0;i<size;i++)
        ans.set_value_in_index(i,arr[i]+ob.get_value_in_index(i));
}
return ans;
}
Thanks for your time and help.
 
     
     
     
    