- 文件包括#include
- 宏定义#define
- 条件编译
#if #endif
#if #else #endif
#ifndef #define #endif
全局变量 extern
main.cpp
1 #include"myCout.h" 2 #include"myHandle.h" 3 #include<iostream> 4 using namespace std; 5 int main() 6 { 7 int pi = 5; 8 double a=1.0,b=3.3; 9 showmin(min,a,b); 10 showmax(max,a,b); 11 showtest(test,a,b); 12 cout<<"pi = "<<pi<<"\nextern pi = "<<::pi<<endl; 13 #if 0 //设置为0,中间内容不编译 14 cout<<"pi = "<<::pi<<endl; 15 cout<<"pi = "<<::pi<<endl; 16 cout<<"pi = "<<::pi<<endl; 17 cout<<"pi = "<<::pi<<endl; 18 cout<<"pi = "<<::pi<<endl; 19 cout<<"pi = "<<::pi<<endl; 20 cout<<"pi = "<<::pi<<endl; 21 #endif 22 cin.get(); 23 return 0; 24 }
myCout.h
1 #ifndef _MYCOUT_H 2 #define _MYCOUT_H 3 4 typedef double funcType(double,double); 5 extern double pi; 6 void showmin(funcType*,double,double); 7 void showmax(funcType*,double,double); 8 void showtest(funcType*,double,double); 9 10 #endif
myCout.cpp
1 #include "myCout.h" 2 #include<iostream> 3 using namespace std; 4 //定义全局变量 5 extern double pi = 3.14159; 6 #if 1 7 void showmin(funcType* fp,double a,double b) 8 { 9 cout<<"min = "<<fp(a,b)<<endl; 10 } 11 void showmax(funcType* fp,double a,double b) 12 { 13 cout<<"max = "<<fp(a,b)<<endl; 14 } 15 void showtest(funcType* fp,double a,double b) 16 { 17 cout<<"test= "<<fp(a,b)<<endl; 18 } 19 #endif
myHandle.h
1 #ifndef _MYHANDLE_H 2 #define _MYHANDLE_H 3 4 double min(double,double); 5 double max(double,double); 6 double test(double,double); 7 8 #endif
myHandle.cpp
1 #include"myHandle.h"; 2 double min(double a,double b) 3 { 4 return a>b?b:a; 5 } 6 double max(double a,double b) 7 { 8 return a>b?a:b; 9 } 10 double test(double a,double b) 11 { 12 extern double pi; 13 return pi; 14 }
运行结果