학교 과제

c++로 작성한 간단한 직사각형 클래스

꾸준함. 2017. 12. 23. 10:30

자료구조 프로그래밍 과목을 배우면서 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


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형