Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. - C++ Class

C++ examples for Class:Class Creation

Description

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store.

Demo Code

                  
#include <string>
#include <iostream>
using namespace std;
class Invoice {/*  w  w  w.  java 2s.  c o m*/
private:
  std::string partNumber;
  std::string partDescription;
                     
  int itemQuantity;
  int pricePerItem;
                     
public:
  Invoice(std::string, std::string, int, int);
                     
  // SETTERS
  void setPartNumber(std::string);
  void setPartDescription(std::string);
  void setItemQuantity(int);
  void setPricePerItem(int);
                     
  // GETTERS
  std::string getPartNumber();
  std::string getPartDescription();
  int getItemQuantity();
  int getPricePerItem();
                     
  int getInvoiceAmount();
};
// Constructor
Invoice::Invoice(std::string number, std::string description, int quantity,
  int price) {
  setPartNumber(number);
  setPartDescription(description);
  setItemQuantity(quantity);
  setPricePerItem(price);
}
// SETTERS
void Invoice::setPartNumber(std::string number) {
  partNumber = number;
}
                     
void Invoice::setPartDescription(std::string description) {
  partDescription = description;
}
void Invoice::setItemQuantity(int quantity) {
  itemQuantity = (quantity > 0) ? quantity : 0;
}
void Invoice::setPricePerItem(int price) {
  pricePerItem = (price > 0) ? price : 0;
}
// GETTERS
std::string Invoice::getPartNumber() { return partNumber; }
                     
std::string Invoice::getPartDescription() { return partDescription; }
                     
int Invoice::getItemQuantity() { return itemQuantity; }
                     
int Invoice::getPricePerItem() { return pricePerItem; }
                     
// calcualates the invoice amount
int Invoice::getInvoiceAmount() { return itemQuantity * pricePerItem; }
                     
int main(int argc, const char *argv[]) {
  Invoice invoice1("12345", "Hammer", 12, 6);
                     
  cout << "Part Number: " << invoice1.getPartNumber();
  cout << "\nPart Description: " << invoice1.getPartDescription();
  cout << "\n" << invoice1.getItemQuantity() << " x " << invoice1.getPricePerItem();
  cout << " = " << invoice1.getInvoiceAmount() << std::endl;
                     
  return 0;
}

Result


Related Tutorials