C++初步之命名空间,类,类的静态方法,函数模板,虚析构函数
在学习C++的时候,开始使用using namespace std;
但是对于命名空间不是很了解.后来在学习C#的时候多了些了解.
但是对于程序包而言主要还是在Java中对于Java包的理解来理解的.
我自己写了个涉及题目上概念的一个简单的CPP程序.如下:
首先声明一点,我是在Ubuntu下用Eclipse的Linuxtool来开发的.
首先是头文件,
/* * Util.h * * Created on: 2011-10-12 * Author: banxi1988 */#ifndef UTIL_H_#define UTIL_H_namespace banxi {class Util {public:Util();virtual ~Util();template<typename T>static T max(T a,T b){return a > b?a:b;}};}//end of namespace of banxi#endif /* UTIL_H_ *//* * Util.cpp * * Created on: 2011-10-12 * Author: banxi1988 */#include "Util.h"namespace banxi {Util::Util() {// TODO Auto-generated constructor stub}Util::~Util() {// TODO Auto-generated destructor stub}}#include <iostream>#include "Util.h"using namespace banxi;using namespace std;int main() {int a = 3,b = 4;double c = 3.2,d = 4.3;cout <<"compare"<<a<<","<<b<<"the max was:"<<Util::max(a,b)<< endl; // printscout <<"compare"<<c<<","<<d<<"the max was:"<<Util::max(c,d)<< endl; // printsreturn 0;}/** * 输出结果: * * compare3,4the max was:4 * compare3.2,4.3the max was:4.3 */