COM SCI 32 Lecture Notes - Lecture 12: Linked List, Iterator, Standard Template Library

64 views30 pages

Document Summary

You can de(cid:210)ne a comparison operator for a class/struct like this: class dog public: int getweight() const return m_weight; private: int m_weight; bool operator>=(const dog &a, const dog &b) if(a. getweight() >= b. getweight()) return true; return false; You can de(cid:210)ne >, =, <=, ==, != All comparison operators must return a boolean value. Your comparison function should compare object a against object b using whatever approach makes sense. Here we say dog a is greater than dog b if its weight is bigger. All comparison operators must accept constant reference parameters. You can de(cid:210)ne your comparison operator inside or outside the class. Since the operator function above is de(cid:210)ned outside the class, it cannot access private member variables. In c++, we use c++"s template function to solve this problem. Item temp; temp = a; a = b; Always place your templated functions in a header (cid:210)le.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents