the copy constructor test2 and assignment operator test2 fails to pass the test
ID: 3870540 • Letter: T
Question
the copy constructor test2 and assignment operator test2 fails to pass the test in my pricelist.cpp file here is what i got
//priceList.cpp
include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PriceList.h"
#include "PriceListItem.h"
using namespace std;
PriceList::PriceList()
{
numItems = 0;
arr = new PriceListItem[1000000];
}
PriceList::PriceList(const PriceList &list)
{
numItems = list.numItems;
arr = list.arr;
}
// Load information from a text file with the given filename.
void PriceList::createPriceListFromDatafile(string filename) {
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
string code;
double price;
bool taxable;
while (myfile >> name >> code >> price >> taxable) {
// cout << code << " " << taxable << endl;
addEntry(name, code, price, taxable);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return true only if the code is valid
bool PriceList::isValid(string code) {
for (int i = 0; i < numItems; i++)
{
if (arr[i].getCode()== code)
{
return true;
}
}
return false;
}
// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found
PriceListItem PriceList::getItem(string code) {
//const
for (int i = 0; i < numItems; i++)
{
if (arr[i].getCode() == code)
{
return arr[i];
}
}
throw invalid_argument("Invalid code:"+code);
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// create item object
PriceListItem a(itemName, code, price, taxable);
// store item object in array
arr[numItems] = a;
numItems++;
}
==============================================================================
//priceList.h
#pragma once
#include <string>
#include <stdexcept>
#include "PriceListItem.h"
using namespace std;
class PriceList {
public:
PriceList();
PriceList(const PriceList &list);
void createPriceListFromDatafile(string filename); // Load information from a text file with the given filename (Completed)
void addEntry(string itemName, string code, double price, bool taxable); // add to the price list information about a new item. A max of 1,000,000 entries can be added
bool isValid(string code); //const; // return true only if the code is valid
PriceListItem getItem(string code);// const; // return price, item name, taxable? as an PriceListItem object; throw exception if code is not found
private:
PriceListItem* arr;
int numItems;
};
=====================================================================
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include "priceListItem.h"
#include "PriceList.h"
//#include "GroceryBill.h"
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT THIS FILE (except for your own testing)
// CODE WILL BE GRADED USING A MAIN FUNCTION SIMILAR TO THIS
//////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
template <typename T>
bool testAnswer(const string &nameOfTest, const T &received, const T &expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
template <typename T>
bool testAnswerEpsilon(const string &nameOfTest, const T &received, const T &expected) {
const double epsilon = 0.0001;
if ((received - expected < epsilon) && (expected - received < epsilon)) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
int main() {
{
// test only the PriceList class
PriceList priceList;
priceList.addEntry("Apples", "1000", 1.99, false);
priceList.addEntry("Bananas", "2000", 0.99, false);
testAnswer("PriceList isValid1", priceList.isValid("1000"), true);
testAnswer("PriceList isValid2", priceList.isValid("19"), false);
// test copy constructor
PriceList priceList2 = priceList;
testAnswer("PriceList copy constructor test1", priceList2.isValid("1000"), true);
priceList.addEntry("Milk", "3000", 3.49, false);
priceList2.addEntry("Eggs", "4000", 4.99, false);
testAnswer("PriceList copy constructor test2", priceList.isValid("4000"), false);
// test assignment operator
PriceList priceList3;
priceList3 = priceList;
testAnswer("PriceList assignment test1", priceList3.isValid("1000"), true);
priceList.addEntry("Orange_juice_1gal", "4500", 6.49, false);
priceList3.addEntry("Diapers_30ct", "5000", 19.99, false);
testAnswer("PriceList assignment test2", priceList.isValid("5000"), false);
}
{
// test capacity of the PriceList class
PriceList priceList;
for (int i = 1; i<100000; i++)
priceList.addEntry(string("Apples_") + to_string(i), to_string(i), 1.99, false);
testAnswer("PriceList big data1", priceList.isValid("20000"), true);
testAnswer("PriceList big data2", priceList.isValid("100000"), false);
}
system("pause");
}
======================================================
// priceListItem.h
#pragma once
#include <string>
using namespace std;
class PriceListItem {
public:
PriceListItem();
PriceListItem(const string &itemName, const string &code, double price, bool taxable);
string getItemName();
string getCode();
double getPrice();
bool isTaxable();
private:
string code;
double price;
string itemName;
bool taxable;
};
Explanation / Answer
Given below is the fixed code. The assignment operator was not overloaded. Now it has been implemented. Also in copy constructor , you need to allocate a new array and copy over the values from other . We can allocate the memory and then use assignment operator as shown in the code below. All tests passed. Please rate if it helped. thank you.
PriceList.h
//priceList.h
#pragma once
#include <string>
#include <stdexcept>
#include "PriceListItem.h"
using namespace std;
class PriceList {
public:
PriceList();
PriceList(const PriceList &list);
void createPriceListFromDatafile(string filename); // Load information from a text file with the given filename (Completed)
void addEntry(string itemName, string code, double price, bool taxable); // add to the price list information about a new item. A max of 1,000,000 entries can be added
bool isValid(string code); //const; // return true only if the code is valid
PriceListItem getItem(string code);// const; // return price, item name, taxable? as an PriceListItem object; throw exception if code is not found
PriceList& operator = (const PriceList &list2); //overloaded assignment operator
private:
PriceListItem* arr;
int numItems;
};
PriceList.cpp
//priceList.cpp
#include <cstring>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PriceList.h"
#include "PriceListItem.h"
using namespace std;
PriceList::PriceList()
{
numItems = 0;
arr = new PriceListItem[1000000];
}
PriceList::PriceList(const PriceList &list)
{
arr = new PriceListItem[1000000]; //allocate space and then copy all elements
*this = list; //use overloaded assigment operator to copy over the input parameter
}
// Load information from a text file with the given filename.
void PriceList::createPriceListFromDatafile(string filename) {
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
string code;
double price;
bool taxable;
while (myfile >> name >> code >> price >> taxable) {
// cout << code << " " << taxable << endl;
addEntry(name, code, price, taxable);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return true only if the code is valid
bool PriceList::isValid(string code) {
for (int i = 0; i < numItems; i++)
{
if (arr[i].getCode()== code)
{
return true;
}
}
return false;
}
// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found
PriceListItem PriceList::getItem(string code) {
//const
for (int i = 0; i < numItems; i++)
{
if (arr[i].getCode() == code)
{
return arr[i];
}
}
throw invalid_argument("Invalid code:"+code);
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// create item object
PriceListItem a(itemName, code, price, taxable);
// store item object in array
arr[numItems] = a;
numItems++;
}
PriceList& PriceList::operator = (const PriceList &list2)
{
if(&list2 != this) //nothing to do self assignment
{
numItems = list2.numItems;
for(int i = 0; i <list2.numItems; i++)
arr[i] = list2.arr[i];
}
return *this;
}
output
PASSED PriceList isValid1: expected and received 1
PASSED PriceList isValid2: expected and received 0
PASSED PriceList copy constructor test1: expected and received 1
PASSED PriceList copy constructor test2: expected and received 0
PASSED PriceList assignment test1: expected and received 1
PASSED PriceList assignment test2: expected and received 0
PASSED PriceList big data1: expected and received 1
PASSED PriceList big data2: expected and received 0
Note: Ideally instead of creating an array of 100000 elements, we would create a static variable of DEFAULT_CAPACITY say 100 and create array of DEFAULT_CAPACITY . When the array is full, we create a bigger array may be of double capacity and deallocate old array. Since I am not sure if it was in the instrution to create array of 1000000 elements, I just left it as it is.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.