Part 1 – Publication Class In your solution to homework 3, rename Book to Public
ID: 3595883 • Letter: P
Question
Part 1 – Publication Class
In your solution to homework 3, rename Book to Publication wherever it appears. Test your program to ensure it works properly.
Part 2 – 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, using this menu: 1) Display people 2) Display publications 3) Edit people 4) Edit publications 5) Check out publication 6) Check in publication 7) Quit
here is the other code:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person
{
string fullName;
int identifier;
string email;
public:
Person()
{
fullName = "";
identifier = 0;
email = "";
}
Person(string name, int id, string mail)
{
fullName = name;
identifier = id;
email = mail;
}
void setFullName(string name) { fullName = name; }
void setIdentifier(int id) { identifier = id; }
void setEmail(string mail) { email = mail; }
string getFullName() { return fullName; }
int getIdentifier() { return identifier; }
string getEmail() { return email; }
};
#endif
#ifndef BOOK_H
#define BOOK_H
class Book
{
string title;
string author;
bool status; //To check if book is checked out.
Person borrower; //Person who borrowed the book.
public:
Book(string ttl, string athr, bool stts, Person brwr)
{
title = ttl;
author = athr;
status = stts;
borrower = brwr;
}
/*Person getBorrower()
{
return borrower;
}*/
void setBorrower(Person brwr)
{
borrower = brwr;
}
void checkOut(Person brwr)
{
if(!status)
setBorrower(brwr);
}
void checkIn()
{
status = false;
}
string getTitle() { return title; }
string getAuthor() { return author; }
bool getStatus() { return status; }
string getBorrower()
{
string temp = "Name : " + borrower.getFullName();
temp += " ID : " + to_string(borrower.getIdentifier());
temp += " Email: " + borrower.getEmail();
return temp;
}
};
#endif
#include <iostream>
using namespace std;
int main()
{
Person person1 ("Ito Duy Le", 123, "Leduyhoangminh95@hotmail.com");
Person person2 ("Daniel Hien Dang", 234, "danielhiendang95@gmal.com");
Person person3("Jen Nguyen", 345, "Jennguyen95@gmal.com");
Person person4("Laxus Dreyar", 456, "Laxusdreyar23@yahoo.co.in");
cout << "Name : " << person1.getFullName() << endl;
cout << "ID : " << person1.getIdentifier() << endl;
cout << "Email: " << person1.getEmail() << endl << endl;
cout << "Name : " << person2.getFullName() << endl;
cout << "ID : " << person2.getIdentifier() << endl;
cout << "Email: " << person2.getEmail() << endl << endl;
cout << "Name : " << person3.getFullName() << endl;
cout << "ID : " << person3.getIdentifier() << endl;
cout << "Email: " << person3.getEmail() << endl << endl;
cout << "Name : " << person4.getFullName() << endl;
cout << "ID : " << person4.getIdentifier() << endl;
cout << "Email: " << person4.getEmail() << endl << endl;
person1.setFullName("Laxus Dreyar");
person1.setIdentifier(567);
person1.setEmail("Laxusdreyar23@yahoo.co.in")
cout << "Name : " << person1.getFullName() << endl;
cout << "ID : " << person1.getIdentifier() << endl;
cout << "Email: " << person1.getEmail() << endl << endl;
Book book1("Programming in C", "Dennis Ritchie", true, person1);
cout << "Title: " << book1.getTitle() << endl;
cout << "Author: " << book1.getAuthor() << endl;
if(book1.getStatus())
cout << "Borrower Details: "<< endl << book1.getBorrower() << endl << endl;
Book book2("Macbeth", "William Shakespear", false, person1);
cout << "Title: " << book2.getTitle() << endl;
cout << "Author: " << book2.getAuthor() << endl;
if(book2.getStatus())
cout << "Borrower Details: "<< endl << book2.getBorrower() << endl << endl;
Book book3("First Love", "Sachin Garg", true, person2);
cout << endl << "Title: " << book3.getTitle() << endl;
cout << "Author: " << book3.getAuthor() << endl;
if(book3.getStatus())
cout << "Borrower Details: "<< endl << book3.getBorrower() << endl;
}
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
#ifndef PERSON_H
#define PERSON_H
class Person
{
string fullName;
int identifier;
string email;
public:
Person()
{
fullName = "";
identifier = 0;
email = "";
}
Person(string name, int id, string mail)
{
fullName = name;
identifier = id;
email = mail;
}
void setFullName(string name) { fullName = name; }
void setIdentifier(int id) { identifier = id; }
void setEmail(string mail) { email = mail; }
string getFullName() { return fullName; }
int getIdentifier() { return identifier; }
string getEmail() { return email; }
};
#endif
#ifndef Publication_H
#define Publication_H
class Publication
{
string title;
string author;
bool status; //To check if Publication is checked out.
Person borrower; //Person who borrowed the Publication.
public:
Publication(string ttl, string athr, bool stts, Person brwr)
{
title = ttl;
author = athr;
status = stts;
borrower = brwr;
}
/*Person getBorrower()
{
return borrower;
}*/
void setBorrower(Person brwr)
{
borrower = brwr;
}
void checkOut(Person brwr)
{
if(!status)
setBorrower(brwr);
}
void checkIn()
{
status = false;
}
string getTitle() { return title; }
string getAuthor() { return author; }
bool getStatus() { return status; }
string getBorrower()
{
string temp = "Name : " + borrower.getFullName();
temp += " ID : " + to_string(borrower.getIdentifier());
temp += " Email: " + borrower.getEmail();
return temp;
}
};
#endif
#ifndef Book_H
#define Book_H
class Book : public Publication
{
int pages;
string format;
public:
Book(string ttl, string athr, bool stts, Person brwr,int bookPages,string bookFormat): Publication(ttl,athr,stts,brwr)
{
pages = bookPages;
format = bookFormat;
}
void setPages(int p)
{
pages = p;
}
void setFormat(string formatTmp)
{
format = formatTmp;
}
int getPages() { return pages; }
string getFormat() { return format; }
};
#endif
#ifndef Music_H
#define Music_H
class Music : public Publication
{
int duration;
string format;
public:
Music(string ttl, string athr, bool stts, Person brwr,int mduration,string musicFormat): Publication(ttl,athr,stts,brwr)
{
duration = mduration;
format = musicFormat;
}
void setDuration(int d)
{
duration = d;
}
void setFormat(string formatTmp)
{
format = formatTmp;
}
int getDuration() { return duration; }
string getFormat() { return format; }
};
#endif
#ifndef Video_H
#define Video_H
class Video : public Publication
{
string resolution;
string producer;
public:
Video(string ttl, string athr, bool stts, Person brwr,string vResolution,string producerF): Publication(ttl,athr,stts,brwr)
{
resolution = vResolution;
producer = producerF;
}
void setResolution(string r)
{
resolution = r;
}
void setProducer(string producerF)
{
producer = producerF;
}
string getResolution() { return resolution; }
string getProducer() { return producer; }
};
int main()
{
Person person1 ("Ito Duy Le", 123, "Leduyhoangminh95@hotmail.com1");
Person person2 ("Daniel Hien Dang", 234, "danielhiendang95@gmal.com1");
Person person3("Jen Nguyen", 345, "Jennguyen95@gmal.com1");
Person person4("Laxus Dreyar", 456, "Laxusdreyar23@yahoo.co.in1");
Publication publication1("Programming in C", "Dennis Ritchie", true, person1);
Publication publication2("Macbeth", "William Shakespear", false, person1);
Publication publication3("First Love", "Sachin Garg", true, person2);
cout << "Book Details " << endl;
Book book("Programming in C", "Dennis Ritchie", true, person1,120,"SOFT");
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
if(book.getStatus())
cout << "Borrower Details: "<< endl << book.getBorrower() << endl ;
cout << "Book Pages: " << book.getPages() << endl;
cout << "Book Format: " << book.getFormat() << endl << endl;
cout << "Music Details " << endl;
Music music("Macbeth", "William Shakespear", false, person2,450,"MP3");
cout << "Title: " << music.getTitle() << endl;
cout << "Author: " << music.getAuthor() << endl;
if(music.getStatus())
cout << "Borrower Details: "<< endl << music.getBorrower() << endl ;
cout << "Music Duration: " << music.getDuration() << endl;
cout << "Music Format: " << music.getFormat() << endl << endl;
cout << "Video Details " << endl;
Video video("First Love", "Sachin Garg", true, person3,"4K","Denis wills");
cout << "Title: " << video.getTitle() << endl;
cout << "Author: " << video.getAuthor() << endl;
if(video.getStatus())
cout << "Borrower Details: "<< endl << video.getBorrower() << endl ;
cout << "Video Resolution: " << video.getResolution() << endl;
cout << "Video Producer: " << video.getProducer() << endl << endl;
vector<Person> personList;
personList.push_back(person1);
personList.push_back(person2);
personList.push_back(person3);
personList.push_back(person4);
vector<Publication> publicationList;
publicationList.push_back(book);
publicationList.push_back(music);
publicationList.push_back(video);
vector<Person>::iterator it;
vector<Publication>::iterator it1;
int choice = 0;
cin >> choice;
while(choice == 2){
cout << "Kindly enter following choice " << endl ;
cout << "1) Display people" << endl ;
cout << "2) Display publications " << endl ;
cout << "3) Edit people " << endl ;
cout << "4) Edit publications " << endl ;
cout << "5) Check out publication " << endl ;
cout << "6) Check in publication " << endl ;
cout << "7) Quit " << endl ;
switch (choice) {
case 1:
std::cout << "People Information " << endl;
for ( it = personList.begin(); it != personList.end(); ++it ) {
//it->printFriendInfo();
cout << "Name : " << it->getFullName() << endl;
cout << "ID : " << it->getIdentifier() << endl;
cout << "Email: " << it->getEmail() << endl << endl;
}
break;
case 2:
std::cout << "Publication Information " << endl;
for ( it1 = publicationList.begin(); it1 != publicationList.end(); ++it1 ) {
cout << "Title: " << it1->getTitle() << endl;
cout << "Author: " << it1->getAuthor() << endl;
if(it1->getStatus()){
cout << "Borrower Details: "<< endl << it1->getBorrower() << endl << endl ;
}
}
break;
}
}
return 0;
}
Output
------------
$g++ -o main *.cpp $main Book Details Title: Programming in C Author: Dennis Ritchie Borrower Details: Name : Ito Duy Le ID : 123 Email: Leduyhoangminh95@hotmail.com1 Book Pages: 120 Book Format: SOFT Music Details Title: Macbeth Author: William Shakespear Music Duration: 450 Music Format: MP3 Video Details Title: First Love Author: Sachin Garg Borrower Details: Name : Jen Nguyen ID : 345 Email: Jennguyen95@gmal.com1 Video Resolution: 4K Video Producer: Denis wills Kindly enter following choice 1) Display people 2) Display publications 3) Edit people 4) Edit publications 5) Check out publication 6) Check in publication 7) Quit Title: Programming in C Author: Dennis Ritchie Borrower Details: Name : Ito Duy Le ID : 123 Email: Leduyhoangminh95@hotmail.com1 Title: Macbeth Author: William Shakespear Title: First Love Author: Sachin Garg Borrower Details: Name : Jen Nguyen ID : 345 Email: Jennguyen95@gmal.com1
Description:
1. Added all the derived classes like Book,Music and video
2. Remaned existing Book class to Publication
3. Introduced Menu.
4. Introduced setter,getter and constructor in each class.
Please verify it and let me know if you need more assitance on same