Vous êtes sur la page 1sur 5

Page 1 of 5

Hamdard University Islamabad Campus


Hamdard Institute of Engineering & Technology
Department of Computing

Program: BS Computer Science

Course Number CS122


Course Title Object Oriented Programming
Semester/Year Fall 2020

Instructor Ms. Saadia Mooqaddas

ASSIGNMENT No. 04

Assignment Title Lab NO 4

Submission Date
Due Date

Student Name Muhammad Umer Farooq


Student CMS-ID 445-2020
Signature*

*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

cout << "Initial quantity was invalid." << endl;


}
if (price>0)
itemPrice = price;
else
{
itemPrice = 0;
cout << "Initial price was invalid." << endl;
}
}
void Invoice::setPartNumber(string number)
{
if (number.length() <= 50)
partNumber = number;
if (number.length() > 50)
{
partNumber = number.substr(0, 50);
cout << "Name \"" << number << "\" exceeds maximum length
(50).\n" << "Limiting partNumber to first 50 characters.\n" << endl;
}
}
void Invoice::setPartDescription(string description)
{
if (description.length() <= 50)
partDescription = description;
if (description.length() > 50)
{
partDescription = description.substr(0, 50);
cout << "Name \"" << description << "\" exceeds maximum length
(50).\n" << "Limiting partDescription to first 50 characters.\n" << endl;
}
}
void Invoice::setItemQuantity(int quantity)
{
if (quantity>0)
itemQuantity = quantity;
else
{
itemQuantity = 0;
cout << "Initial quantity was invalid." << endl;
}
}
void Invoice::setItemPrice(int price)
{
if (price>0)
itemPrice = price;
else
Page 4 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:

Vous aimerez peut-être aussi