Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I need help with getting overloaded operator to work in a template for 3 classes. My code below is working, and the output is

C++ I need help with getting overloaded operator to work in a template for 3 classes.

My code below is working, and the output is correct, but the way I am using the overloaded operator< is not correct.

One rule I must follow is : "need to Update the overloaded operator< template so it will call the individual class operator<."

The output needs to be :

Sample output: C:\csc2401>g++ -Wall PartB-3.cpp C:\csc2401>a Family My Cousins Name: Peter Age: 64 My Library Literature Name: Wuthering Heights ISBN: 921-4567-89 Sports Car From 1900 to 2014 Model: AH211 Price: 23456.8

My code :

#include #include #include

using namespace std;

template class Collection { public: Collection(); Collection(string name, string description); bool operator< (T& b); void add_item(T c); void display_best(); // 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

friend class Car; friend class Person; friend class Book; };

template Collection::Collection()

{ }

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

/* I need to modify this overloaded operator so that it can deal with calls from Car, Person, Book classes */ template bool operator<(T a, T b) { return a.get_price() < b.get_price(); // this was orignally only for the Car class, now it needs to work with Person and Book classes as well. }

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

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

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

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 << " "; }

class Person { public: Person(); Person(string n, int a); int get_age(); bool operator< (Person& b); void display()const; private: string name; int age; };

Person::Person(string n, int a) { name = n; age = a; }

int Person::get_age() { return age; }

bool operator<(Person a, Person b) { return a.get_age() < b.get_age(); }

void Person::display() const { cout << "Name: " << name << " Age: " << age << " "; }

class Book { public: Book(); Book(string n, string i); string get_isbn(); bool operator< (Book& b); void display()const; private: string name; string isbn; };

Book::Book(string n, string i) { name = n; isbn = i; }

string Book::get_isbn() { return isbn; }

bool operator<(Book a, Book b) { return a.get_isbn() < b.get_isbn(); }

void Book::display() const { cout << "Name: " << name << " ISDN: " << isbn << " "; }

int main() { Person p1("Peter", 64); Person p2("Paul", 46); Person p3("Mary", 24); Collection P("Family", "My Cousins"); P.add_item(p1); P.add_item(p2); P.add_item(p3); P.display_best(); cout << endl; Book b1("Moby Dick", "123-4567-89"); Book b2("Wuthering Heights", "921-4567-89"); Book b3("Thre Three Musketeers", "654-4567-89"); Collection b("My Library", "Literature"); b.add_item(b1); b.add_item(b2); b.add_item(b3); b.display_best(); cout << endl; Car c1("MQ310", 12345.99); Car c2("AH211", 23456.78); Car c3("ZH42", 3456.49); Collection c("Sports Car", "From 1900 to 2014"); c.add_item(c1); c.add_item(c2); c.add_item(c3); c.display_best(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

Students also viewed these Databases questions

Question

2. Recruitment and competence analysis.

Answered: 1 week ago

Question

3. Write a policy statement to address these issues.

Answered: 1 week ago