#ifndef _BOOK_H_ #define _BOOK_H_ // These two lines of preprocessor directives,
ID: 3737487 • Letter: #
Question
#ifndef _BOOK_H_
#define _BOOK_H_
// These two lines of preprocessor directives, and the one at the end #endif,
// prevent the header file from being included (linked in) multiple times,
// when it is used multiple times by the user.
#include "Buy.h"
#include <string>
using namespace std;
class Book {
private:
string title, author; // private local variables
float price;
public:
Book(string,string,float); // constructor
Buy *buys; // linked list of buy
// accessor methods
string getTitle();
string getAuthor();
float getPrice();
void setTitle(string);
void setAuthor(string);
void setPrice(float);
// class methods
void buyBook(string date);
string displayBought();
int timesBought();
string toString();
};
#endif // _BOOK_H_
#include "Book.h"
#include <string>
#include <iostream>
// Q1 : CLASS METHODS Part 1 : Constructor, Accessor, and Set Methods for Book (5 points)
// Constructor
// Create a constructor for the class Book which takes 2 string parameters and one float parameter.
// Use the parameters to initialize the three private local variables title, author, and price.
// HINT: Don't forget to initialize your linked list of buys to NULL.
// code here
// Accessor and Set Methods
// Create accessor and set methods for private local strings title and author and also float price.
// code here
// Q2 : CLASS METHODS Part 2 : Class Methods for Book (10 points)
// Create a method called "toString"
// This method has no input parameters and returns the string containing the title, author, and price of a book.
// This function is used in print_all function inside main to print the information of a book.
// The exact format can be found in the sample output of assignment description.
// code here
// Create a method named "buyBook" which has one string parameter date and no return type (see helper function for use).
// This method is used to add a new date to the book's linked list of buys. The string parameter is the date of buying.
// You should add the date to the tail of the linked list "buys". Buys will be added in chronological order.
// code here
// Create a method named "timesBought"
// This method has no input parameters and returns the number of times the book has been bought.
// This function is used in print_all function inside the main function.
// code here
// Create a method named "displayBought" which has no parameters and returns a string (see print_all function for use).
// This method will be used to return a string for the dates where the book has been bought.
// If the book was not bought, return an empty string.
// code here]
I have no clue what I have to do in order to get constructors, methods, accessors, and things like the questions ask... Pleas help.
Explanation / Answer
The method definition is given below:
In question 2 the last 3 methods cannot be defined without the Buy.h structure. I have defined those at the last ;
hope this helps. Thanks.
#include "Book.h"
#include <string>
#include <iostream>
// Q1 : CLASS METHODS Part 1 : Constructor, Accessor, and Set Methods for Book (5 points)
// Constructor
// Create a constructor for the class Book which takes 2 string parameters and one float parameter.
// Use the parameters to initialize the three private local variables title, author, and price.
// HINT: Don't forget to initialize your linked list of buys to NULL.
// code here
Book::Book(string t,string a,float p)
{
title =t;
author = a;
price = p;
buys=NULL;
}
// Accessor and Set Methods
// Create accessor and set methods for private local strings title and author and also float price.
// code here
string Book::getAuthor()
{
return author;
}
string Book::getTitle()
{
return title;
}
float Book::getPrice()
{
return price;
}
void Book::setAuthor(string a)
{
author = a;
}
void Book::setTitle(string t)
{
title =t;
}
void Book::setPrice(float p)
{
price = p;
}
// Q2 : CLASS METHODS Part 2 : Class Methods for Book (10 points)
// Create a method called "toString"
// This method has no input parameters and returns the string containing the title, author, and price of a book.
// This function is used in print_all function inside main to print the information of a book.
// The exact format can be found in the sample output of assignment description.
// code here
string Book::toString()
{
string result;
result.append(getTitle());
result.append(" ");
result.append(getAuthor());
result.append(" ");
string s=to_string(getPrice());
result.append(s);
result.append(" ");
return result;
}
/******************************************************/
// Create a method named "buyBook" which has one string parameter date and no return type (see helper function for use).
// This method is used to add a new date to the book's linked list of buys. The string parameter is the date of buying.
// You should add the date to the tail of the linked list "buys". Buys will be added in chronological order.
// code here
void Book::buyBook(string date)
{
Buy *temp;
temp = buys;
while(temp!=NULL)
{
temp = temp->next;
}
temp = new Buy(date);
}
// Create a method named "timesBought"
// This method has no input parameters and returns the number of times the book has been bought.
// This function is used in print_all function inside the main function.
// code here
int Book::timesBought()
{
Buy *temp;
temp = buys;
int count=0;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}
// Create a method named "displayBought" which has no parameters and returns a string (see print_all function for use).
// This method will be used to return a string for the dates where the book has been bought.
// If the book was not bought, return an empty string.
// code here]
string Book::displayBought()
{
string result="";
if(timesBought()==0)
return result;
else
{
int count = timesBought();
Buy *temp;
temp = buys;
for(int i=0;i<count;i++)
{
result.append(temp->getDate());
result.append(" ");
temp=temp->next;
}
return result;
}
}
/*hope this helps. Thanks.*/
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.