Hamdard University Islamabad Campus: Assignment No
Hamdard University Islamabad Campus: Assignment No
ASSIGNMENT No. 04
Submission Date
Due Date
*By signing above, you attest that you have contributed to this submission and
confirm that all work you have contributed to this submission is your own work. Any
suspicion of copying or plagiarism in this work will result in an investigation of
Academic Misconduct and may result in a “0” on the work, an “F” in the course, or
possibly more severe penalties.
Page 2 of 5
Lab Tasks
Task # 1
Program Statement:
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 number (type string), a part description (type
string), a quantity of the item being purchased (type int) and a price per item (type int). 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 get Invoice Amount that calculates 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.
Program Code:
#include<iostream>
#include <string>
using namespace std;
class Invoice
{
private:
string partNumber;
string partDescription;
int itemQuantity;
int itemPrice;
public:
Invoice(string, string, int, int);
void setPartNumber(string);
string getPartNumber();
void setPartDescription(string);
string getPartDescription();
void setItemQuantity(int);
int getItemQuantity();
void setItemPrice(int);
int getItemPrice();
int getInvoiceAmount();
};
Invoice::Invoice(string number, string description, int quantity, int price)
{
partNumber = number;
partDescription = description;
if (quantity>0)
itemQuantity = quantity;
else
{
itemQuantity = 0;
Page 3 of 5
{
itemPrice = 0;
cout << "Initial price was invalid." << endl;
}
}
string Invoice::getPartNumber()
{
return partNumber;
}
string Invoice::getPartDescription()
{
return partDescription;
}
int Invoice::getItemQuantity()
{
return itemQuantity;
}
int Invoice::getItemPrice()
{
return itemPrice;
}
int Invoice::getInvoiceAmount()
{
return itemQuantity*itemPrice;
}
int main()
{
Invoice Invoice1("a343", "Screw Guage", 2, 30);
Invoice Invoice2("b434", "Screws", 10, 3);
cout << "Invoice1's initial part number is: " <<
Invoice1.getPartNumber() << endl << "and part description is: " <<
Invoice1.getPartDescription() << endl;
cout << "quantity per item is: " << Invoice1.getItemQuantity() << endl
<< "price per item is: " << Invoice1.getItemPrice() << endl;
cout << "Invoice1's total amount is: " << Invoice1.getInvoiceAmount()
<< endl << endl;
cout << "Invoice2's initial part number is: " <<
Invoice2.getPartNumber() << endl << "and part description is: " <<
Invoice2.getPartDescription() << endl;
cout << "quantity per item is: " << Invoice2.getItemQuantity() << endl
<< "price per item is: " << Invoice2.getItemPrice() << endl;
cout << "Invoice2's total amount is: " << Invoice2.getInvoiceAmount()
<< endl;
system("pause");
}
Page 5 of 5
Program Output: