17.(28 pts) Book with Pages (Parts a -c). Let\'s say we need to design two class
ID: 3730218 • Letter: 1
Question
17.(28 pts) Book with Pages (Parts a -c). Let's say we need to design two classes, one named Page and one named Book. Each Page consists of an integer page number and no more than 500 English characters. The sequence of characters on a Page is always terminated by the null character. A Book consists of a title with no more than 50 characters (terminated by a null character) and no more than 650 Pages. (10 pts) Declare the Page class based on the description above. There are many operations that we could define for a Page, but we are only going to look at a subset for this problem. All attributes should be private. The class should declare the following member operations/functions only: a. i. fi. (2 pts) A constructor with one parameter for setting the page number (2 pts) A const function called getPageNumber () for getting the page number ifi. (2 pts) A const function called getIsCurrentPage for getting the status of the page, i.e if it is the current page being read (true) or not (false (2 pts) A function called printPage () for displaying each character on the page to the screen (should not be done by overloading the stream insertion operator). This function should not accept any arguments and should not return a value. iv. class Page public: // Should contain four prototypes/declarations retum misCane Poge Nunber P t Page Number( ) in ge retum Roge Number private: I/ Should contain three attributes- one has been provided below bool mlsCurrentPage;/ true if this page is the current one being viewed; false otherwise /1 1 pt / data member 2 data members required! int poge Number char CortentExplanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
// Class Page definition
class Page
{
// Private data member to store data
private:
int pageNo;
char *pageContent;
bool mlsCurrentPage;
public:
// Default constructor definition
Page()
{
// Sets the page number to zero
pageNo = 0;
// Dynamically allocates memory for 500 characters for page contents
pageContent = new char[500];
// Sets page visited status to false
mlsCurrentPage = false;
}// End of default constructor
// Parameterized constructor
// Receives page number and page contents as parameter
Page(int pg, char *con)
{
// Dynamically allocates memory for 500 characters for page contents
pageContent = new char[500];
int i;
// Sets the page number
pageNo = pg;
// Sets page visited status to false
mlsCurrentPage = false;
// Loops till length of the contents parameter and assigns it to data member page contents
for(i = 0; i < strlen(con); i++)
pageContent[i] = con[i];
// Assigns null character
pageContent[i] = '';
}// End of parameterized constructor
// Function set the page number
void setPageNo(int no)
{
pageNo = no;
}// End of function
// Returns the page number
int getPageNo()
{
return pageNo;
}// End of function
// Function set the page contents
void setPageContents(char con[])
{
pageContent = con;
}// End of function
// Function to return current page visited status
bool isCurrentPage()
{
return mlsCurrentPage;
}// End of function
// Function set the page status
void setCurrentPage()
{
mlsCurrentPage = true;
}// End of function
// Function to print the page contents
void printPage()
{
cout<<" Contents ";
// Loops till length of the page contents
for(int c = 0; c < strlen(pageContent); c++)
{
// Checks if number of characters is 70 then display the character and newline
if(c == 70)
cout<<pageContent[c]<<endl;
// Otherwise less than 70 character, display only the character
else
cout<<pageContent[c];
}// End of loop
}// End of function
};// End of class
// Class Book definition
class Book
{
// Private data member to store title
char *title;
// Creates an pointer for Page class using deligation method
Page *pages;
// Counter for number of pages in book
int counter;
public:
// Default constructor definition
Book()
{
// Initializes page counter to zero
counter = 0;
// Dynamically allocates memory for 50 characters for book title
title = new char[50];
// Dynamically allocates memory for 650 for number of pages
pages = new Page[650];
}// End of default constructor
// Parameterized constructor
// Receives book title as parameter
Book(char *ti)
{
// Dynamically allocates memory for 650 for number of pages
pages = new Page[650];
// Initializes page counter to zero
counter = 0;
int i;
// Dynamically allocates memory for 50 characters for book title
title = new char[50];
// Loops till length of the book title and assigns it to data member title
for(i = 0; i < strlen(ti); i++)
title[i] = ti[i];
// Assigns null character
title[i+1] = '';
}// End of parameterized constructor
// Function to add page
// Receives page object as reference
void addPage(Page &pg)
{
// Assigns page object to pages counter position
pages[counter] = pg;
// Increase the counter by one
counter++;
}// End of function
// Function to return book title
char *getBookTitle()
{
return title;
}// End of function
// Function to display book information
void printBook()
{
// Displays book title
cout<<" Book Title: "<<title;
// Loops till number of pages in the book
for(int c = 0; c < counter; c++)
{
// Displays page number
cout<<" Page "<<(c + 1);
// Calls the function to display the page contents
pages[c].printPage();
// Set the page contents to visited
pages[c].setCurrentPage();
}// End of for loop
}// End of function
// Function to page number visited status
bool isCurrentPage(int no)
{
return pages[no].isCurrentPage();
}// End of function
};// End of class
// main function definition
int main()
{
// Creates page contents
char *data = (char *)"This is a demo to check the page contents. Page contents cannot exceed more than 500 characters. Each line must display 70 characters only.";
char *data1 = (char *)"You can check the page visit status. True for page visited. False for page not visited.";
char *data2 = (char *)"This is another page contents for book 2. This page limit is also 500 characters.";
// Creates book title
char *title1 = (char*)"Traveling Salesman";
char *title2 = (char*)"My Success";
// Creates page using parameterized constructor
Page p1(1, data);
Page p2(2, data1);
Page p3(3, data2);
cout<<" Book 1 ";
// Creates book using parameterized constructor
Book b1(title1);
// Calls the function add a page to a book
b1.addPage(p1);
b1.addPage(p2);
// Checks book specified page status and displays information
if(b1.isCurrentPage(0))
cout<<" Page 1 is current page: TRUE";
else
cout<<" Page 1 is current page: FALSE";
// Calls the function to print book information
b1.printBook();
// Checks book specified page status and displays information
if(b1.isCurrentPage(1))
cout<<" Page 2 is current page: TRUE";
else
cout<<" Page 2 is current page: FALSE";
cout<<" Book 2 ";
// Creates book using parameterized constructor
Book b2(title2);
b2.addPage(p1);
b2.addPage(p3);
// Checks book specified page status and displays information
if(b2.isCurrentPage(0))
cout<<" Page 1 is current page: TRUE";
else
cout<<" Page 1 is current page: FALSE";
// Calls the function to print book information
b2.printBook();
// Checks book specified page status and displays information
if(b2.isCurrentPage(1))
cout<<" Page 2 is current page: TRUE";
else
cout<<" Page 2 is current page: FALSE";
}// End of main function
Sample Output:
Book 1
Page 1 is current page: FALSE
Book Title: Traveling Salesman
Page 1 Contents
This is a demo to check the page contents. Page contents cannot exceed
more than 500 characters. Each line must display 70 characters only.
Page 2 Contents
You can check the page visit status. True for page visited. False for p
age not visited.
Page 2 is current page: TRUE Book 2
Page 1 is current page: FALSE
Book Title: My Success
Page 1 Contents
This is a demo to check the page contents. Page contents cannot exceed
more than 500 characters. Each line must display 70 characters only.
Page 2 Contents
This is another page contents for book 2. This page limit is also 500 c
haracters.
Page 2 is current page: TRUE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.