자료구조 프로그래밍 과목을 배우면서 c++로 작성한 간단한 직사각형 클래스입니다.(2가지 버전)
Recta.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
int xLow, yLow, height, width;
public:
Rectangle(int, int, int, int); //constructor
void Print();
bool LessThan(Rectangle &);
bool Equal(Rectangle &);
int GetHeight();
int GetWidth();
};
#endif
Recta.cpp
#include <iostream>
#include "recta.h"
using namespace std;
Rectangle::Rectangle(int x = 0, int y = 0, int h = 0, int w = 0) :xLow(x), yLow(y), height(h), width(w)
{
}
void Rectangle::Print()
{
cout << "Position: " << xLow << " " << yLow << ": Height = " << height << " Width = " << width << endl;
}
bool Rectangle::LessThan(Rectangle &s)
{
if (height*width < s.height*s.width)
return true;
else
return false;
}
bool Rectangle::Equal(Rectangle &s)
{
if (xLow == s.xLow && yLow == s.yLow&&height == s.height&&width == s.width)
return true;
else
return false;
}
int Rectangle::GetHeight()
{
return height;
}
int Rectangle::GetWidth()
{
return width;
}
hw1a.cpp
#include <iostream>
#include "recta.h"
using namespace std;
int main(void)
{
Rectangle r(2, 3, 6, 6), s(1, 2, 4, 6);
cout << "<rectangle r> ";
r.Print();
cout << "<rectangle s> ";
s.Print();
if (r.LessThan(s))
cout << "s is bigger";
else if (r.Equal(s))
cout << "Same Size";
else
cout << "r is bigger";
cout << endl;
return 0;
}
Rectb.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
using namespace std;
class Rectangle
{
private:
int xLow, yLow, height, width;
public:
Rectangle(int, int, int, int); //constructor
bool operator<(Rectangle &);
bool operator==(Rectangle &);
int GetHeight();
int GetWidth();
friend ostream &operator<<(ostream &os, Rectangle &);
};
#endif
Rectb.cpp
#include <iostream>
#include "rectb.h"
using namespace std;
Rectangle::Rectangle(int x = 0, int y = 0, int h = 0, int w = 0) :xLow(x), yLow(y), height(h), width(w)
{
}
bool Rectangle::operator<(Rectangle &s)
{
if (height*width < s.width*s.height)
return true;
else
return false;
}
bool Rectangle::operator==(Rectangle &s)
{
if (height*width == s.width*s.height)
return true;
else
return false;
}
int Rectangle::GetHeight()
{
return height;
}
int Rectangle::GetWidth()
{
return width;
}
ostream &operator<<(ostream &os, Rectangle &s)
{
os << "Position " << s.xLow << " " << s.yLow << "; Height = " << s.GetHeight() << " Width = " << s.GetWidth() << endl;
return os;
}
hw1b.cpp
#include <iostream>
#include "rectb.h"
using namespace std;
int main(void)
{
Rectangle r(2, 3, 6, 6), s(1, 2, 4, 6);
cout << "<rectangle r> " << r << "<rectangle s> " << s;
if (r < s)
cout << "s is bigger";
else if (r == s)
cout << "Same Size";
else
cout << "r is bigger";
cout << endl;
return 0;
}
개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
'학교 과제' 카테고리의 다른 글
c++로 작성한 후위연산 계산기 (0) | 2017.12.29 |
---|---|
c++로 작성한 미로 클래스 (0) | 2017.12.27 |
c++로 작성한 간단한 행렬 클래스 (0) | 2017.12.26 |
c++로 작성한 간단한 다항식 클래스 (2) | 2017.12.24 |
C로 작성한 가우스-조던 소거법 (4) | 2017.12.21 |