Question: C++ Code Problem A) (20 Points) Implement the Big Three for the CarMakeInventory Class and use them In lecture we will discuss the Course class

C++ Code

Problem A) (20 Points) Implement the Big Three for the CarMakeInventory Class and use them In lecture we will discuss the Course class to demonstrate the use of dynamic memory in classes, and the necessity of implementing a copy constructor, a destructor, and an overloaded version of the assignment operator (that is, the big three) when dynamic memory is used in a class. The Course Class and an example of its use has been posted on the class Moodle site, and we will go over it in Lecture. However, for this problem you will implement the big three in similar fashion as they are implemented in the Course class for a different class, the CarMakeInventory class.

NOTE, there are differences between the two classes (Course and CarMakeInventory). You will have to account for the differences in your implementation of the methods, operators, and functions for the CarMakeInventory class.

Constraints: Specifically, in this assignment you will add the necessary C++ code to the C++ code that we posted on the class Moodle site for this assignment by defining and using following methods, operators, and functions for the CarMakeInventory class: 1) A Copy Constructor creates a copy of an object of type CarMakeInventory when it is passed by value to a function, or when a programmer wants to make a copy of an existing CarMakeInventory object by calling the copy constructor explicitly. 2) A Destructor which deallocates the memory dynamically that was dynamically allocated when an instance 3 destructor is called when an object is deleted, when returning from a function that has a call-by-value parameter that is an object, and in other cases). 3) An overloaded assignment operator, used when assigning one object of type CarMakeInventory to another object of type CarMakeInventory. 4) An overloaded > (greater than) relational operator 5) A function that prints out the information stored in an object of class CarMakeInventory 6) A sort function that sorts an array of CarMakeInventory objects using the overloaded assignment operator (=) and overloaded > (greater-than) operator that you define for the class CarMakeInventory.

The C++ Code you are to augment for this assignment is in the file CarMakeInventory.cpp, and available on Moodle, with this assignment description. The declaration of CarMakeInventory Class and the functions you need to implement from the file CarMakeInventory.cpp in the HW 10 Assignment folder on Moodle is shown below. Note, the comments in the class definition for CarMakeInventory below describe what we have provided, and what we require you to implement.

class CarMakeInventory { public: CarMakeInventory(string name = "", int size=15); // Initializes the CarMakeInventory object so it can accept "size" model names // and list prices. // Sets the name of the CarMakeInventory object to name.

CarMakeInventory(const CarMakeInventory &acourse); //Copy Constructor - you define this

~CarMakeInventory( ); //Destructor - you define this //Returns all the dynamic memory used by the CarMakeInventory object to the freestore.

void operator =(const CarMakeInventory& rightSide); //Overloads the == operator, you define this friend bool operator > (CarMakeInventory& cone, CarMakeInventory& ctwo); //the overloaded > operator as a friend function, you define this // methods to set and get the Inventory name for a particular car Make, // and to set and get Model Name and List Price void setName(string aname); string getName(); void setModel(int location, string mName); string getModel(int location); float getPrice(string mName); void setPrice (string id, float price); float sumMakePrices(); // returns the sum of the List Prices //for each price stored in the Model array int CarMakeInventorySize; // stores count of Models for a partcular make of car private: string Make; // Stores the make of car (e.g, Ford, Acura,...) string *Model; // pointer to dynamic array to store Model Names //for a particular Make float *Price; // pointer dynamic array to store the Price // for each car Model (e.g., Taurus, Mustang) // for a particular Make of car (e.g., Ford) 4 }; // function to print out Inventory information (Make, each Model, and price // of each Model for a particular Make of car. // Note, you can redeclare this as a friend function, // but its parameter aCarMakeInventory must remain pass by value // (it will test your copy constructor) // // You define this! void pCarMakeInventoryInfo(CarMakeInventory aCarMakeInventory); // function to sort Car Make Inventory into ascending order based on the sum of // List Prices of the Models for a Make. // The function must use the overloaded > operator for the CarMakeInventory class, and will use // the overloaded assignment operator // // You define this! void sortInventory(CarMakeInventory CarMakeInventoryList[], int num);

The main program in the file CarMakeInventory.cpp program reads in a data file with the data in following format: The first line in the file specifies the number of Makes of Car in the file. The file contains the following information for each Make in the file: The Make of Car (a string) e.g., Ford The number of Models (an integer) The name of each Model in the Dealers Inventory (e.g., Mustang, Taurus, ) The list price of each Model(real numbers, separated by spaces, on a single line) You can assume that the file will always be correctly formatted. An example data file for you to use as you, design, develop and test your methods, operations, and functions for program is available on Moodle, in the file named CarInventory.dat, along with this assignment description.

Below is a listing of the data file: CarInventory.dat 5 BMW 3 330i 530i 740e 45000.00 49000.50 100000.75 Acura 5 TLX RLX MDX ILX NSX 30000.75 75000.25 43000.30 28000.50 150000.00 Porsche 2 Macan MacanS 50000.75 55000.50 Toyota 7 Sienna Camry Corolla Rav4 Yaris Avalon X6 30725.00 25650.25 19425.075 33025.00 15635.00 33250.75 26255.75 Ford 4 Mustang Taurus Explorer FocusRS 25680.75 27690.50 32140.00 41120.00 5

Make sure to use the following main program (also found in CarMakeInventory.cpp in the HW 10 Assignment folder on Moodle) to test your CarMakeInventory class. //Program to demonstrate (and test) the use of the class CarMakeInventory. int main( ) { CarMakeInventory *ListOfCarMakes[10]; //an array of pointers string filename; string make; ifstream infile; int modelCount; string Model; float Price; cout << "Enter input file name: "; cin >> filename; infile.open(filename.c_str()); if (infile.fail()) { cout << "Can not open file: " << filename << endl; exit(1); } int CarMakeInventoryCnt; infile >> CarMakeInventoryCnt; // get the number of different Car Makes // stored in the input file for (int i = 0; i < CarMakeInventoryCnt; i++) { // read in a Car Make name infile >> make; // read in the number of different Models infile >> modelCount; ListOfCarMakes[i] = new CarMakeInventory(make, modelCount); // instantiate a Make // object and put it // on the list of Car // Makes in the Inventory for (int j=0; j < modelCount; j++){ // get and store the Model names for each Make infile >> Model; ListOfCarMakes[i]->setModel(j, Model); } for (int k=0; k < modelCount; k++){ //get and store the List Price for each Model //in the CarMakeInventory infile >> Price; ListOfCarMakes[i]->setPrice(ListOfCarMakes[i]->getModel(k), Price); } } cout << "Inventory information obtained from file: " << filename << endl; //Print out the information the program just obtained //from the input file (CarInventory.dat) // //The copy constructor you wrote will be tested here, as will //The destructor you wrote for (int i = 0; i < CarMakeInventoryCnt; i++) pCarMakeInventoryInfo(*ListOfCarMakes[i]); cout << endl << endl; //Create an array of CarMakeInventory Objects CarMakeInventory CarMakeObjects[10]; // note, this is NOT dynamically allocated (The code continues on the next page, this message is not part of it) 6 //Assign the contents of the objects pointed to by the ListOfCarMakes array //to the array of in CarMakeObjects. //your overloaded assignment operator //will be tested here. for (int i = 0; i < CarMakeInventoryCnt; i++) CarMakeObjects[i] = *ListOfCarMakes[i]; // now, sort the CarMakeInventory objects stored // in the CarMakeObjects array into // ascending order based on the sum of the model list prices // for each CarMakeInventory object. // The overloaded assignment operator and // the overloaded > operator should be // used by the sort you write sortInventory(CarMakeObjects, CarMakeInventoryCnt); cout << "Car Make Inventory Information Listed by Sum of List Prices for each Make" << endl; cout << "(in Ascending Order) "; for (int i = 0; i < CarMakeInventoryCnt; i++) pCarMakeInventoryInfo(CarMakeObjects[i]); return 0;

Finally, below is an example of what your output should look like when you program is run with the file CarMakeInventory.dat (user input is in bold): Enter input file name: CarInventory.dat Inventory information obtained from file: CarInventory.dat Make: BMW Model: 330i List Price: 45000.00 Model: 530i List Price: 49000.50 Model: 740e List Price: 100000.75 Sum of Model List Prices: 194001.25 Make: Acura Model: TLX List Price: 30000.75 Model: RLX List Price: 75000.25 Model: MDX List Price: 43000.30 Model: ILX List Price: 28000.50 Model: NSX List Price: 150000.00 Sum of Model List Prices: 326001.81 Make: Porsche Model: Macan List Price: 50000.75 Model: MacanS List Price: 55000.50 Sum of Model List Prices: 105001.25 Make: Toyota Model: Sienna List Price: 30725.00 Model: Camry List Price: 25650.25 Model: Corolla List Price: 19425.07 Model: Rav4 List Price: 33025.00 Model: Yaris List Price: 15635.00 Model: Avalon List Price: 33250.75 Model: X6 List Price: 26255.75 Sum of Model List Prices: 183966.83 Make: Ford Model: Mustang List Price: 25680.75 Model: Taurus List Price: 27690.50 Model: Explorer List Price: 32140.00 Model: FocusRS List Price: 41120.00 Sum of Model List Prices: 126631.25 7 Car Make Inventory Information Listed by Sum of List Prices for each Make (in Ascending Order) Make: Porsche Model: Macan List Price: 50000.75 Model: MacanS List Price: 55000.50 Sum of Model List Prices: 105001.25 Make: Ford Model: Mustang List Price: 25680.75 Model: Taurus List Price: 27690.50 Model: Explorer List Price: 32140.00 Model: FocusRS List Price: 41120.00 Sum of Model List Prices: 126631.25 Make: Toyota Model: Sienna List Price: 30725.00 Model: Camry List Price: 25650.25 Model: Corolla List Price: 19425.07 Model: Rav4 List Price: 33025.00 Model: Yaris List Price: 15635.00 Model: Avalon List Price: 33250.75 Model: X6 List Price: 26255.75 Sum of Model List Prices: 183966.83 Make: BMW Model: 330i List Price: 45000.00 Model: 530i List Price: 49000.50 Model: 740e List Price: 100000.75 Sum of Model List Prices: 194001.25 Make: Acura Model: TLX List Price: 30000.75 Model: RLX List Price: 75000.25 Model: MDX List Price: 43000.30 Model: ILX List Price: 28000.50 Model: NSX List Price: 150000.00 Sum of Model List Prices: 326001.81 Test and revise your program until you are sure it is correct. Use more than the values we have specified in the example run above, as we will be testing your program with more than one set of values.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!