(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—a part num- ber (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). [Note: In subsequent chapters, we’ll use numbers that contain

decimal points (e.g., 2.75)—called floating-point values—to represent dollar amounts.] Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calcu- lates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test program that demonstrates class Invoice’s capabilities.

What will be an ideal response?


```
// Definition of Invoice class that represents an invoice
// for an item sold at a hardware store.
#include // program uses C++ standard string class
using namespace std;

// Invoice class definition
class Invoice
{
public:
// constructor initializes the four data members
Invoice( string, string, int, int );

// set and get functions for the four data members
void setPartNumber( string ); // part number
string getPartNumber();
void setPartDescription( string ); // part description
string getPartDescription();
void setQuantity( int ); // quantity
int getQuantity();
void setPricePerItem( int ); // price per item
int getPricePerItem();

// calculates invoice amount by multiplying quantity x price per item
int getInvoiceAmount();
private:
string partNumber; // the number of the part being sold
string partDescription; // description of the part being sold
int quantity; // how many of the items are being sold
int pricePerItem; // price per item
}; // end class Invoice
```
```
// Member-function definitions for class Invoice.
#include
#include "Invoice.h"
using namespace std;

// Invoice constructor initializes the class's four data members
Invoice::Invoice( string number, string description, int count,
int price )
{
setPartNumber( number ); // store partNumber
setPartDescription( description ); // store partDescription
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store pricePerItem
} // end Invoice constructor

// set part number
void Invoice::setPartNumber( string number )
{
partNumber = number; // no validation needed
} // end function setPartNumber

// get part number
string Invoice::getPartNumber()
{
return partNumber;
} // end function getPartNumber

// set part description
void Invoice::setPartDescription( string description )
{
partDescription = description; // no validation needed
} // end function setPartDescription

// get part description
string Invoice::getPartDescription()
{
return partDescription;
} // end function getPartDescription

// set quantity; if not positive, set to 0
void Invoice::setQuantity( int count )
{
if ( count > 0 ) // if quantity is positive
quantity = count; // set quantity to count

if ( count <= 0 ) // if quantity is not positive
{
quantity = 0; // set quantity to 0
cout << "\nquantity cannot be negative. quantity set to 0.\n";
} // end if
} // end function setQuantity

// get quantity
int Invoice::getQuantity()
{
return quantity;
} // end function getQuantity

// set price per item; if not positive, set to 0.0
void Invoice::setPricePerItem( int price )
{
if ( price > 0 ) // if price is positive
pricePerItem = price; // set pricePerItem to price

if ( price <= 0 ) // if price is not positive
{
pricePerItem = 0; // set pricePerItem to 0
cout << "\npricePerItem cannot be negative. "
<< "pricePerItem set to 0.\n";
} // end if
} // end function setPricePerItem

// get price per item
int Invoice::getPricePerItem()
{
return pricePerItem;
} // end function getPricePerItem

// calulates invoice amount by multiplying quantity x price per item
int Invoice::getInvoiceAmount()
{
return getQuantity() * getPricePerItem();
} // end function getInvoiceAmount
```
```
// Create and manipulate an Invoice object.
#include
#include "Invoice.h"
using namespace std;

// function main begins program execution
int main()
{
// create an Invoice object
Invoice invoice( "12345", "Hammer", 100, 5 );

// display the invoice data members and calculate the amount
cout << "Part number: " << invoice.getPartNumber() << endl;
cout << "Part description: " << invoice.getPartDescription() << endl;
cout << "Quantity: " << invoice.getQuantity() << endl;
cout << "Price per item: $" << invoice.getPricePerItem() << endl;
cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;

// modify the invoice data members
invoice.setPartNumber( "123456" );
invoice.setPartDescription( "Saw" );
invoice.setQuantity( -5 ); // negative quantity, so quantity set to 0
invoice.setPricePerItem( 10 );
cout << "\nInvoice data members modified.\n\n";

// display the modified invoice data members and calculate new amount
cout << "Part number: " << invoice.getPartNumber() << endl;
cout << "Part description: " << invoice.getPartDescription() << endl;
cout << "Quantity: " << invoice.getQuantity() << endl;
cout << "Price per item: $" << invoice.getPricePerItem() << endl;
cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;
return 0; // indicate successful termination
} // end main
```
Part number: 12345
Part description: Hammer
Quantity: 100
Price per item: $5
Invoice amount: $500
quantity cannot be negative. quantity set to 0.
Invoice data members modified.
Part number: 123456
Part description: Saw
Quantity: 0
Price per item: $10
Invoice amount: $0

Computer Science & Information Technology

You might also like to view...

Select all that apply. Which of the following statement(s) about global variables is(are) true?

a. A global variable is accessible only to the main function. b. A global variable is declared in the high-est-level block in which it is used. c. A global variable can have the same name as a variable that is declared locally within a function. d. A global variable is the same as a named constant. e. If a function contains a local variable with the same name as a global variable, the global variable's name takes precedence within the function.

Computer Science & Information Technology

Using your local Internet Browser

a. Describe howyou can tell when you are connected to a site that is using the SSL protocol. b. Suppose you are connected to a site that is using the SSL protocol. Describe how you can determine the name of the CA that supplied the certificate used by that site. c. Determine howmany bits are in the keys that your browser uses for SSL encryption.

Computer Science & Information Technology

When planning a podcast, which of the following is nota step that needs to be considered?

a. Choose an aggregator. b. Develop a schedule. c. Consider the optimal length for the podcast. d. Decide upon a topic or theme for the podcast.

Computer Science & Information Technology

?Transitionis a technique of creating the illusion of movement by displaying a sequence of changing images in rapid succession.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology