C++/디딤돌 C++

[C++ 소스] iostream 클래스 내부

언제나휴일 2016. 12. 16. 13:59
반응형

[C++ 소스] iostream 클래스 내부


LikeAsiostream.cpp

LikeAsiostream.h

Program.cpp




//LikeAsiostream.h

#pragma once

class LikeAsiostream

{

public:   

    LikeAsiostream &operator<<(char value);

    LikeAsiostream &operator<<(int value);

    LikeAsiostream &operator<<(float value);

    LikeAsiostream &operator<<(double value);

    LikeAsiostream &operator<<(const void *value);

    LikeAsiostream &operator<<(const char *value);

 

    LikeAsiostream &operator>>(char &value);

    LikeAsiostream &operator>>(int &value);

    LikeAsiostream &operator>>(float &value);

    LikeAsiostream &operator>>(double &value);   

    LikeAsiostream &operator>>(char *value);

};

#define lendl "\n"

extern LikeAsiostream ein,eout;



///LikeAsiostream.cpp

#include "LikeAsiostream.h"

#include <stdio.h>

#pragma warning(disable:4996)

LikeAsiostream ein;

LikeAsiostream eout;

 

LikeAsiostream &LikeAsiostream::operator<<(char value)

{

    printf("%c",value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator<<(int value)

{

    printf("%d",value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator<<(float value)

{

    printf("%f",value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator<<(double value)

{

    printf("%f",value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator<<(const void *value)

{

    printf("%p",value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator<<(const char *value)

{

    printf("%s",value);

    return (*this);

}

                       

LikeAsiostream &LikeAsiostream::operator>>(char &value)

{

    scanf("%c",&value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator>>(int &value)

{

    scanf("%d",&value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator>>(float &value)

{

    scanf("%f",&value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator>>(double &value)

{

    scanf("%lf",&value);

    return (*this);

}

LikeAsiostream &LikeAsiostream::operator>>(char *value)

{

    scanf("%s",value);

    return (*this);

}


//Program.cpp

#include "LikeAsiostream.h"

 

int main()

{

    eout<<"번호:";

    int num;

    ein>>num;

    eout<<"이름:";

    char name[100];

    ein>>name;

   

    eout<<"번호:"<<num<<" 이름:"<<name<<lendl;

    return 0;

}


실행 결과

번호:45

이름:홍길동

번호:45 이름:홍길동



본문

[디딤돌 C++] 53. iostream 흉내내기





반응형