Question

In: Computer Science

1. Define a class counterType to implement a counter. Your class must have a private data...

1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative.


2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that defines the book as an ADT.

Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another member variable.

Include the member functions to perform the various operations on objects of type bookType. For example, the usual operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title of the book. Similarly, the typical operations that can be performed on the number of copies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (if one is needed).

Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and tests various operations on the objects of the class bookType. Declare an array of 100 components of type bookType. Some of the operations that you should perform are to search for a book by its title, search by ISBN, and update the number of copies of a book.

Solutions

Expert Solution

1. the code snippet for the first part:-

#include <iostream>

using namespace std;

/**

class definition for CounterType

*/

class CounterType {

private:

// private variables

int count;

int counter = 0;

public:

// default constructor

CounterType() { count = 0; }

// constructor which accept count value and

// set the value to count

CounterType(int c) { count = c; }

// incrementing counter

void increseCounter() { counter++; }

// decreasing the count

int decreaseCount() {

// if count reach to then returning false

if (count == 0)

return 0;

count--;

return 1;

}

// methods for get count and get counter

int getCount() { return count; }

int getCounter() { return counter; }

// method for printing the info

void print_c(ostream &cout) {

cout << "Count : " << getCount() << "\n";

cout << "Counter : " << getCounter() << "\n\n";

}

};

// test method

int main(int argc, char const *argv[]) {

int value;

cout << "Please enter a COUNT value: ";

cin >> value;

CounterType c(value);

// using while loop printing

// count and counter

while (1) {

c.print_c(cout);

c.increseCounter();

int t = c.decreaseCount();

// if count reach to 0 then nothing program will exit

if (!t) {

break;

}

}

return 0;

}// end of program

2. Code for the second part:-

#include<iostream>
#include<string>

usingnamespace std;


classbookType//class to store information about the book
{
string title; //stories the title of an oject
string Authors[4]; //stores the Authors names
int n_Author; //stores number of Authors
string publishr; //Stores the name of Publisher
string ISBN;//store ISBN NUmber
double price; //Stores the price
int copies;
public:
bookType(); //constructor
void setTitle(string); //function declaration to set the title
string r_title(); //function declaration to return the title
void showTitle(); //function declaration to show the title

void SetAuthors(int); //function declaration to set the Author(s)
void ShowAuthors()const; //function declaration to show the Autho(s)

void setPublishr(string); //function declaration to set the Publisher name
void showPublishr(); //function declaration to show the Publisher name

void setIBSN(string); //function declaration to set the IBSN No.
string r_IBSN(); //function declaration to return the IBSN No.
void showIBSN(); //function declaration to show the IBSN No.

void setPrice(double); //function declaration to set the price
double R_price(); //function declaration to return the price
void showPrice(); //function declaration to show the price

void setCopies(int); //function declaration to set the No.of Copies
int r_copies(); //function declaration to return the No.of Copies
void showCopies(); //function declaration to show the No.of Copies
void u_Copies(int); //function declaration to update the No.of Copies

void print(); //function declaration to print the all data about the book

};
void search();
bookType A[3]; //creating object using Aray


int main()
{
string ti;
int n,c=1;
double p;
char co;
for (int i = 0; i < 3; i++) //loop to take the data of multiple books
{
cout <<"BOOK No."<< i + 1 << endl;
cout <<"Enter the Title :";
getline(cin, ti); //Takes the title from the user
A[i].setTitle(ti); //set the title in private member of a class
cout <<"Enter the Numbers of Authors : ";
while (!(cin >> n) || n<0) { //validation checking of number of authors
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the Numbers of Authors again :";
}
if (n > 4 || n <= 0)
{
cout <<"Enter the Numbers of Authors in the range of 1~4 :";
cin >> n;
}
A[i].SetAuthors(n); //set the Authors NAme

cout <<"Enter the Name of Publisher :";
getline(cin, ti); //Takes the Nmae of the publisher from the user
A[i].setPublishr(ti); //sets the publisher name
cout <<"Enter the ISBN no : ";
getline(cin, ti); //Takes the ISBN no. from the user
A[i].setIBSN(ti); //ststhe ISBN name
cout <<"Enter the price : ";
while (!(cin >> p) || p<0) { //validation checking of price entered by the user
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the Price again :";
}
A[i].setPrice(p); //sets the price of the book
cout <<"Enter the number of copies :";
while (!(cin >> n) || n<0) { //validation checking of no of copies entered by the user
cin.clear();
cin.ignore(999, '\n');
cout <<"Invalid data type! \nPlease Enter the number of copies again :";
}
A[i].setCopies(n); //set the no. of copies
cout << endl;
cin.ignore();
}
for (int i = 0; i < 3; i++) //loop to diplay the data of multiple books
{
A[i].print();
cout << endl;
}
search(); //calling of search function
system("pause");
return 0;
}
voidbookType::setTitle(stringt) //Functon to set the title
{
title = t;
}
stringbookType::r_title() //Functon to return the title
{
return title;
}
voidbookType::showTitle() //Functon to show the title
{
cout <<"\tTitle : "<< r_title() << endl;
}

