#include <iostream>
using namespace std;
class Point
{
private:
     int x;
     int y;

public:
     bool InitMembers(int xpos,int ypos);
     int GetX() const;
     int GetY() const;
     bool SetX(int xpos);
     bool SetY(int ypos);
};

bool Point::InitMembers(int xpos, int ypos)
{
     if(xpos<0 || ypos<0)
     {
          cout<<"벗어난 범위의 값 전달"<<endl;
          return false;
     }

 x=xpos;
 y=ypos;
 return true;

}

int Point::GetX() const
{
     return x;
}
int Point::GetY() const
{
     return y;
}
bool Point::SetX(int xpos)
{
     if(0>xpos || xpos>100)
     {
          cout<<"벗어난 범위의 값 전달"<<endl;
          return false;
     }
     x=xpos;
     return true;
}
bool Point::SetY(int ypos)
{
     if(0>ypos || ypos>100)
     {
      cout<<"벗어난 범위의 값 전달"<<endl;
      return false;
     }
     y=ypos;
     return true;
}

class Rectangle
{
private:
     Point upLeft;
     Point lowRight;

public:
     bool InitMembers(const Point &ul, const Point &lr);
     void ShowRecInfo() const;
};

bool Rectangle::InitMembers(const Point &ul, const Point &lr)
{
     if(ul.GetX()>lr.GetX() || ul.GetY()>lr.GetY())
     {
          cout<<"잘못된 위치정보 전달"<<endl;
          return false;
     }
 upLeft=ul;
 lowRight=lr;
 return true;
}
void Rectangle::ShowRecInfo() const
{
     cout<<"좌 상단: "<<'['<<upLeft.GetX()<<",";
     cout<<upLeft.GetY()<<']'<<endl;
     cout<<"우 하단: "<<'['<<lowRight.GetX()<<",";
     cout<<lowRight.GetY()<<']'<<endl<<endl;
}

int main(void)
{
     Point pos1;
     if(!pos1.InitMembers(-2,4))
          cout<<"초기화 실패"<<endl;
     if(!pos1.InitMembers(2,4))
          cout<<"초기화 실패"<<endl;
 
 Point pos2;
 if(!pos2.InitMembers(5,9))
      cout<<"초기화 실패"<<endl;
 
 Rectangle rec;

 if(!rec.InitMembers(pos2,pos1))
      cout<<"직사각형 초기화 실패"<<endl;

 if(!rec.InitMembers(pos1,pos2))
      cout<<"직사각형 초기화 실패"<<endl;

 rec.ShowRecInfo();
 return 0;
}

 

실행결과:
벗어난 범위의 값 전달
초기화 실패
잘못된 위치정보 전달
직사각형 초기화 실패
좌 상단: [2, 4]
우 하단: [5, 9]


(위 소스는 한치의 오차도 없이 교재에 있는 글자 그대로 옮긴 것임을 밝힙니다.)
윤성우 열혈 c++ 153~157쪽에 나와있는 분할된 소스를 하나로 다 합친건데요

 

제가 초짜라 그런것 같은데 메인 함수에서 멤버함수를 호출하는 부분이 이해가 안가네요

 

초기화 실패??? 좌상단 우하단 모두 제대로 값이 잘들어갔는데 소스는 왜 초기화 실패라고 쓰여있는건가여??

 

그리고 InitMember 함수의 return 값은 0 아니면 1일 텐데

 

if(!0) 이나 if(!1)은 도대체 뭔가요??? if문에다가 저렇게도 쓸 수 있나요???

 

윤성우 열혈 c++프로그래밍 가지고 계신 분들 153~157쪽 도움좀 구해봅니다 ㅠㅠㅠㅠ