Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A class to represent a media item
- class Media {
- public:
- // A constructor to initialize the data members
- Media(string type, string author, string title, int year, bool status) {
- this->type = type;
- this->author = author;
- this->title = title;
- this->year = year;
- this->status = status;
- }
- // A method to display the media information
- void display() {
- cout << "Type: " << type << endl;
- cout << "Author: " << author << endl;
- cout << "Title: " << title << endl;
- cout << "Year: " << year << endl;
- cout << "Status: " << (status ? "Free" : "Occupied") << endl;
- }
- // Getters and setters for the data members
- string getType() {
- return type;
- }
- void setType(string type) {
- this->type = type;
- }
- string getAuthor() {
- return author;
- }
- void setAuthor(string author) {
- this->author = author;
- }
- string getTitle() {
- return title;
- }
- void setTitle(string title) {
- this->title = title;
- }
- int getYear() {
- return year;
- }
- void setYear(int year) {
- this->year = year;
- }
- bool getStatus() {
- return status;
- }
- void setStatus(bool status) {
- this->status = status;
- }
- private:
- // Data members to store the media information
- string type; // The type of the media
- string author; // The author of the media
- string title; // The title of the media
- int year; // The year of publication of the media
- bool status; // The status of the media (free or occupied)
- };
- // A class to represent a virtual library
- class Library {
- public:
- // A constructor to initialize the data member
- Library() {
- mediaList = vector<Media>(); // An empty vector of media items
- }
- // A method to add a new media item to the library
- void addMedia(Media media) {
- mediaList.push_back(media); // Append the media item to the vector
- cout << "Media added successfully." << endl;
- }
- // A method to display all the media items in the library
- void displayAll() {
- cout << "The library contains " << mediaList.size() << " media items." << endl;
- for (int i = 0; i < mediaList.size(); i++) { // Loop through the vector
- cout << "Media #" << i + 1 << ":" << endl;
- mediaList[i].display(); // Display each media item
- cout << endl;
- }
- }
- private:
- // A data member to store a vector of media items
- vector<Media> mediaList;
- };
- // A main function to test the classes
- int main() {
- // Create a library object
- Library library;
- // Create some media objects
- Media m1("Book", "J.K. Rowling", "Harry Potter and the Philosopher's Stone", 1997, true);
- Media m2("Magazine", "National Geographic", "The Future of Medicine", 2020, false);
- Media m3("CD-ROM", "Microsoft", "Windows 10", 2015, true);
- // Add the media objects to the library
- library.addMedia(m1);
- library.addMedia(m2);
- library.addMedia(m3);
- // Display all the media items in the library
- library.displayAll();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement