C++ Q\': • The main program and each class definition must be in separate files.
ID: 3594441 • Letter: C
Question
C++ Q':
• The main program and each class definition must be in separate files.
• Hard code test data in the program. Do not enter test data when you run the program.
• Use functions to break your main program into components
• Do not use data structures
Part 1
a) Person class
Write a Person class that tracks subscribers to a library, with these member attributes:
• full name
• identifier (numeric)
• email address
Person has these member functions:
• Constructor - initializes new object
• get and set function for each attribute
Write a main program with these capabilities:
• Create test data for four people
• Display all people
• Edit attributes of an existing person
1b) Book Class
Write a Book class that tracks all books in a library, with these member attributes:
• title
• author
• status - indicates if book is checked out
• borrower – a pointer to the person that has currently checked out book, if any
Book has these member functions:
• Constructor - initializes new object
• get and set function for each attribute
• checkOut - enables Person to check out Book - do not allow if book is already checked
out
• checkIn - enables Person to check in Book
Extend your main program of part 1 with these capabilities:
• Create test data for six books
• display all books in library, along with name of borrower (if any)
• enable people to check books in and out
Part 2
a) Publication Class
In your solution to part 1, rename Book to Publication wherever it appears. Test your
program to ensure it works properly.
2b) Book, Music, and Video classes
Add a new Book class, as well as Music and Video classes. All three classes are derived from the
Publication class. Add these member variables:
Book class:
• pages
• format (hardcover, softcover, digital)
Music class:
• duration (seconds)
• format (MP3, AAV, WAV)
Video class:
• resolution (low, high, 4K)
• producer
All three classes should have constructors, get, and set functions.
Extend your main program to edit and display Publication and Person data. Your user interface
should use menus. For example, the main menu might look like this:
1) Display people
2) Display publications
3) Edit people
4) Edit publications
5) Quit
Explanation / Answer
Given code for part 1. Please post part 2 as a separate question. Thanks.
Person.h
#ifndef Person_h
#define Person_h
#include <iostream>
using namespace std;
class Person
{
private:
string fullname;
int id;
string email;
public:
Person();
Person(string name, int id, string email);
string getFullName();
void setFullName(string name);
int getId();
void setId(int id);
string getEmail();
void setEmail(string email);
};
#endif /* Person_h */
Person.cpp
#include "Person.h"
Person::Person()
{
id = 0;
}
Person::Person(string name, int id, string email)
{
this->fullname = name;
this->id = id;
this->email = email;
}
string Person::getFullName()
{
return fullname;
}
void Person::setFullName(string name)
{
this->fullname = name;
}
int Person::getId()
{
return id;
}
void Person::setId(int id)
{
this->id = id;
}
string Person::getEmail()
{
return email;
}
void Person::setEmail(string email)
{
this->email = email;
}
Book.h
#ifndef Book_h
#define Book_h
#include "Person.h"
class Book
{
private:
string title;
string author;
Person *borrower;
public:
Book();
Book(string title, string author);
string getTitle();
void setTitle(string title);
string getAuthor();
void setAuthor(string author);
Person* getBorrower();
bool checkOut(Person* p);
void checkIn();
};
#endif /* Book_h */
Book.cpp
#include "Book.h"
Book::Book()
{
borrower = NULL;
}
Book::Book(string title, string author)
{
this->title = title;
this->author = author;
}
string Book::getTitle()
{
return title;
}
void Book::setTitle(string title)
{
this->title = title;
}
string Book::getAuthor()
{
return author;
}
void Book::setAuthor(string author)
{
this->author = author;
}
Person* Book::getBorrower()
{
return borrower;
}
bool Book::checkOut(Person* p)
{
if(borrower == NULL)
{
borrower = p;
return true;
}
else
return false;
}
void Book::checkIn()
{
borrower = NULL;
}
Test.cpp
#include "Person.h"
#include "Book.h"
#include <iostream>
using namespace std;
int main()
{
Person p1("John Smith", 111, "john@gmai.com");
Person p2("Michael J", 222, "michael@gmai.com");
Person p3("Robert", 333, "robert@gmai.com");
Person p4("Bob", 44, "bob@gmai.com");
cout << "p1 [Name: " << p1.getFullName() << ", Id: " << p1.getId() << ", Email: " << p1.getEmail() << "]" << endl;
cout << "p2 [Name: " << p2.getFullName() << ", Id: " << p2.getId() << ", Email: " << p2.getEmail() << "]" << endl;
cout << "p3 [Name: " << p3.getFullName() << ", Id: " << p3.getId() << ", Email: " << p3.getEmail() << "]" << endl;
cout << "p4 [Name: " << p4.getFullName() << ", Id: " << p4.getId() << ", Email: " << p4.getEmail() << "]" << endl;
cout << " changing values ";
//changing the values
p1.setId(666);
p2.setFullName("Michael Jackson");
p3.setEmail("robert@hotmai.com");
cout << "p1 [Name: " << p1.getFullName() << ", Id: " << p1.getId() << ", Email: " << p1.getEmail() << "]" << endl;
cout << "p2 [Name: " << p2.getFullName() << ", Id: " << p2.getId() << ", Email: " << p2.getEmail() << "]" << endl;
cout << "p3 [Name: " << p3.getFullName() << ", Id: " << p3.getId() << ", Email: " << p3.getEmail() << "]" << endl;
Book b1("Book1", "Author1");
Book b2("Book2", "Author2");
Book b3("Book3", "Author3");
Book b4("Book4", "Author4");
Book b5("Book5", "Author5");
Book b6("Book6", "Author6");
cout << "b1 checkout by p1 and b2 checkout by p2" << endl;
b1.checkOut(&p1);
b2.checkOut(&p2);
cout << "b1: [Title: " << b1.getTitle() << ", Author: " << b1.getAuthor() << ", Borrorwer: " << b1.getBorrower()->getFullName() << "]" << endl;
cout << "b2: [Title: " << b2.getTitle() << ", Author: " << b2.getAuthor() << ", Borrorwer: " << b2.getBorrower()->getFullName() << "]" <<endl;
cout << "b3: [Title: " << b3.getTitle() << ", Author: " << b3.getAuthor() ;
if(b3.getBorrower() == NULL)
cout << ", Borrower: None ]" << endl;
else
cout << ", Borrower: " << b3.getBorrower()->getFullName() << endl;
cout << "trying checkout of b1 by p4" << endl;
b1.checkOut(&p4);
cout << "b1: [Title: " << b1.getTitle() << ", Author: " << b1.getAuthor() << ", Borrorwer: " << b1.getBorrower()->getFullName() << "]" << endl;
cout << "b1 checkin" << endl;
b1.checkIn();
cout << "trying checkout of b1 by p4" << endl;
b1.checkOut(&p4);
cout << "b1: [Title: " << b1.getTitle() << ", Author: " << b1.getAuthor() << ", Borrorwer: "
<< b1.getBorrower()->getFullName() << "]" << endl;
}
output
p1 [Name: John Smith, Id: 111, Email: john@gmai.com]
p2 [Name: Michael J, Id: 222, Email: michael@gmai.com]
p3 [Name: Robert, Id: 333, Email: robert@gmai.com]
p4 [Name: Bob, Id: 44, Email: bob@gmai.com]
changing values
p1 [Name: John Smith, Id: 666, Email: john@gmai.com]
p2 [Name: Michael Jackson, Id: 222, Email: michael@gmai.com]
p3 [Name: Robert, Id: 333, Email: robert@hotmai.com]
b1 checkout by p1 and b2 checkout by p2
b1: [Title: Book1, Author: Author1, Borrorwer: John Smith]
b2: [Title: Book2, Author: Author2, Borrorwer: Michael Jackson]
b3: [Title: Book3, Author: Author3, Borrower: None ]
trying checkout of b1 by p4
b1: [Title: Book1, Author: Author1, Borrorwer: John Smith]
b1 checkin
trying checkout of b1 by p4
b1: [Title: Book1, Author: Author1, Borrorwer: Bob]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.