연재 완료/C++ Lang 예제코드 모음

Cpp_other_ex 1. Class

라이피 (Lypi) 2019. 4. 17. 12:11
반응형
Point.h
#pragma once

#include 

class Point
{
private:
	int x, y;
public:
	void SetPosition(int _x, int _y);
	void Move(int _x, int _y);
	void Show(void);
	Point();
	~Point();
};


Point.cpp
#include "Point.h"

void Point::SetPosition(int _x, int _y)
{
	x = _x;
	y = _y;
}
void Point::Move(int _x, int _y)
{
	x += _x;
	y += _y;
}

void Point::Show(void)
{
	std::cout << "(" << x << "," << y << ")" << std::endl;
}

Point::Point()
{
	std::cout << "constuctor" << std::endl;
}


Point::~Point()
{
	std::cout << "dIstructor" << std::endl;
}


main.cpp
#include "Point.h"

int main()
{
	Point p1, p2;

	p1.SetPosition(10, 20);	//p1의 좌표 설정
	p2.SetPosition(50, 60); //p2의 좌표 설정

	p1.Move(5, 0);
	p2.Move(0, 5);

	p1.Show();
	p2.Show();

	return 0;
}
반응형