21
Chapter-15 Operator Overloading Abu Saleh Musa Miah M.Sc. Engg(On going) University of Rajshahi

Lecture 5, c++(complete reference,herbet sheidt)chapter-15

Embed Size (px)

Citation preview

Chapter-14

Chapter-15 Operator Overloading

Abu Saleh Musa MiahM.Sc. Engg(On going)University of Rajshahi

Operator OverloadingAn operator function is created using the keyword operator. Operator functions can be either members or nonmembers of a class. Nonmember operator functions are almost always friend functions of the class

Creating a Member Operator FunctionA member operator function takes this general form:ret-type class-name::operator#(arg-list){// operations}Operator functions return an object of the class they operate on,

But ret-type can be any valid type.

The # is a placeholder. When you create an operator function, substitute the operator for the #.

First example of operator overloading.#include using namespace std;class loc {int longitude, latitude;public:loc() {}loc(int lg, int lt) {longitude = lg;latitude = lt; }void show() {cout