Skip to content

Commit c35abe3

Browse files
author
XuhuaHuang
committed
Add friend insertion operator overload
1 parent f6a8fb0 commit c35abe3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

FriendClass/modern_square_rectangle.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class rectangle_t {
5050
}
5151

5252
void convert(const square_t& sq) noexcept;
53+
54+
friend std::ostream& operator<<(std::ostream& os, const rectangle_t& rect);
5355
};
5456

5557
class square_t {
@@ -79,19 +81,28 @@ void rectangle_t::convert(const square_t& sq) noexcept {
7981
height = sq.side;
8082
}
8183

84+
std::ostream& operator<<(std::ostream& os, const rectangle_t& rect) {
85+
os << rect.width << " x " << rect.height;
86+
return os;
87+
}
88+
8289
int main() {
8390
rectangle_t r1;
8491
rectangle_t r2(2, 3);
8592

93+
std::cout << "Original rectangle: " << r2 << std::endl;
94+
8695
r1 = double_in_size(r2);
8796

97+
std::cout << __LINE__ << ": " << r1 << std::endl;
8898
std::cout << r1.area() << std::endl;
8999

90100
rectangle_t rect;
91101
square_t sqr(4);
92102

93103
rect.convert(sqr);
94104

105+
std::cout << __LINE__ << ": " << rect << std::endl;
95106
std::cout << __LINE__ << ": " << rect.area() << std::endl;
96107

97108
return 0;

0 commit comments

Comments
 (0)