voidbookType::SetAuthors(intn) //Functon to set the names of author(s)
{
cin.ignore();
for (int i = 0; i <n; i++)
{
cout <<"\tAuthor #"<< i + 1<<" : ";
getline(cin, Authors[i]);
}

}
voidbookType::ShowAuthors() const//Functon to display the names of Authors
{
cout <<"\tAuthor(s)";
if (Authors[0] != "")
cout << Authors[0];
if (Authors[1] != "")
cout <<", "<<Authors[1];
if (Authors[2] != "")
cout <<", "<< Authors[2];
if (Authors[3] != "")
cout <<", "<< Authors[3];
cout << endl;
}
voidbookType::setPublishr(stringp) //Functon to set the name of publisher
{
publishr = p;
}
voidbookType::showPublishr() //Functon to show the name of publisher
{
cout <<"\tPublisher : "<< publishr << endl;
}
voidbookType::setIBSN(stringi) //Functon to set ISBN no.
{
ISBN = i;
}
stringbookType::r_IBSN() //Functon to return ISBN no.
{
return ISBN;
}
voidbookType::showIBSN() //Functon to return ISBN no.
{
cout <<"\tISBN : "<< r_IBSN() << endl;
}
voidbookType::setPrice(doublep) //Functon to set the price of a book
{
price = p;
}
doublebookType::R_price() //Functon to return the price of a book
{
return price;
}
voidbookType::showPrice() //Functon to show the price of a book
{
cout <<"\tPrice : "<< R_price() << endl;
}
voidbookType::setCopies(intn) //Functon to set the no. of copies of a book
{
copies = n;
}
intbookType::r_copies() //Functon to return the no. of copies of a book
{
return copies;
}
voidbookType::u_Copies(intu) //Functon to update the no. of copies of a book
{
copies=u;
}
voidbookType::showCopies()
{
cout <<"\tCopies in Stock : "<< r_copies() << endl;
}
voidbookType::print() ////Functon to print the book information
{
showTitle();
ShowAuthors();
showPublishr();
showIBSN();
showPrice();
showCopies();
}
bookType::bookType() //constructor definition
{
title = "";
Authors[0] = "";
Authors[1] = "";
Authors[2] = "";
Authors[3] = "";
n_Author = 0;
ISBN = "0-00000-00";
price = 0.00;
copies = 0;
}
void search() //function to serach the book by ISBN no. or by title
{
int ch,c=1,n;
char co;
string ti;
cout <<"Enter 1 for to Search by 'Title' and '2' for to search by IBSN No. : ";
cin >> ch; //takes the choice
switch (ch)
{
case 1: //executes if user enters 1
{
cin.ignore();
cout <<"Enter the title :";
getline(cin, ti);
for (int i = 0; i < 3; i++)
{
string t = A[i].r_title();
if (ti == t)
{
c = 0;
A[i].print();
cout <<"Did you want of modify the no of copies (y/n) :";
cin >> co;
switch (co)
{
case'y':
{
cout <<"Enter the nmuber of Copies :";
cin >> n;
A[i].u_Copies(n);
cout << endl <<"Modified DATA "<< endl << endl;
A[i].print();
break;
}
case'n':
break;
default:
cout <<"Invliad Choice"<< endl;
}
}
if (c == 1 && i == 2)
cout <<"Not found a book with this title"<< endl;
}
break;
}
case 2: //executes if user enters 2
{
cin.ignore();
cout <<"Enter the IBSN :";
getline(cin, ti);
for (int i = 0; i < 3; i++)
{
string t = A[i].r_IBSN();
if (t == ti)
{
c = 1;
A[i].print();
cout <<"Did you want of modify the no of copies (y/n) :";
cin >> ch;
switch (ch)
{
case'y':
{
cout <<"Enter the nmuber of Copies :";
cin >> n;
A[i].u_Copies(n);
cout << endl <<"Modified DATA "<< endl << endl;
A[i].print();
break;
}
case'n':
break;
default:
cout <<"Invliad Choice"<< endl;
}
}
if (c = 1 && c == 2)
cout <<"Not found a book with this ISBN NO."<< endl;
}
break;
}
default: //exectes if user enter invalid choice
cout <<"Invliad choice"<< endl;
}

}


Related Solutions

public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include an accessor method that returns the current count value (getCount()). There should be no input /...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
For this Lab you have to implement a class Builder. Your Builder class should have instance...
For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s). Examining the problem, we need to create a Builder class, declare Builder class as follows public class Builder { } Inside the Builder class, declare a String variable called name. Step 3: Defining the constructors: Remember that the purpose of...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the end of...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT