Question
Need help with this question please! I have included all relevant source code for this question. You do not need to modify this code, I
Need help with this question please! I have included all relevant source code for this question. You do not need to modify this code, I have included it to avoid confusion and maybe help implement the Array class. Will UPVOTE
Create a new collection class called Array. We will move the responsibility for managing the elements of the book array away from the Library class and into the new collection class.
1- Create a new Array class. You will need both a header file and a source file for this class. You can refer to the course material (section 2.1) and the coding examples done in class to see what belongs in the header file and what belongs in the source file.
- The class will contain two data members:
a data member called elements, which is an array of Book objects
a data member called size, which is the current number of books in the array
**Since the book array is moving from the Library class, you will also move the constant array size definition (MAX_ARR_SIZE) into the correct location.
*Write the following functions for the Array class:
- a constructor that initializes the data member(s) that require initialization
an add(Book&) function that adds the given book parameter to the back of the book array
a print() function that prints out all the books in the array to the screen Note: These functions will be identical to the ones you wrote for the Library class .
Source code:
/Book.h
#ifndef BOOK_H
#define BOOK_H
#include
using namespace std;
class Book
{
public:
Book(int=0, string="Unknown", string="Unknown", int=0);
void setBook(int, string, string, int);
void print();
private:
int id;
string title;
string author;
int year;
};
#endif
//end of Book.h
// Book.cpp
#include "Book.h"
#include
#include
using namespace std;
Book::Book(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
void Book::setBook(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
void Book::print()
{
cout << setw(3) << id
<<" Title: " << setw(40) << title
<<"; Author: " << setw(20) << author
<<"; Year: " << year << endl;
}
//end of Book.cpp
// Library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "Book.h"
#define MAX_ARR_SIZE 20
class Library{
private:
Book books[MAX_ARR_SIZE];
int numOfBooks;
public:
Library();
void addBook(Book &);
void print();
};
#endif
//end of Library.h
// Library.cpp
#include "Library.h"
#include
#include
using namespace std;
Library::Library()
{
numOfBooks=0;
}
void Library::addBook(Book &book)
{
if(numOfBooks < MAX_ARR_SIZE)
{
books[numOfBooks] = book;
numOfBooks++;
}else
cout<<" Library full"<
}
void Library::print()
{
if(numOfBooks == 0)
cout<<" Library is empty"<
else
{
cout<<" Books in the library : "<
for(int i=0;i
books[i].print();
}
}
//end of Library.cpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started