Write a C++ program that defines a struct named book with title and author as at
ID: 3749616 • Letter: W
Question
Write a C++ program that defines a struct named bookwith title and author as attributes. Then define a class named librarywith private data members as (name , address and numberOfbooks, and a pointer of books named content).
Your class must contain setters and getters to each member attribute.
Notes:
In set_number_of_Books, you should initialize the dynamic pointer using the member attribute numberOfbooks
In set_specific_book_info which is the function that should fill a specific cell of the contentarray, must take the index of the book it will set with specific author and title.
In main, please let the user enter the number of the required libraries which means you will define a pointer to libraries.
Explanation / Answer
//C++ program
#include<iostream>
using namespace std;
typedef struct book{
string title;
string author;
}book;
class library{
private:
string name;
string address;
int number_of_books;
book *b;
public :
library(){
number_of_books=0;
b =NULL;
}
void set_number_of_books(int n){
number_of_books=n;
b= new book[n];
for(int i=0;i<n;i++){
cout<<"Enter Title : ";
cin>>b[i].title;
cout<<"Enter author : ";
cin>>b[i].author;
}
}
void get_books(){
for(int i=0;i<number_of_books;i++){
cout<<" Title : "<<b[i].title;
cout<<" Author : "<<b[i].author<<" ";
}
}
int get_book_count(){
return number_of_books;
}
};
int main(){
int num,n;
library *l;
cout<<"Enter number of library : ";
cin>> num;
l=new library[num];
cout<<"Enter book count of each library ";
for(int i=0;i<num;i++){
cout<<"Enter book count : ";
cin>>n;
l[i].set_number_of_books(n);
}
cout<<"Here is list of book in each library ";
for(int i=0;i<num;i++){
cout<<"Library "<<i+1<<" : "<<"Total "<<l[i].get_book_count()<<" Books ";
l[i].get_books();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.