Hello I need help with these questions. I am still a beginner with the c++ langu
ID: 3751668 • Letter: H
Question
Hello I need help with these questions. I am still a beginner with the c++ language and im having problems trying to understand what to do here. IF you can include comments for what you did and how that would be appreciated! Will upvote thank you.
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
class Book
{
public:
Book(int=0, string="Unknown", string="Unknown", int=0);
void setBook(int, string, string, int);
void print();
private:
int id;
string title;
string author;
int year;
};
#endif
~
Book.cc
#include <iostream>
#include <iomanip>
using namespace std;
#include "Book.h"
Book::Book(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
void Book::setBook(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
main.cc
#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
#define MAX_ARR_SIZE 128
int mainMenu();
void printLibrary(Book arr[MAX_ARR_SIZE], int num);
int main()
{
Book library[MAX_ARR_SIZE];
int numBooks = 0;
string title, author;
int id, year;
int menuSelection;
while (1) {
menuSelection = mainMenu();
Makefile
OPT = -Wall
t01: main.o Book.o
g++ $(OPT) -o t01 main.o Book.o
main.o: main.cc Book.h
g++ $(OPT) -c main.cc
Book.o: Book.cc Book.h
g++ $(OPT) -c Book.cc
clean:
rm -f *.o t01
Explanation / Answer
Please give thumbs up, thanks
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
class Book
{
public:
Book(int=0, string="Unknown", string="Unknown", int=0);
void setBook(int, string, string, int);
void print();
int getid();
string gettitle();
string getaythor();
int getyear();
private:
int id;
string title;
string author;
int year;
};
#endif
Book.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "Book.h"
Book::Book(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
void Book::setBook(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
int Book:: getid(){
return this->id;
}
string Book::gettitle()
{
return this->title;
}
string Book::getaythor()
{
return this->author;
}
int Book::getyear()
{
return this->year;
}
Library.h
#ifndef Library_h
#define Library_h
#include"Book.h"
#include<iostream>
#define MAX_ARR_SIZE 10
class Library
{
private:
Book B[MAX_ARR_SIZE];
int no_of_books;
public:
Library()
{
this->no_of_books=0;
}
void addBook(Book &B)
{
this->B[this->no_of_books]=B;
this->no_of_books++;
}
void print(){
for(int i=0; i<this->no_of_books; i++){
cout<<B[i].getid()<<" "<<B[i].gettitle()<<" "<<B[i].getaythor()<<" "<<B[i].getyear()<<endl;
}
}
};
#endif
Main.cpp
#include<iostream>
#include"Library.h"
using namespace std;
int main()
{
Library L=Library();
int n;
int id=1,year;
string title,author;
cout<<"How many books wants to add to Library : ";
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"Enter Title : ";
cin>>title;
cout<<"Enter Author : ";
cin>>author;
cout<<"Enter year : ";
cin>>year;
Book B=Book(id++,title,author,year);
L.addBook(B);
}
L.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.