스트림 입출력의 정체
스트림 입출력의 정체
#include <iostream>
namespace myStd
{
using namespace std; // myStd 내에서만 사용
class ostream {
public :
ostream& operator<<(const char * str) {
printf("%s", str);
return *this;
}
ostream& operator<<(char ch) {
printf("%c", ch);
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&)) {
return fp(*this);
}
};
ostream& endl(ostream& os) { // 전역함수
os << '\n';
fflush(stdout); // 출력버퍼 비우기
return os;
}
ostream cout; // 객체 생성
}
int main() {
using myStd::cout;
using myStd::endl;
cout << "파이는 " << 3.14 << " " << 123 << endl;
return 0;
}사용자 정의 객체와 스트림 입출력
Last updated