friends of classes operator...

16
Friends of Classes Operator overloading Spring 2019 CS250 - Intro to CS II 1

Upload: others

Post on 02-Jun-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Friends of ClassesOperator overloading

Spring 2019 CS250 - Intro to CS II 1

Page 2: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Operator Overloading

• C++ allows overloading operators to work with classes

• What are some C++ operators?

Spring 2019 CS250 - Intro to CS II 2

https://en.cppreference.com/w/cpp/language/operators

Page 3: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

operator> GreaterThan

Spring 2019 CS250 - Intro to CS II 3

class SimpleRectangle // SimpleRectangle.h{ public:SimpleRectangle(double length, double width);bool operator>(const SimpleRectangle &rcRHS) const;

private:double mLength, mWidth;

};

// SimpleRectangle.cppbool SimpleRectangle::operator>(const SimpleRectangle &rcRHS) const{return mLength * mWidth > rcRHS.mLength * rcRHS.mWidth;

}

Page 4: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Usage

Spring 2019 CS250 - Intro to CS II 4

int main(){SimpleRectangle cBoxOne(100.0, 20.5);SimpleRectangle cBoxTwo(50.1, 25.3);

if( cBoxOne > cBoxTwo ){

cout << “BoxOne is bigger!”;}

// ???? What happens here?if( cBoxOne > 100.0 ){

cout << “BoxOne is bigger than 100.0!”;}

}

Page 5: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

operator> GreaterThan

Spring 2019 CS250 - Intro to CS II 5

class SimpleRectangle // SimpleRectangle.h{ public:SimpleRectangle(double length, double width);bool operator>(const SimpleRectangle &rcRHS) const;bool operator>(double area) const;

private:double mLength, mWidth;

};

// SimpleRectangle.cppbool SimpleRectangle::operator>(double area) const{return mLength * mWidth > area;

}

Page 6: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Usage

Spring 2019 CS250 - Intro to CS II 6

int main(){SimpleRectangle cBoxOne(100.0, 20.5);

if( cBoxOne > 100.0 ){

cout << “BoxOne is bigger than 100.0!”;}

if( cBoxOne > 30 ) // ?????{

cout << “BoxOne is bigger than 30!”;}

if( 100 > cBoxOne ) // ?????{

cout << “BoxOne is not greater than 100!”;}

}

Page 7: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Problem!

• if( 100 > cBoxOne )

• An int is the first parameter– Can’t be a member of Rectangle– Needs access to mLength and mWidth!

• Solution: friend function

Spring 2019 CS250 - Intro to CS II 7

Page 8: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Friend

Spring 2019 CS250 - Intro to CS II 8

class SimpleRectangle // SimpleRectangle.h{ public:SimpleRectangle(double length, double width);bool operator>(const SimpleRectangle &rcRHS) const;bool operator>(double area) const;

friend bool operator>(double area, const SimpleRectangle &rcRHS);private:double mLength, mWidth;

};

// SimpleRectangle.cppbool operator>(double area, const SimpleRectangle &rcRHS){return area > rcRHS.mLength * rcRHS.mWidth;

}

Page 9: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Usage

Spring 2019 CS250 - Intro to CS II 9

int main(){SimpleRectangle cBoxOne(100.0, 20.5);

if( 101.5 > cBoxOne ) // ?????{

cout << “BoxOne is not greater than 101.5!”;}

if( 100 > cBoxOne ) // ?????{

cout << “BoxOne is not greater than 100!”;}

}

Page 10: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Friends of Classes

Spring 2019 CS250 - Intro to CS II 10

• A friend can be

a) a regular stand-alone function

b) a member of another class

c) an entire class

Page 11: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Overloading Stream Operators

• operator>>(istream, userObject)• Extraction operator

• operator<<(ostream, userObject)• Insertion operator

• Works with keyboard, screen, files, etc.• ostream and istream

Spring 2019 CS250 - Intro to CS II 11

Page 12: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

operator>>

Spring 2019 CS250 - Intro to CS II 12

class SomeClassName // SomeClassName.h{ public: friend istream& operator>> (istream &rcInput,

SomeClassName &rcObject); private:int mData;

};

// SomeClassName.cppistream& operator>> (istream &rcInput, ClassDef &rcObject){rcInput >> rcObject.mData;return rcInput;

}

Page 13: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Overloading Stream Operators

•The general format for overloading the stream operators is as follows:

class SomeClassName{ public: ..... friend istream& operator>> (istream &rcInput,

ClassDef &rcObject); friend ostream& operator<< (ostream &rcOutput,

const ClassDef &rcObject);private:.....

};

Spring 2019 CS250 - Intro to CS II 13

Page 14: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Overload Insertion Operator <<

class Rational { public: Rational (int = 0, int = 1); ..... friend ostream& operator<< (ostream &rcOutput,

const Rational &rcRational);private: ...

};

Spring 2019 CS250 - Intro to CS II 14

Page 15: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Overload Insertion Operator <<

ostream& operator<< (ostream &rcOutput,const Rational &rcRational)

{rcOutput << rcRational.mNumerator << '/'

<< rcRational.mDenominator;

return rcOutput;}

Spring 2019 CS250 - Intro to CS II 15

Page 16: Friends of Classes Operator overloadingzeus.cs.pacificu.edu/chadd/cs250s20/Lectures/06_Friends.pdf · Operator overloading Spring 2019 CS250 -Intro to CS II 1. Operator Overloading

Overload Problemfor

Rational

1. Overload the insertion operator <<

2. Overload the extraction operator >>

3. Overload the multiplication operator *

Rational operator*(const Rational &rcRHS) const;

4. Write a test driverSpring 2019 CS250 - Intro to CS II 16