天天看点

文章标题ifndef boy_h_define boy_h_endifinclude”boy.h”include”iostream”include”boy.h”

在iostream头文件中包含了一些类如cout、cin,而这些类被声明在std空间中。其中命名空间是为了防止引用模糊,例如,在一个空间ns1中有一个a类,在另一个空间中ns2也有一个名为a的类,那么如果直接使用类a在编译器会报错,因为不知道使用哪个。但是通过命名空间通过命名空间来引用,像这样引用ns1::a 这样就不会报错了。

同理std也是这样的,在std空间声明了cin,cout等类。下面就是我的代码:

//boy.h

ifndef boy_h_

define boy_h_

class compare

{

private:

int i;

public:

compare(int x);

void show();

};

endif

//boy.cpp

include”boy.h”

//

compare::compare(int x)

{

i=x;

}

void compare::show()

{

using std::cout;

cout<

include”iostream”

using namespace std;

include”boy.h”

int main()

{

compare s(4);

s.show();

int i=3;

cout<