-1

I see multiple examples on how to add two variables of the same class, but I can't find examples on how to add variables of different classes with overloading.

We haven't worked with templates yet if it is relevant to my problem, but I am assuming we are not allowed to use it.

I want to add class book to class person with the + operator. ex. Person p; Book b; p+b;//adds a book like it would if it were using a function like p.addBook(b);

 class Person
{
    private:
        B * b;
        string sName;
        int numOfBook;
        int maxNumOfBooks;  

maxNumOfBooks is the max that person is allowed to carry

    public: 
        Person();
        Person(const Person&);
        ~Person();
        const Person operator = (const Person &)
        void addBook(const Book &); //adds a book to a Person
        Person operator+(Person );
        Book getBook(){return b[numOfBook];}//returns index of book
};

Person::Person()
{
    numOfBook = 0;
    maxNumOfBooks = 10;
    b = new B[maxNumOfBooks];
    for(int x=0;x<maxNumOfBooks;x++)
    {

sets name to "". I did overload this.

        b[x] = "";  
    }
}

Person::Person(const Person& p)
{
    numOfBook = p.numOfBook;
    maxNumOfBooks = p.maxNumOfBooks;
    b = new B[maxNumOfBooks];
    for(int x=0;x<maxNumOfBooks;x++)
    {
        b[x] = p.b[x];
    }
}
Person::~Person()
{
    delete [] b;
}

void Person::addBook()
{

replaces "" if numOfBooks < maxNumOfBooks;

Did code this.

}

Person Person::operator+(Person obj)
{

not entirely sure what to do here. but I am assuming it must look something like this.

    Spell a;
    ++numOfBook;
    a=this->getBook(numOfBook);
    obj.addBook(a);
    return *this;
}

 const Person Person::operator = (const Person & obj)
 {
      delete [] b;
    numOfBook = p.numOfBook;
    maxNumOfBooks = p.maxNumOfBooks;
    b = new B[maxNumOfBooks];
    for(int x=0;x<maxNumOfBooks;x++)
    {
        b[x] = p.b[x];
    }
   return *this;

 }

class Book
{
    private:
        string name;
        int price;
    public: 
        void setP();
        int getP();
};


int main()
{
    Person s;
    Book bk("Story", 18);

    s+bk;  

I want to add a book at Person, but it should also make use of the addBook() function.

}
ArcheAngel
  • 35
  • 1
  • 3
  • 7
  • You are returning `Person` objects by value. So where are your assignment operator and copy constructor for the `Person` object? Those two functions are mandatory for your `operator +` to work correctly, and you totally missed writing them. Please read up on the [rule of 3](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – PaulMcKenzie Sep 12 '16 at 04:31
  • @PaulMcKenzie, I can add it if it is needed for my question. It is in my original code. – ArcheAngel Sep 12 '16 at 04:33
  • 1
    So you decide later to add functions now that are absolutely essential for your `operator +` to work correctly? Those functions should have been in your original post, not as an afterthought. – PaulMcKenzie Sep 12 '16 at 04:34
  • @PaulMcKenzie I added the missing code. – ArcheAngel Sep 12 '16 at 04:45
  • 2
    `operator +` should be returning a brand new `Person` object, not a reference to the original object. An `operator +=` would return a reference to the original object. In addition, your assignment operator is flawed in that it does not check for self-assignment (in addition to other flaws). – PaulMcKenzie Sep 12 '16 at 04:50

1 Answers1

1

When you overload the + operator, you return a new instance of the return type (often this is overloaded for arithmetic type where the two inputs and output are the same type, but that's not at all necessary).

You just need to figure out what the operator is supposed to do. In this case, the + operator should add a book to a copy of the person object called on (i.e. return a new instance and not modify the origional).

Person Person::operator + (const Book& b){
  Person tmp(*this); //create a copy of this Person to modify
  tmp.addBook(b);    //modify this new copy and not the original
  return tmp;        //return the modified copy
}

whereas the += operator will modify the current object and return a reference

Person& Person::operator += (const Book& b){
  addBook(b);
  return *this;
}

If you already have += defined you can change your + operator to:

Person Person::operator + (const Book& b){
  return Person(*this) += b;
}

Note:

Person P;
Book B;
P + B;  // P is not modified;

Is not equivalant to:

Person P;
Book B;
P.addBook(B);  // P is modified

However the following is.

Person P;
Book B;
P += B;  // P is modified

As well as

Person P;
Book B;
P = P + B; // P is modified because it is assigned (P + B)
Timothy Murphy
  • 1,322
  • 8
  • 16
  • Also, once `operator +=` is in place, `operator +` is simply `return Person(*this) += b;` – PaulMcKenzie Sep 12 '16 at 05:06
  • @PaulMcKenzie, Good Point. – Timothy Murphy Sep 12 '16 at 05:10
  • @Timothy Murphy, thank you I will look at it and start coding. I also apologize for not adding necessary code as I wasn't thinking. I posted too quickly without making sure that I added all the essential code. – ArcheAngel Sep 12 '16 at 05:31
  • @ArcheAngel If you think my answer fully solved your problem, you should accept it. If not, let me know what problems you're still encountering, so I or someone else can help you with it. – Timothy Murphy Sep 12 '16 at 23:35