Question: You are given a class Book that represents a book with the following attributes: Title (string) , Author (string) , and Year of Publication (integer) The class must include: A constructor that initializes these attributes and prints a message when a Book object is created. A destructor that prints a message when a Book object is destroyed. A member function displayInfo () to print the book details in a readable format. Define the Book class with the constructor, and displayInfo () function. Create an array of Book objects to store three books with different details. Use pointers to access the objects in the array and call displayInfo () for each book.
Question: You are given a class Book that represents a book with the following attributes: Title (string) , Author (string) , and Year of Publication (integer) The class must include: A constructor that initializes these attributes and prints a message when a Book object is created. A destructor that prints a message when a Book object is destroyed. A member function displayInfo () to print the book details in a readable format. Define the Book class with the constructor, and displayInfo () function. Create an array of Book objects to store three books with different details. Use pointers to access the objects in the array and call displayInfo () for each book.
Question: You are given a class Book that represents a book with the following attributes: Title (string) , Author (string) , and Year of Publication (integer) The class must include: A constructor that initializes these attributes and prints a message when a Book object is created. A destructor that prints a message when a Book object is destroyed. A member function displayInfo () to print the book details in a readable format. Define the Book class with the constructor, and displayInfo () function. Create an array of Book objects to store three books with different details. Use pointers to access the objects in the array and call displayInfo () for each book.
Question: You are given a class Book that represents a book with the following attributes: Title (string) , Author (string) , and Year of Publication (integer) The class must include: A constructor that initializes these attributes and prints a message when a Book object is created. A destructor that prints a message when a Book object is destroyed. A member function displayInfo () to print the book details in a readable format. Define the Book class with the constructor, and displayInfo () function. Create an array of Book objects to store three books with different details. Use pointers to access the objects in the array and call displayInfo () for each book.
Question: You are given a class Book that represents a book with the following attributes: Title (string) , Author (string) , and Year of Publication (integer) The class must include: A constructor that initializes these attributes and prints a message when a Book object is created. A destructor that prints a message when a Book object is destroyed. A member function displayInfo () to print the book details in a readable format. Define the Book class with the constructor, and displayInfo () function. Create an array of Book objects to store three books with different details. Use pointers to access the objects in the array and call displayInfo () for each book.
#include <iostream> #include <string> using namespace std; // Define the Book class class Book { private: string title; string author; int yearOfPublication ; public: // Constructor to initialize attributes Book(string t, string a, int y) : title(t), author(a), yearOfPublication (y) { cout << "Book object created: " << title << endl ; } // Destructor ~Book() { cout << "Book object destroyed: " << title << endl ; } // Member function to display book information void displayInfo () { cout << "Title: " << title << endl ; cout << "Author: " << author << endl ; cout << "Year of Publication: " << yearOfPublication << endl ; cout << endl ; } }; int main() { // Create an array of Book objects Book* library[3]; // Initialize each Book object in the array library[0] = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925); library[1] = new Book("To Kill a Mockingbird", "Harper Lee", 1960); library[2] = new Book("1984", "George Orwell", 1949); // Access and display information using pointers for (int i = 0; i < 3; ++ i ) { library[ i ]-> displayInfo (); } // Clean up: delete dynamically allocated objects for (int i = 0; i < 3; ++ i ) { delete library[ i ]; } return 0; }