C++/Operator Overloading
cout 의 범위를 넖히는 연산자 overloading
(19140) 다음과 같은 카드 클래스가 있다. 이 카드 클래스를 cout 을 이용하여 출력하도록 프로그램을 작성하라. Hint 1: 연산자 오버로딩 (메소드 버전 아니고 전역 함수 버전으로) Hint 2: 우리가 쓰던 cout은 ostream 클래스의 instance 임을 알면 도움이 된다. 단, 파라메터 전달시 복사가 일어나지 않도록 유의한다. #include using namespace std; class Card{ public: Card(int r, int s) : rank(r), suit(s){ //Empty } void print() const{ cout
클래스로 cout 구현하기 (2가지 version)
(19080) COUT 클래스를 만들어 정수, double, 그리고 스트링을 출력할 수 있게한다. COUT클래스 내부에서는 출력을 위해 printf를 사용한다. #include class COUT{ public: COUT():data_(0){} int getValue() { return data_; } COUT operator
단항 연산자 정의
(19060) unary operator * 를 사용하면 사각형의 면적을 돌려주도록 재정의하라. 아울러 ~ 는 폭을, ! 는 높이를 돌려주도록 정의한다. #include class CRect { public: // 생성자 정의 CRect(int w, int h):m_width(w), m_height(h){ // } // 연산자 *, ~, ! 정의 int operator *(){ return m_width*m_height; } int operator ~(){ return m_width; } int operator !(){ return m_height; } private: int m_width, m_height; }; using namespace std; int main(){ CRect r(2,3); // ..