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.
}