Question
This lab will test your ability to perform deep and shallow copies, overload operators, friendship, and use the copy constructor and assignment as well as
This lab will test your ability to perform deep and shallow copies, overload operators, friendship, and use the copy constructor and assignment as well as the move constructor and assignment.You have to write three files.
This program creates objects of type House. Each house has an owner, address, rooms, bathrooms, and a price. The class functions that operate on this data are as follows:
House();//Default constructor
House(const char* _owner, const char* _address, const double _rooms, const double _bathrooms, const double _price);//Data for the house object
House(const House& house);//Copy constructor
House& operator=(const House& house);//Copy assignment
House(House&& house) noexcept;//Move constructor
House& operator=(House&& house) noexcept;//Move assignment
bool ChangeOwner(char* newOwner);//Change the owner. Test to see if the character array newOwner actually exists. If it doesn't, return false, otherwise return true.
bool ChangePrice(double newPrice);//Change the price. Test to see if the price is a positive number. If it isn't, return false, otherwise return true.
const House& operator+=(double amount);//increase/decrease the price of the house
void DisplayInfo() const;//Display the owner, address, rooms, bathrooms, and price. All double's should be printed to two decimal places.
~House();//Destructor
There are also two functions that operate on this data from outside the class. They are as follows:
bool operator==(const House& house1, const House& house2);//Do the two houses have the same owner?
void operator>>(House& house1, House& house2);//Move contents from the house1 object to the house2 object
Note that operator== might require access to private members of the House object, so you may have to modify House.h. Also, you might decide that operator== and operator>> can be implemented within the class. You are free to move them to within the class if you wish.
You have been given a header file for House and a main file for testing: House.h and HouseMain.cpp. Complete House.cpp.You have to write three files.
A sample run is as follows:
Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $2500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $5600000.00 Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $2500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $5600000.00 ---------------------------- Owner: none Address: none Rooms: 0.00 Bathrooms: 0.00 Price: $0.00 Owner: none Address: none Rooms: 0.00 Bathrooms: 0.00 Price: $0.00 Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $2500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $5600000.00 Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $2500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $5600000.00 ---------------------------- Houses 3 and 5 are the same Houses 4 and 6 are the same ---------------------------- Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $3500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $6600000.00 Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $2500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $5600000.00 ---------------------------- Owner: none Address: none Rooms: 0.00 Bathrooms: 0.00 Price: $0.00 Owner: none Address: none Rooms: 0.00 Bathrooms: 0.00 Price: $0.00 Owner: Lionel Messi Address: 34 Cedarwood Road Rooms: 5.00 Bathrooms: 2.50 Price: $3500000.00 Owner: Neymar da Silva Santos Junior Address: 12 Broadway Street Rooms: 7.00 Bathrooms: 4.00 Price: $6600000.00
To assist you with this lab, you might want to look at the following sample code taken from our SEP101 final exam. A Warehouse class is implemented using operator overloading and copy/move constructor and assignment operators: Warehouse.h, Warehouse.cpp and WarehouseMain.cpp.
Sample Code:
//Warehouse.h - class declaration for a warehouse #ifndef _WAREHOUSE_H_ #define _WAREHOUSE_H_ #includestruct WarehouseItem { std::string name; std::string type; int barcode; double price; }; class Warehouse { std::string name = ""; std::string address = ""; WarehouseItem* item = nullptr; int numItems = 0; const int MAX_ITEMS = 15; public: Warehouse() {}//default constructor Warehouse(std::string name, std::string address, WarehouseItem _item[], int num); Warehouse(const Warehouse& warehouse);//copy constructor Warehouse(Warehouse&& warehouse) noexcept ;//move constructor Warehouse& operator=(const Warehouse& warehouse);//copy assignment Warehouse& operator=(Warehouse&& warehouse) noexcept;//move assignment void SetAddress(std::string newAddr) { address = newAddr; }//change the address Warehouse& operator+=(WarehouseItem& _item);//add an item Warehouse& operator-=(std::string name);//remove an item void DisplayItems(std::ostream& os) const;//display all items ~Warehouse(); }; std::ostream& operator<<(std::ostream& os, const Warehouse& warehouse); #endif _WAREHOUSE_H_
//Warehouse.cpp - function definitions for the warehouse #include "Warehouse.h" using namespace std; Warehouse::Warehouse(std::string name, std::string address, WarehouseItem item[], int num) { this->name = name; this->address = address; if (num > 0 && num <= MAX_ITEMS) { numItems = num; this->item = new WarehouseItem[numItems]; for (int i = 0; i < numItems; ++i) { this->item[i].name = item[i].name; this->item[i].type = item[i].type; this->item[i].barcode = item[i].barcode; this->item[i].price = item[i].price; } } else { numItems = 0; this->item = nullptr; } } Warehouse::Warehouse(const Warehouse& warehouse) {//copy constructor *this = warehouse;//will invoke the copy assignment } Warehouse::Warehouse(Warehouse&& warehouse) noexcept {//move constructor *this = std::move(warehouse);//will invoke the move assignment } Warehouse& Warehouse::operator=(const Warehouse& warehouse) {//copy assignment if (this != &warehouse) {//don't copy to yourself //shallow copies name = warehouse.name; address = warehouse.address; numItems = warehouse.numItems; //deep copy any resources //remove existing resourse delete[] item; if (numItems > 0) { //allocate for a new resource and copy data over item = new WarehouseItem[numItems]; for (int i = 0; i < numItems; ++i) { item[i].name = warehouse.item[i].name; item[i].type = warehouse.item[i].type; item[i].barcode = warehouse.item[i].barcode; item[i].price = warehouse.item[i].price; } } else { item = nullptr; } } return *this; } Warehouse& Warehouse::operator=(Warehouse&& warehouse) noexcept {//move assignment if (this != &warehouse) {//don't move to yourself //shallow copies name = warehouse.name; address = warehouse.address; numItems = warehouse.numItems; //remove existing resource delete[] item; //take control of the resource on the rhs item = warehouse.item; //Put the rhs into an empty state warehouse.name = ""; warehouse.address = ""; warehouse.numItems = 0; warehouse.item = nullptr; } return *this; } Warehouse& Warehouse::operator+=(WarehouseItem& item) { if (numItems < MAX_ITEMS) { WarehouseItem* newItem = new WarehouseItem[numItems + 1]; for (int i = 0; i < numItems; ++i) { newItem[i].name = this->item[i].name; newItem[i].type = this->item[i].type; newItem[i].barcode = this->item[i].barcode; newItem[i].price = this->item[i].price; } newItem[numItems].name = item.name; newItem[numItems].type = item.type; newItem[numItems].barcode = item.barcode; newItem[numItems].price = item.price; // if (this->item != nullptr)delete[] this->item;//the comparison is not necessary delete[] this->item; this->item = newItem; ++numItems; } else { cout << "Error: maximum items reached. Aborting..." << endl; } return *this; } Warehouse& Warehouse::operator-=(std::string name) { bool found = false; for (int i = 0; i < numItems && !found; ++i) { if (item[i].name == name) { found = true; WarehouseItem* newItem = new WarehouseItem[numItems - 1]; for (int j = 0; j < i; ++j) {//'i' is 3 newItem[j].name = item[j].name; newItem[j].type = item[j].type; newItem[j].barcode = item[j].barcode; newItem[j].price = item[j].price; } for (int j = i; j < numItems - 1; ++j) {//skips over item 3 newItem[j].name = item[j + 1].name; newItem[j].type = item[j + 1].type; newItem[j].barcode = item[j + 1].barcode; newItem[j].price = item[j + 1].price; } // if (item != nullptr)delete[] item;//the comparison is not necessary delete[] item; item = newItem; --numItems; } } if (!found) cout << "Error: item " << name << " not found. Could not remove it." << endl; return *this; } Warehouse::~Warehouse() { if (item != nullptr) { delete[] item; item = nullptr; } } void Warehouse::DisplayItems(ostream& os) const { os << endl; os << name << " " << address << endl; os << "LIST OF ITEMS:" << endl; for (int i = 0; i < numItems; ++i) { os.setf(ios::fixed); os.precision(2); os << item[i].name << ", " << item[i].type << ", " << item[i].barcode << ", $" << item[i].price << "." << endl; } os << endl; } std::ostream& operator<<(std::ostream& os, const Warehouse& warehouse) { warehouse.DisplayItems(os); return os; }
//WarehouseMain.cpp - main function for the warehouse #include "Warehouse.h" using namespace std; int main() { const int NUM = 13; WarehouseItem item[NUM] = { {"Hammer1", "hammer", 1000123, 11.99}, {"Screwdriver1", "screwdriver", 1000124, 8.99}, {"Wrench1", "wrench", 1000125, 12.99}, {"Extension Cord", "anonymous", 1000126, 6.99}, {"Hammer2", "hammer", 1000127, 12.99}, {"Screwdriver2", "screwdriver", 1000128, 10.99}, {"Wrench2", "wrench", 1000129, 15.99}, {"Stud Finder", "anonymous", 1000130, 26.99}, {"Hammer3", "hammer", 1000131, 9.99}, {"Screwdriver3", "screwdriver", 1000132, 7.99}, {"Wrench3", "wrench", 1000133, 14.99}, {"Plaster", "anonymous", 1000134, 4.99}, {"Hammer4", "hammer", 1000135, 10.49}, }; Warehouse warehouse1("Home Depot", "2920 Argentia Road, Mississauga", item, NUM); cout << "Warehouse1: " << warehouse1; WarehouseItem newItem1 = { "Shovel", "anonymous", 1000136, 17.99 }; WarehouseItem newItem2 = { "Hammer5", "hammer", 1000137, 13.99 }; WarehouseItem newItem3 = { "Wrench4", "wrench", 1000138, 12.39 }; warehouse1 += newItem1; warehouse1 += newItem2; warehouse1 += newItem3; cout << "Warehouse1: " << warehouse1; warehouse1 -= "Screwdriver3"; cout << "Warehouse1: " << warehouse1; //test the copy constructor Warehouse warehouse2(warehouse1); warehouse2.SetAddress("5975 Terry Fox Way, Mississauga"); warehouse2 -= "Wrench3";//change something cout << "Warehouse2: " << warehouse2; //test the copy assignment Warehouse warehouse3; warehouse3 = warehouse2; warehouse3.SetAddress("3065 Mavis Rd, Mississauga"); warehouse3 -= "Plaster"; cout << "Warehouse3: " << warehouse3; //test the move constructor, move everything from warehouse1 to warehouse4 Warehouse warehouse4(std::move(warehouse1)); warehouse4.SetAddress("99 Cross Ave, Oakville"); cout << "Warehouse1: " << warehouse1; cout << "Warehouse4: " << warehouse4; //test the move assignment, move everything from warehouse2 to warehouse5 Warehouse warehouse5; warehouse5 = std::move(warehouse2); warehouse5.SetAddress("49 First Gulf Blvd, Brampton"); cout << "Warehouse2: " << warehouse2; cout << "Warehouse5: " << warehouse5; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started