namespace
namespace 사용 이유 : 변수명이 겹칠 수 있기 때문에
EX)
using
사용한다는 뜻 ex)using namespace std; // namespace 안에 있는 std을 사용한다는 뜻라이브러리가 namespace std공간에 넣어놨기 때문에 std를 사용하기 싫으면 using namespace std;를 선언
그러면 std::를 안쓰고 cout를 사용할 수 있다.
개별적으로 풀어주는법
std안에 있는 cout만 풀어준다라는 뜻 std를 다풀어주면 네임스페이스 기능을 완전히 상실하기 때문에 귀찮더라도 자주쓰는 기능만 개별적으로 풀어주는게 더 나음
입출력 직접 구현해보기
반환 타입이 자기 자신이어야 하는 이유는 <<을 호출 할 때 s << "a"를 실행하고 자기 자신을 리턴해야 또 함수를 실행해줘서 그 다음에 있는 1을 해줘서 연속적으로 할 수 있기 때문에 반환값은 자기 자신이어야 한다.
std에 있는 자주 쓰는 기능들 구현해보기
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include<iostream>
#include "test.h"
#include<time.h>
#include "list.h"
#include "b.h"
#include "CArr.h"
#include "CList.h"
using std::cout;
void Myendl() // 개행을 위한 함수
{
printf("\n");
}
class MYostream{
public:
MYostream& operator << (int pstring)
{
printf("%d", pstring);
return *this;
}
MYostream& operator << (const char* pstring) //문자열에 시작주소
{
printf("%s", pstring);
return *this;
}
MYostream& operator >> (int& pstring)
{
scanf_s("%d", pstring);
return *this;
}
MYostream& operator << (void (*Myendl) (void)) //함수의 주소를 받아와서 <<를 이용한 개행을 가능케함
{
Myendl();
return *this;
}
};
int main()
{
MYostream s;
s << "a" << Myendl<< 1;
int a = 0;
s >> a;
return 0;
}
|
cs |
endl 엔터키는 연산자가 아니기때문에 함수를 만들어 주소를 직접 전달해 가능케 함
'C++' 카테고리의 다른 글
C++ iterator (0) | 2022.09.02 |
---|---|
C++ STL(vector,list) (0) | 2022.08.31 |
C++ 클래스 템플릿 (2) | 2022.08.27 |
C++ 함수 템플릿 (0) | 2022.08.26 |
C++ 클래스를 이용한 배열 (0) | 2022.08.24 |