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
using namespace std;
template
friend class Car; friend class Person; friend class Book; };
template
{ }
template
/* I need to modify this overloaded operator so that it can deal with calls from Car, Person, Book classes */ template
template
template
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started