티스토리 뷰

cin, cout, endl

- 콘솔 입출력에 사용되는 cout, cin, endl는 사실 연산자 오버로딩으로 구현되어 있다.

 

- cout, cin은 namespace std 내의 객체의 이름이고, endl은 함수의 이름이다.

 

#include <iostream>
namespace mystd
{
    using namespace std;

    class ostream
    {
    public:
    	// ostream을 참조형으로 반환
        ostream& operator<< (char *str) // 문자열
        {
            printf("%s", str);
            return *this;
        }
        ostream& operator<< (char str) // 문자
        {
            printf("%c", str);
            return *this;
        }
        ostream& operator<< (int num) // 정수
        {
            printf("%d", num);
            return *this;
        }
        ostream& operator<< (double e) // 실수형
        {
            printf("%g", e);
            return *this;
        }
        ostream& operator<< (ostream& (*fp)(ostream &ostm))
        {
            return fp(*this);
        }
    };

    ostream& endl(ostream &ostm)
    {
        ostm << '\n';
        fflush(stdout); // 버퍼 비움
        return ostm;
    }

    ostream cout; // cout은 객체의 이름이다
}

int main()
{
    using mystd::cout;
    using mystd::endl;

    cout << "Simple String";
    cout << endl; // cout.operator(endl)
    // operator<< 연산자 오버로딩이 ostream& 형을 반환하기 때문에 이 문장이 성립된다 
    cout << 3.14 << endl << 123 << endl;

}

 

<< 연산자 오버로딩 

#include <iostream>
using namespace std;

class Point
{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0) : xpos(x), ypos(y) {}

    friend ostream& operator<< (ostream& os, const Point &pos);
};

//ostream& operator<< (ostream& cout, const Point &pos)
//{
//    cout << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
//    return cout;
//}

ostream& operator<< (ostream& os, const Point &pos)
{
    os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
    return os;
}

int main()
{
    Point pos1(1, 3);
    cout << pos1;
    Point pos2(3, 4);
    cout << pos2;
}

 

ostream& operator<< (ostream& os, const Point &pos) 

위 함수는 << 연산자를 오버로딩하고 있다.

파라미터로 ostream의 참조형을 받고있는데 위에서 봤다싶이 ostream은 namespace std에 정의된 클래스다.

 

위 함수 내부의 다음 문장은 

 

 os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;

 

다음과 같이 해석된다.

 

namespace std 내부에는 << 연산자 오버로딩이 되어있고, 

해당 << 연산자 오버로딩에 의해 먼저 '['이 출력되고 전달 받은 ostream&형이 다시 반환된다.

stream&형이 다시 반환되었기 때문에 또 다시 << 연산이 진행될수 있어 다음에는 ", "이 출력된다 ... 

 

 


문제 10-3 [입력을 위한 >> 연산자의 오버로딩] 

#include <iostream>
using namespace std;

class Point
{
private:
    int xpos, ypos;
public:
    Point(int x = 0, int y = 0) : xpos(x), ypos(y) {}

    friend ostream& operator<< (ostream& os, const Point &pos);
    friend istream& operator>> (istream& is, Point &pos);
};

// output
ostream& operator<< (ostream& os, const Point &pos)
{
    os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
    return cout;
}
// input
istream& operator>> (istream& is, Point &pos)
{
    is >> pos.xpos >> pos.ypos;
    return is;
}

int main()
{
    Point pos1;
    cout << "x,y 좌표 순으로 입력: ";
    cin >> pos1;
    cout << pos1;
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함