C++

C++ 11일차

일어나코딩해야지 2018. 6. 5. 14:25

C++ 11일차


멤버 메서드 사용하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
 
using namespace std;
 
class vector2 {
public:
    vector2() :x(0), y(0) {}
    vector2(float x, float y) : x(x), y(y) {}
 
    float GetX() const;
    float GetY() const;
private:
    float x;
    float y;
};
 
vector2 Sum(vector2 a, vector2 b) {
    return vector2(a.GetX() + b.GetX(), a.GetY() + b.GetY());
}
 
int main() {
    vector2 a(23);
    vector2 b(-14);
    vector2 c = Sum(a, b);
 
    cout << a.GetX << " " << a.GetY << endl;
    cout << b.GetX << " " << b.GetY << endl;
    return 0;
}
 
vector2::vector2() : x(0), y(0) {}
vector2::vector2(float x, float y) : x(x), y(y) {}
float vector2::GetX() const { return x; }
float vector2::GetY() const { return y; }
cs

 위의 예제는 멤버 메서드 중에 하나인 vector를 잘 설명해 주는 예제입니다.


동영상 : https://www.youtube.com/watch?v=QaCpk53KuNg&list=PLlJhQXcLQBJqywc5dweQ75GBRubzPxhAk&index=66