#include <iostream>
using namespace std;
//this pointer : 객체 자기자신의 주소값을 가지고있는 포인터, 자동적으로 생성
//Point pt; pt.x == this ->x
class Point {
private:
int x;
int y;
public:
Point():x(0),y(0){}// default 값
Point(int _x,int _y):x(_x),y(_y){}
// void setXY(int _x, int _y) {
// x = _x;
//y = _y;
// }
void setXY(int _x, int _y) {
this->x = _x;
this->y = _y; // 시작주소값을 저장하는것
}
int getX() const{ return x; } // 맴버변수를 메인에 반환
int getY() const{ return y; } // const를 적어줘야만 사용가능하다
Point operator+(const Point& pt) { // pt1 + pt2 -> pt1. + (pt2)
Point result(this->x + pt.getX(), this->y + pt.getY());
return result;
}
Point& operator=(const Point& pt) { // pt3 = pt2 = pt1
this->x = pt.getX();
this->y = pt.getY();
return (*this);
}
};
//pass by reference : 객체는 함수의 입력이 될때 pass by ref.추천한다
//const : 함수 내부에서 객체의 맴버 변수가 바뀌지않음
// -> Class의 method들에 cosnt를 삽입해야함(함수이름 const{})
//외부 메쏘드가 아니다 클라스 밖에 있다
void print_pt(const Point& pt) { // 메모리 복사가 이뤄지지않는다!! / 동일한 것을 가르키지만 값은 다르다
cout << pt.getX() << ", " << pt.getY() << endl;
}
//연산자 overloading : +,-,*,==,!=,>=,=,+= 등을 객체에 맞게 재정의
//1) class 외부: operator+(좌측 피연산자, 우측 피연산자)
//2) class 내부: operator+(우측 피연산자)-> 좌측 피연산자는 자기자신
//result = pt1 + pt2 -> +(pt1,pt2)
//Point operator + (const Point& pt1,const Point& pt2) {
//Point result(pt1.getX() + pt2.getX(), pt1.getY() + pt2.getY());
//return result;
//}
// cout<<pt.getX()<<", "<<pt.getY()
// cout<< pt ---> <<(cout,pt)
// cout << pt1 << pt2
// cout << pt1을 실행 --> 결과 return cout // cout<<pt2 실행
ostream& operator<<(ostream& cout,const Point pt) {
cout << pt.getX() << ", " << pt.getY();
return cout;
}
int main() {
Point pt1(1, 2); // 2번 생성자 호출
print_pt(pt1);
//객체의 포인터: (*pt). 는 pt->로 대체가능하다
Point* pPt = &pt1; // 주소값을 저장하는 변수
cout << (*pPt).getX() << ", " << (*pPt).getY() << endl; // 점프해간다
cout << pPt->getX() << ", " << pPt->getY() << endl; // 이게 더 쓰기 편하다
cout << endl;
Point* list = new Point[5];
for (int i = 0; i < 5; i++)
cout << list[i].getX() << ", " << list[i].getY() << endl;
cout << endl;
delete[] list; // 할당해제
Point pt2(10, 20);
Point pt3(100, 200);
pt1 = pt2 + pt3; // 덧셈은 두개의 입력값을 갖는 함수이다 포인터에는 정의가 되어있지않는다 함수 overloading필요!!
cout << pt1 << endl;
cout << pt2 << endl;
cout << pt3 << endl; // 함수오버로딩으로 내부적 함수 재정의로 가능해짐
return 0;
}
1, 2
1, 2
1, 2
0, 0
0, 0
0, 0
0, 0
0, 0
110, 220
10, 20
100, 200
class Point {
private:
int x;
int y;
public:
Point():x(0),y(0){}// default 값
Point(int _x,int _y):x(_x),y(_y){}
// void setXY(int _x, int _y) {
// x = _x;
//y = _y;
// }
void setXY(int _x, int _y);
int getX() const{ return x; } // 맴버변수를 메인에 반환
int getY() const{ return y; } // const를 적어줘야만 사용가능하다
Point operator+(const Point& pt);
Point& operator=(const Point& pt);
};
// void setXY(int _x, int _y) {
// x = _x;
//y = _y;
// }
void Point:: setXY(int _x, int _y) {
this->x = _x;
this->y = _y; // 시작주소값을 저장하는것
}
int Point::getX() const { return x; } // 맴버변수를 메인에 반환
int Point::getY() const { return y; } // const를 적어줘야만 사용가능하다
Point Point::operator+(const Point& pt) { // pt1 + pt2 -> pt1. + (pt2)
Point result(this->x + pt.getX(), this->y + pt.getY());
return result;
}
Point& Point::operator=(const Point& pt) { // pt3 = pt2 = pt1
this->x = pt.getX();
this->y = pt.getY();
return (*this);
}
해당 자료는 경희대학교 소프트웨어융합학과 배성호교수님 수업내용을 참조하였습니다.
'Code > C++' 카테고리의 다른 글
[C++](example) class 생성/ friend/ spy (0) | 2019.11.12 |
---|---|
[C++] class 생성/ friend / spy (0) | 2019.11.11 |
[C++] (example) private& public/ 계좌만들기/ 분자& 분모 (0) | 2019.11.02 |
[C++] 계좌만들기/ private & public (0) | 2019.11.02 |
[C++] class & object/ private & public (0) | 2019.11.02 |