为了防止某一软件库中的一些标识符和其它软件库中的冲突 ,可以为 各种标识符加上能反映软件性质的前缀。
例如三维图形标准 OpenGL 的所有库函数 均以 gl 开头,所有常量(或宏定义)均以 GL 开头。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 //定义复数类 6 class complex{ 7 float real; //实部 8 float image; //虚部 9 public: 10 //重载的运算符"+"的原型 11 complex operator+ (complex right); 12 //重载赋值运算符"="的定义 13 complex operator= (complex right); 14 void set_complex(float re, float im); 15 void put_complex(char *name); 16 }; 17 //重载加法运算符"+"的定义 18 complex complex::operator+ (complex right) { 19 complex temp; 20 temp.real = this->real + right.real; 21 temp.image = this->image + right.image; 22 return temp; 23 } 24 //重载加赋值运算符"="的定义 25 complex complex::operator= (complex right) { 26 this->real = right.real; 27 this->image = right.image; 28 return *this; 29 } 30 //定义set_complex()成员函数 31 void complex::set_complex(float re, float im) { 32 real = re; 33 image = im; 34 } 35 //定义put_complex()成员函数 36 void complex::put_complex(char *name) { 37 cout<<name<<": "; 38 cout << real << ' '; 39 if (image >= 0.0 ) cout << '+'; 40 cout << image << "i "; 41 } 42 //在main()函数中使用complex类的对象 43 44 int main(int argc, char** argv) { 45 complex A, B, C; //创建复数对象 46 47 //设置复数变量的值 48 A.set_complex(1.2, 0.3); 49 B.set_complex(-0.5, -0.8); 50 51 //显示复数数据 52 A.put_complex("A"); 53 B.put_complex("B"); 54 55 //赋值运算,显示结果 56 C = A; 57 C.put_complex("C=A"); 58 59 //加法及赋值运算,显示结果 60 C = A + B; 61 C.put_complex("C=A+B"); 62 return 0; 63 return 0; 64 }