Rect.h
Rect.cpp
main.cpp
#ifndef RECT_H
#define RECT_H
#include "Point.h"
class Rect
{
public:
// 생성자
Rect();
// 각 점의 값 지정/얻기
void SetTopLeft(const Point& topLeft);
void SetBottomRight(const Point& bottomRight);
void SetRect(int left, int top, int right, int bottom);
Point GetTopLeft() const;
Point GetBottomRight() const;
void GetRect(int& left, int& top, int& right, int& bottom);
// 넓이, 높이 계산
int GetWidth() const;
int GetHeight() const;
// 내
용 출력
void Print() const;
protected:
Point _topLeft;
Point _bottomRight;
};
#endif
Rect.cpp
#include "Rect.h"
#include <iostream>
using namespace std;
Rect::Rect()
{
}
void Rect::SetTopLeft(const Point& topLeft)
{
_topLeft = topLeft;
}
void Rect::SetBottomRight(const Point& bottomRight)
{
_bottomRight = bottomRight;
}
void Rect::SetRect(int left, int top, int right, int bottom)
{
_topLeft.SetX(left);
_topLeft.SetY(top);
_bottomRight.SetX(right);
_bottomRight.SetY(bottom);
}
Point Rect::GetTopLeft() const
{
return _topLeft;
}
Point Rect::GetBottomRight() const
{
return _bottomRight;
}
void Rect::GetRect(int& left, int& top, int& right, int& bottom)
{
left = _topLeft.GetX();
top = _topLeft.GetY();
right = _bottomRight.GetX();
bottom = _bottomRight.GetY();
}
int Rect::GetWidth() const
{
if(_bottomRight.GetX() > _topLeft.GetX())
{
return (_bottomRight.GetX() - _topLeft.GetX() + 1);
}
else
{
return (_topLeft.GetX() - _bottomRight.GetX() + 1);
}
}
int Rect::GetHeight() const
{
if(_bottomRight.GetY() > _topLeft.GetY())
{
return (_bottomRight.GetY() - _topLeft.GetY() + 1);
}
else
{
return (_topLeft.GetY() - _bottomRight.GetY() + 1);
}
}
void Rect::Print() const
{
cout << "{L=" << _topLeft.GetX() << ", T=" << _topLeft.GetY();
cout << ", R=" << _bottomRight.GetX() << ", B=" <<
_bottomRight.GetY() << "}\n";
}
main.cpp
#include "Rect.h"
#include <iostream>
using namespace std;
int main()
{
// Rect 객
체 생성
Rect rc1;
// 값을 바
꿔본다.
rc1.SetRect(10, 10, 0, 0);
// 내용 출력
rc1.Print();
// 넓이, 높이 출력
cout << "rc1.GetWidth() = " << rc1.GetWidth() << endl;
cout << "rc1.GetHeight() = " << rc1.GetHeight() << endl;
// 값을 바꿔본다.
rc1.SetRect(10, 10, 20, 20);
// 내용 출력
rc1.Print();
// 넓이, 높이 출력
cout << "rc1.GetWidth() = " << rc1.GetWidth() << endl;
cout << "rc1.GetHeight() = " << rc1.GetHeight() << endl;
return 0;
}