Friends of Classes Operator...
Transcript of Friends of Classes Operator...
![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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022041102/5edd7522ad6a402d66688f17/html5/thumbnails/16.jpg)
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