Lecture 9 - Generic Programming

Lecture 9 - Generic Programming - Wednesday, February 8th...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Wednesday, February 8th Custom Comparison Operators Templates The Standard Template Library (STL) STL Iterators STL Algorithms (find, find_if, sort, etc) Generic Programming! Generic Programming In this lecture, were going to learn about Generic Programming The goal of GP is to build algorithms that are able to operate on many different types of data (not just a single type). For example, a sort function that doesnt just sort ints but can sort strings , ints , Student objects , etc. Or a linked list class that doesnt just hold Students , but can hold Students , ints , Robots , etc. Once you define such a generic function or class, you can quickly reuse it to solve many different problems . Part 1: Allowing Generic Comparisons Consider the following main function that compares various objects to each other Notice that the way we compare two dogs (by weight) is different than the way we compare two circles (by radius) . main() { int i1 = 3, i2 = 5; if (i1 > i2) cout << i1 is bigger; Circ a(5), b(6); if (a. radius () > b. radius ()) cout << a was bigger; Dog fido(10), spot(20); if (fido. weight () > spot. weight ()) cout << fido is bigger; } Wouldnt it be nice if we could compare objects like circles and dogs just like we compare two integers? if (a > b) if (fido > spot) cout << fido is bigger; We can! Lets see how! Custom Comparison Operators You can define custom operators for classes and structs either inside your class, like this class Dog { public: bool operator<(const Dog &other) { if (m_weight < other.m_weight) return true; return false; // otherwise } int getWeight() const { return m_weight; } private: int m_weight; }; Or outside your class, like this bool operator>=( const Dog &a , const Dog &b ) { if (a.getWeight() >= b.getWeight()) return true; return false; // otherwise } NOTE: If you define the operator outside of the class, it may only use public methods from your class! All comparison operators must return a Boolean value: true or false Comparison operators defined inside the class have a single other parameter like the assignment operator. Comparison operators defined outside the class have two parameters, one for each of the two operands. if ( a >= b ) cout << a is >= b\n; Since Im defined outside the class, I can only use public methods like getWeight()!...
View Full Document

Page1 / 67

Lecture 9 - Generic Programming - Wednesday, February 8th...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online