C++ Code Problem A) (20 Points) Implement the Big Three for the CarMakeInventory
ID: 3713652 • Letter: C
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 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.
Explanation / Answer
SourceCode:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstddef>
#include <cstring>
using namespace std;
class Company
{
public:
Company(string name, int size);
//Initializes the company object so it can accept "size" employee ids.
// Sets the name of the company object to name.
Company( );
//Initializes the company object so it can accept up to 15 employee id's of
//type int, and sets the name of the course to the empty string.
Company(const Company &acompany);
//Copy Constructor - you define this
~Company( );
//Destructor - you define this
//Returns all the dynamic memory used by the company object to the freestore.
void operator =(const Company& rightSide);
//Overloads the == operator, you define this
friend bool operator > (Company& cone, Company& ctwo);
//the overloaded > operator as a friend function, you define this
// methods to set and get the company name, and to set and get employee id numbers and salaries
void setName(string aname);
string getName();
void setEid(int location, int idValue);
int getEid(int location);
float getSalary(int id);
void setSalary (int id, float salary);
int CompanySize; //Number of employees that work for the company
private:
string Cname; // Name of the Company
int *Eid; //pointer to dynamic array that holds the values of the Employee ids
float *Esalary; // dynamic array that holds the salaries for each employess
float getPayroll(); // returns the company payroll (sum of the salaries in the Esalary array).
// Useful for implementing the > operator
}; // end definition of Company interface
// function to print out company information
void pCompanyInfo(Company acompany);
// function to sort companies into ascending order based on the total company payroll
void sortCompanies(Company CompanyList[], int num);
//Program to demonstrate (and test) the use of the class Company.
int main( ) {
Company *Clist[10]; // Array of pointers to Company Objects
ifstream infile; // The file pointer to the data file
char filename[30]; // Name of the file with the Company Data
string cname; // read the name of the company into cname
int csize; //read the number of employees into csize
int eid; // read an employee id into eid
float esalary; // read a salary into esalary
cout << "Enter input file name: ";
cin >> filename;
infile.open(filename);
if (infile.fail()) {
cout << "Can not open file: " << filename << endl;
exit(1);
}
int companyCnt;
infile >> companyCnt; // get the number of companies stored in the file
for (int i = 0; i < companyCnt; i++) {
// read in a company name
infile >> cname;
// read in a company size
infile >> csize;
Clist[i] = new Company(cname, csize); // instantiate a company and put it on the list of companies
for (int j=0; j < csize; j++){ // get and store the employee ids for the company
infile >> eid;
Clist[i]->setEid(j, eid);
}
for (int k=0; k < csize; k++){ //get and store the employee salaries for each employee in the company
infile >> esalary;
Clist[i]->setSalary(Clist[i]->getEid(k), esalary);
}
} // end reading data from input file
cout << endl << "Company information" << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint); // set decimal places
cout.precision(2);
//Print out the information the program just obtained from the file
for (int i = 0; i < companyCnt; i++)
pCompanyInfo(*Clist[i]);
cout << endl << endl;
//Create an array of Company Objects
Company CompObjects[10];
for (int i = 0; i < companyCnt; i++)
CompObjects[i] = *Clist[i];
// now, sort the company objects into ascending order
sortCompanies(CompObjects, companyCnt);
cout << "Company Information in Ascending Order by Total Payroll" << endl;
for (int i = 0; i < companyCnt; i++)
pCompanyInfo(CompObjects[i]);
return 0;
} // end main
// function to print out company information - you define this
void pCompanyInfo(Company acompany) {
cout << " Company Name: " << acompany.getName() << endl;
for (int i = 0; i < acompany.CompanySize; i++)
cout << "Employee: " << i << " id number: " << acompany.getEid(i) << " Salary: " << acompany.getSalary(acompany.getEid(i)) << endl;
}
//function to sort the companies stored in the CompanyList into
//order of total payroll (that is, lowest total payroll to highest)
//you define this
void sortCompanies(Company CompanyList[], int num){
for (int i = num - 1; i > 0; i--)
for (int j = 0; j < i; j++) {
if (CompanyList[j] > CompanyList[j + 1]) {
Company temp;
temp = CompanyList[j]; // overloaded assignment operator is used here
CompanyList[j] = CompanyList[j + 1];
CompanyList[j + 1] = temp;
}
}
}
//Method Definitons for Company
Company::Company()
{
CompanySize = 15;
Cname="";
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
}
Company::Company(string name, int size)
{
CompanySize = size;
Cname = name;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
}
//Copy Constructor - you define this
Company:: Company(const Company & acompany)
{
CompanySize = acompany.CompanySize;
Cname = acompany.Cname;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
for (int i = 0; i < CompanySize; i++) {
Eid[i] = acompany.Eid[i];
Esalary[i] = acompany.Esalary[i];
}
}
// destructor - you implement this
Company::~Company()
{
delete [] Eid;
delete [] Esalary;
}
void Company::setName(string aname)
{
Cname = aname;
}
string Company::getName()
{
return Cname;
}
void Company::setEid(int location, int idValue)
{
if (location >= 0 && location < CompanySize)
Eid[location] = idValue;
else
cout << "Error: Max Employese: " << CompanySize << " Exceeded" << endl << endl;
}
int Company::getEid(int location)
{
return Eid[location];
}
void Company::setSalary(int id, float Value)
{
bool found = false;
for (int i = 0; i < CompanySize; i++)
if (Eid[i] == id) {
Esalary[i] = Value;
found = true;
break;
}
if (!found)
cout << "Error: could not set salary for employee: " << id << endl;
return;
}
float Company::getSalary(int id) {
for (int i = 0; i < CompanySize; i++)
if (Eid[i] == id)
return (Esalary[i]);
cout << "Error: could not find salary for employee: " << id << endl;
return 0.0;
}
//Overloaded assignment operator - you implement this
void Company::operator =(const Company& right_side)
{
if (right_side.Eid != Eid || right_side.Esalary != Esalary) {
if (Eid != 0)
delete [] Eid;
if (Esalary != 0)
delete [] Esalary;
CompanySize = right_side.CompanySize;
Cname = right_side.Cname;
Eid = new int [CompanySize];
Esalary = new float [CompanySize];
for (int i = 0; i < CompanySize; i++) {
Eid[i] = right_side.Eid[i];
Esalary[i] = right_side.Esalary[i];
}
}
}
// This might be useful for overloading the > operator for the Company Class
float Company::getPayroll() {
float total = 0;
for (int i = 0; i < CompanySize; i++)
total += Esalary[i];
return total;
}
// The overloaded > operator for the course class, you are to implement this.
bool operator > (Company& cone, Company& ctwo) {
return (cone.getPayroll() > ctwo.getPayroll()); // return true if this condition is satisfied
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.