• 코드:
​x
 
1
#include <iostream>
2
using namespace std;
3
​
4
class Position
5
{
6
private:
7
    double x_;
8
    double y_;
9
public:
10
    Position(double x, double y);   // 생성자 
11
    void Display();
12
    Position operator-(const Position& other);  // - 연산자 함수 
13
};
14
​
15
int main(void)
16
{
17
    Position pos1 = Position(3.3, 12.5);
18
    Position pos2 = Position(-14.4, 7.8);
19
    Position pos3 = pos1 - pos2;
20
    
21
    pos3.Display();
22
    return 0;
23
}
24
​
25
Position::Position(double x, double y)
26
{
27
    x_ = x;
28
    y_ = y;
29
}
30
​
31
Position Position::operator-(const Position& other)
32
{
33
    return Position((x_ + other.x_)/2, (y_ + other.y_)/2);
34
}
35
​
36
void Position::Display()
37
{
38
    cout << "두 지점의 중간 지점의 좌표는 x좌표가 " << this->x_ << "이고, y좌표가 " << this->y_ << "입니다." << endl;
39
}
표준입력 & 실행옵션