#include<iostream>
using namespace std;
class Point //클래스 정의
{
public: int _x, _y; // member variable definition
// 보통 멤버변수와 일반변수의 충돌을 피하기 위해 멤버변수의 이름을 '_'을 붙임
//const int maxCount;
void Print(); // member function-->
// 호출하기 위해서는 반드시 객체 이름 명시!
Point(); // constructor(class와 동일한 이름을 가진 멤버함수
// 객체의 동작을 준비함
// 반환값이 존재하지 않는다(자동호출)
Point(int initialX, int initialY);
Point(const Point& pt); //copy constructor
// 다른 객체로부터 값을 복사해서 초기화함
// 객체를 사용한 초기화로 멤버 변수를 1:1로 복사해준다
~Point(); // 함수의 소멸자로서 클래스에 오직 하나만 존재한다
// 함수가 끝나면 동시에 소멸자가 호출된다.
};
void Point::Print() // 클래스의 이름을 정의하고 함수를 정의한다
// (::)-> 범위지정연산자
{
cout << "member function start" << endl;
cout << "x : " << _x << " " << endl << "y : " << _y << endl;
cout << "member function end" << endl << endl;;
}
Point::Point()
//: maxCount(10)--> 초기화 리스트 : 반드시 초기화해야 하는 벰버 변수들은 생성자의 초기화 리스트를 사용한다
{
cout << "constructor start" << endl;
_x = 0;
_y = 0;
cout << "x : " << _x << " " << endl << "y : " << _y << endl;
cout << "constructor end" << endl << endl;
}
Point::Point(int initialX, int initialY)
{
cout << "constructor with parameter start" << endl;
_x = initialX;
_y = initialY;
cout << "x : " << _x << " " << endl << "y : " << _y << endl;
cout << "constructor with parameter end" << endl << endl;
}
Point::Point(const Point& pt)
{
cout << "copy constructor start" << endl;
_x = pt._x;
_y = pt._y;
cout << "copy constructor end" << endl;
}
Point::~Point()
{
cout << "destructor start" << endl;
_x = 0;
_y = 0;
cout << "destructor end" << endl;
}
int main()
{
Point pt; //생성자 자동 호출
pt.Print();
Point pt1;
pt1.Print();
Point pt2(1, 2); //인자 있는 생성자 자동 호출
pt2.Print();
Point pt3 = pt2; // Point pt3(pt2); 과 동일한 표현이다
return 0;
}
해당 코드의 결과값
해당 코드 결과값