Final exam review C++ You have a list of books that needs to be updated. The Boo
ID: 3908412 • Letter: F
Question
Final exam review C++
You have a list of books that needs to be updated. The Book class contains two data members: qty(quantity), an integer value, and title, a string of size 30, and the usual setters and getters. First define the Book class, then write a function named update that searches the unsorted array of Book objects to find a given title. If found, add 1 to qty; otherwise, if there is room, append the new title at the end of the array and set qty to 1. This function has three parameters: the array, its length, and the title.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Book //defining class
{
private:
int qty;
String title[30];
public:
void setQty(int a)
{
qty=a;
}
void setTitle(int index, String a)
{
title[index]=a;
}
int getQty()
{
return qty;
}
String getTitle(int index)
{
return title[index];
}
int update(String a[],int length, String title)//memeber function
{
for(int i=0;i<length;i++)
{
if(a[i]==title) //searching for title
{
return 1; //return 1 if found
}
else
{
a.append(title); // appending if not found
return 0;
}
}
}
}
int main()
{
String searchTitle;
int qty;
int length;
String title[30];
Book b1;
cout<<"Enter Title to be Searched";
cin >>searchTitle ;
cout<<"Enter the quatity";
cin>>qty;
cout<<"Enter lenth of array";
cin>>length;
cout>>"Enter the elements of array";
for(int i=0;i<length;i++)
{
cin>>title[i];
}
for(int i=0;i<length;i++)
{
b1.setTitle(i,title[i]);
}
if(b1.update==1)
{
b1.setQty(qty+1);//incrementing qty since the title not found
}
else
{
for(i=0;i<length;i++)
{
b1.setTitle(i,title[i]);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.