• 코드:
​x
 
1
#include <iostream>
2
#include <cmath>
3
using namespace std;
4
​
5
class Rect;
6
​
7
class Display
8
{
9
public:
10
    void ShowSize(const Rect& target);
11
    void ShowDiagonal(const Rect& target);
12
};
13
​
14
class Rect
15
{
16
private:
17
    double height_;
18
    double width_;
19
public:
20
    Rect(double height, double width);  // 생성자 
21
    void height() const;
22
    void width() const;
23
    friend void Display::ShowDiagonal(const Rect& target);  // 프렌드 멤버 함수 선언 
24
};
25
​
26
int main(void)
27
{
28
    Rect rect01(10, 20);
29
    
30
    Display rect_display;
31
    rect_display.ShowSize(rect01);
32
    rect_display.ShowDiagonal(rect01);
33
    
34
    return 0;
35
}
36
​
37
Rect::Rect(double height, double width)
38
{
39
    height_ = height;
40
    width_ = width;
41
}
42
​
43
void Rect::height() const
44
{
45
    cout << "이 사각형의 높이는 " << this->height_ << "입니다." << endl;
46
}
47
​
48
void Rect::width() const
49
{
50
    cout << "이 사각형의 너비는 " << this->width_ << "입니다." << endl;
51
}
52
​
53
void Display::ShowSize(const Rect& target)
54
{
55
    target.height();
56
    target.width();
57
}
58
​
59
void Display::ShowDiagonal(const Rect& target)
60
{
61
    double diagonal;
62
    diagonal = sqrt(pow(target.height_, 2) + pow(target.width_, 2));
63
    
64
    cout << "이 사각형의 대각선의 길이는 " << diagonal << "입니다." << endl;
65
}
표준입력 & 실행옵션