To make sure the passed argument is not modied in the

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: le] ­hand operand is bound to the object on which it is invoked. Overloading Operators •  Operators are func/ons, which can be defined inside a class (member func/on) or outside a class. •  Just as func/on can be overloaded using different signatures, operators can be overloaded using different operands. bool Book::operator==(Book other) { //return 1 if book and other are the same. return (year_v==other.year() && (name_v.compare (other.name()) == 0)); } bool Book::operator==(string otherTitle) bool Book::operator==(int otherYear) Operator func/ons •  To make sure the argument is passed more efficiently, pass reference using &. •  To make sure the passed argument is not modified in the func/on, add const. •  bool Book::operator==(const Book& other); bool Book::operator==(Book other); if(book1==book2) … //first operand is book1, other is book2 3 10/5/12 Overloading iostream operators •  This is defined...
View Full Document

This document was uploaded on 03/06/2014 for the course CS 3710 at Memorial University.

Ask a homework question - tutors are online