04_01.cpp

来自「一些C++的课件和实验源代码」· C++ 代码 · 共 40 行

CPP
40
字号
// 04_01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

namespace ns1{			// 名字空间域ns1
	class matrix{		// 类域ns1::matrix
		int size;
	public:
		void SetSize(int size){ matrix::size = size + 10;}
		int  GetSize(){ return size; }
	};
};

namespace ns2{			// 名字空间域ns2
	class matrix{		// 类域ns2::matrix
		int size;
	public:
		void SetSize(int size){ matrix::size = size; }
		int GetSize(){ return size; }
	};
};

ns1::matrix  m;			// 全局域中定义变量m,变量类型为ns1::matrix

int main(int argc, char* argv[])
{		
	ns2::matrix m;		// 局部域中定义变量m,变量类型为ns2::matrix
	
	m.SetSize(10);		// 局部域中的m
	::m.SetSize(10);	// 全局域中的m
	printf("\n size = %d", m.GetSize());
	printf("\n size = %d", ::m.GetSize());

	getch();
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?