I need to call a swap function in a class. I don't know how to come up with the swap function. Here's how I'm calling swap function:
void
CLibrary::Sort(CBook* Books, int size)
{
int minIdx;
string minVal;
Books = new CBook[size];
for (int i = 0; i < (size - 1); ++i)
{
minVal = Books[i].GetTitle();
minIdx = i;
for (int k = i + 1; k < size; ++k)
{
if (Books[k].GetTitle() < minVal)
{
minIdx = k;
minVal = Books[k].GetTitle();
}
}
Books[i].Swap(Books[minIdx]);
}
}
And I need to come up with this function:
void
CBook::Swap(CBook& Book)
{
}
Any suggestions will be greatly appreciated!
CBook.h is as follows.
class CBook
{
private:
string mTitle;
string mAuthor;
int mYearPublished;
public:
CBook();
CBook(string Title, string Author, int YearPublished);
~CBook();
string GetTitle() const;
string GetAuthor() const;
int GetYearPublished() const;
void SetBook(string Title, string Author, int YearPublished);
void SetTitle(string Title);
void SetAuthor(string Author);
void SetYearPublished(int YearPublished);
void Swap(CBook &Book);
};
There's also CLibrary class where I'm calling the swap function and where it reads in the input file. Readbooks() function is as follows.
void
CLibrary::ReadBooks()
{
ifstream infile;
string title;
string author;
int yearPublished;
infile.open(mInFileName);
string s;
getline(infile, s);
mNumBooks = stoi(s);
CBook Book;
mBooks = new CBook[mNumBooks];
while (!infile.eof())
{
for (int i = 0; i < mNumBooks; ++i)
{
getline(infile, title);
getline(infile, author);
getline(infile, s);
yearPublished = stoi(s);
mBooks[i].SetBook(title, author, yearPublished);
}
infile.close();
break;
}
}
CBook.CBookclass declaration, i.e.CBook.h. No one knows what to swap if we don't see the members.