Class pointer data and deep copy You will modify the invoice class for this exer
ID: 3674140 • Letter: C
Question
Class pointer data and deep copy
You will modify the invoice class for this exercise. The invoice assignment is listed below for your convenience:
(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 number (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type double). Your class should have constructors that initialize the four data members. Provide a set and get function for each data member. In addition, provide a member function name as getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double. If the quantity is not positive, it should be set to zero.
Write a test program that demonstrates class Invoice’s capabilities, which means in client code, call each public function of the class.
You should separate class definition and member function implementation, i.e., list member function implementation outside of class body. Use one cpp file for class definition, implementation and testing code is fine.
Draw a UML diagram to describe the Invoice class. Be sure to distinguish the accessor and mutator functions. You may use Word to draw the diagram. Drawing class diagram helps you comprehend or visualize what the class does so is part of analyzing process. The proper sequence is draw the diagram first and then code the program.
The new exercise:
What stated above is for exercise on class and UML. You will modify this exercise to practice on class pointer data, constructor, destructor, copy constructor and assignment operator. Note I am asking you to modify the original code, no inheritance is involved.
Modify the invoice class as following:
Change the data type of both part number and part description to pointer to char, from the original type of string.
Modify the default constructor to set the part number and also part description to NULL pointer.
Modify the argument constructor to dynamically allocate memory and initialize part number and part description according to arguments.
Modify the destructor to clean up the dynamic memory.
Define a copy constructor to override the default copy constructor, so object constructed based on existing object will have its own set of dynamic memory.
Also override the assignment operator, to enable deep copying among invoice class objects when assigned to one another.
Test your new class. Make sure the modified features are all tested.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Invoice
{
public:
void setPartNumber (string number)
{
product = number;
}
string getPartNumber()
{
return product;
}
void setPartDescription (string desc)
{
description = desc;
}
string getPartDescription()
{
return description;
}
void setQuantityPurchased (int amount)
{
quantity = amount;
}
int getQuantityPurchased()
{
return quantity;
}
void setPriceItem (float price)
{
prices = price;
}
float getPriceItem()
{
return prices;
}
void getInvoiceAmount()
{
float total = quantity * prices;
cout << "Producto#: " << getPartNumber() << " Descripcion: " << getPartDescription() << " Cantidad: " << getQuantityPurchased() << " Precio: " << getPriceItem() << " Su total: " << total << endl;
}
private:
int quantity;
float prices;
string product;
string description;
};
int main()
{
Invoice invoice1;
Invoice invoice2;
invoice1.setPartDescription("CUtter");
invoice1.setPartNumber("6321");
invoice1.setPriceItem(10.99);
invoice1.setQuantityPurchased(8);
invoice1.getInvoiceAmount();
invoice2.setPartDescription("A nail");
invoice2.setPartNumber("421");
invoice2.setPriceItem(.32);
invoice2.setQuantityPurchased(75);
invoice2.getInvoiceAmount();
return 0;
}
Java:
import java.io.*;
class Invoice
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String part_num;
String part_des;
int num_of_items;
double price_of_items;
double amount;
Invoice()
{
part_num="1";
part_des="book";
num_of_items=1;
price_of_items=100;
}
public String get_part_num() throws IOException
{
System.out.println("Enter part number");
part_num=br.readLine();
return part_num;
}
public String get_part_des()throws IOException
{
System.out.println("Enter part description ");
part_des=br.readLine();
return part_des;
}
public int get_num_of_items()throws IOException
{
System.out.println("Enter number of items ");
num_of_items=Integer.parseInt(br.readLine());
return num_of_items;
}
public double get_price_of_items()throws IOException
{
System.out.println("Enter price of items");
price_of_items=Double.parseDouble(br.readLine());
return price_of_items;
}
public double Invoice_amount()
{
amount = price_of_items*num_of_items;
amount = (amount>0)?amount:0;
return amount;
}
public void displayInfo()
{
System.out.printf("part number "+part_num+" part description "+part_des+" num_of_items "+num_of_items+" price_of_items "+price_of_items);
System.out.printf(" Amount "+amount);
// System.out.println();
}
public static class InvoiceTest
{
public static void main(String [] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Invoice inv1 = new Invoice();
inv1.get_part_num();
inv1.get_part_des();
inv1.get_num_of_items();
inv1.get_price_of_items();
inv1.Invoice_amount();
inv1.displayInfo();
}
}
}