Question: I cannot get my code below to compile.. The idea is to Turn the class Collection into a template class so that it can be

I cannot get my code below to compile..

The idea is to Turn the class Collection into a template class so that it can be used for Car as well as Book, Person and other classes. You have to convert the overloaded operator < to a template function. Test run the program and make sure that you still get the output as: Sample output: C:\csc2401>g++ -Wall PartB-2.cpp C:\csc2401>a Sports Car From 1900 to 2014 Model: AH211 Price: 23456.8

My Code :

#include #include #include

using namespace std;

class Car { public: Car(); Car(string model, double price); string get_model(); double get_price(); void display()const; private: string model; double price; };

template class Collection { public: Collection(); Collection(string name, string description); void add_item(T c); // Was add_item(Car c)... used to Add Car c to priority_queue task. void display_best(); // Display information of the Best car. // Use price to determine best. // Higher price means better. private: priority_queue task; string name; // Name of the collection string description; // Descriptions of the collection };

Car::Car(string m, double p) { model = m; price = p; }

string Car::get_model() { return model; }

double Car::get_price() { return price; }

void Car::display() const { cout << "Model: " << model << " Price: " << price << " "; }

template Collection::Collection()

{ }

template Collection::Collection(string n, string d) { name = n; description = d; }

template bool operator<(T a, T b) //was operator<(Car a, Car b) { return a.get_price() < b.get_price(); }

template void Collection::add_item(T c) // was add_item(Car c) { task.push(c); }

template void Collection::display_best() { cout << name << " " << description << " "; Car tasks = task.top(); task.pop(); tasks.display(); }

//template class Collection;

int main() { // Instantiate 3 car objects Car c1("MQ310", 12345.99); Car c2("AH211", 23456.78); Car c3("ZH42", 3456.49); // Instantiate a Collection object Collection c("Sports Car", "From 1900 to 2014"); // Collection c(""); // Add 3 car objects to the car collection c.add_item(c1); c.add_item(c2); c.add_item(c3); // Display the information of the best car. c.display_best(); return 0; }

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!