• 코드:
​x
 
1
#include <iostream>
2
using namespace std;
3
​
4
class Rect
5
{
6
private:
7
    double height_;
8
    double width_;
9
public:
10
    Rect(double height, double width);  // 생성자 
11
    void DisplaySize();
12
    Rect operator*(double mul) const;
13
    friend Rect operator*(double mul, const Rect& origin);  // 프렌드 함수 
14
};
15
​
16
int main(void)
17
{
18
    Rect origin_rect(10, 20);
19
    Rect rect01 = origin_rect * 2;
20
    Rect rect02 = 3 * origin_rect;
21
    
22
    rect01.DisplaySize();
23
    rect02.DisplaySize();
24
    return 0;
25
}
26
​
27
Rect::Rect(double height, double width)
28
{
29
    height_ = height;
30
    width_ = width;
31
}
32
​
33
void Rect::DisplaySize()
34
{
35
    cout << "이 사각형의 높이는 " << this->height_ << "이고, 너비는 " << this->width_ << "입니다." << endl;
36
}
37
​
38
Rect Rect::operator*(double mul) const
39
{
40
    return Rect(height_ * mul, width_ * mul);
41
}
42
​
43
Rect operator*(double mul, const Rect& origin)
44
{
45
    return origin * mul;
46
}
표준입력 & 실행옵션