I'm very new to c++ and have issues doing what seems like a simple task. I was given a main function to test and I have to create a class that correctly corresponds to the int main. Below is the class I've made so far. There's a lot more to it than what I've provided because I'm having issues starting off the basic parts of the class.
The task is to create a class that makes object lists of ints and can be manipulated with other functions that come later. For now, I am trying to set up the lists and inserting int n values.
#include<iostream>
#include<string>
#include<cmath>
#define DEFAULT 8
#define EMPTY -1
using namespace std;
// Your class specification goes here <<<<<<<<<<<<<<
class Myclass
{
private:
int *a = NULL;
int end = EMPTY;
int size;
public:
Myclass(){} //default constructor
Myclass(Myclass const &other);//copy constructor
Myclass(Myclass &&other);//move constructor
~Myclass() {delete [] a;} //destructor
Myclass& operator=(const Myclass& other); //copy assignment
Myclass& operator=(Myclass&& other); //move assignment
Myclass& operator+=(const Myclass &other);
Myclass& operator+(Myclassconst &other); // overload +
friend ostream& operator<<(ostream &os, const Myclass& other);
};
this in my class so far and below is the definitions.
Myclass::Myclass(Myclass const &other) // copy constructor
{
end = other.end;
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
a[i] = other.a[i];
}
Myclass::Myclass(Myclass &&other) //move constructor
{
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
a[i] = other.a[i];
}
Myclass& Myclass::operator=(const Myclass& other) //overload =
{
if (this == &other) // Check for self assignment
return *this;
end = other.end;
delete [] a;
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
a[i] = other.a[i];
return *this;
}
Myclass& Myclass::operator=(Myclass&& other)
{
if(this != &other)
{
delete[] a;
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
{
a[i] = other.a[i];
}
}
return *this;
}
Myclass& Myclass::operator+(Myclass const &other) //overload +
{
Myclass answer;
int index = 0;
for (int i = 0; i < this->end; i++)
answer.a[index++] = this->a[i];
for (int i = 0; i <= other.end; i++)
answer.a[index++] = other.a[i];
return answer;
}
Myclass& Myclass::operator+=(const Myclass&other)
{
Myclass answer;
int index = 0;
for (int i = 0; i < this->end; i++)
answer.a[index++] = this->a[i];
return answer;
}
ostream& operator<<(ostream &os, const Myclass& other)
{
for (int i = 0; i <= other.end; i++)
os << other.a[i] << ", ";
return os;
}
My main issue is getting the += operator to work correctly as I keep getting the error about adding Myclass and an int together. My += function does not seem correct either but im not sure how to even correct it. I've commented out a2 and a3 because I'm not sure how to deal with the true or false of these objects either. Please help in any way or send me in a direction of information Id really appreciate it.