Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3587517 • Letter: C

Question

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 <iostream>
#include <queue>
#include <string>


using namespace std;

template<typename T>
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 <T> task;
   string name; // Name of the collection
   string description; // Descriptions of the collection

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


template<typename T>
Collection<T>::Collection()

{
}

template<typename T>
Collection<T>::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<typename T>
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<typename T>
void Collection<T>::add_item(T c)
{
    task.push(c);
}

template<typename T>
void Collection<T>::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<Person> 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<Book> 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<Car> c("Sports Car", "From 1900 to 2014");
      c.add_item(c1);
      c.add_item(c2);
      c.add_item(c3);
      c.display_best();
      return 0;
}

Explanation / Answer

#include <iostream>
#include <queue>
#include <string>

using namespace std;

template<typename T>
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 <T> task;
string name; // Name of the collection
string description; // Descriptions of the collection
friend class Car;
friend class Person;
friend class Book;
};

template<typename T>
Collection<T>::Collection()
{
}
template<typename T>
Collection<T>::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<typename T>
bool operator<(T a, T b)
{
return a < b; // this was orignally only for the Car class, now it needs to work with Person and Book classes as well.
}
template<typename T>
void Collection<T>::add_item(T c)
{
task.push(c);
}
template<typename T>
void Collection<T>::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;
};

bool operator<(Car a, Car c) {

return a.get_price() < c.get_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<Person> 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<Book> 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<Car> c("Sports Car", "From 1900 to 2014");
c.add_item(c1);
c.add_item(c2);
c.add_item(c3);
c.display_best();
return 0;
}