1、整理C+程序的结构典型案例第五章 C+程序的结构典型案例【案例5-1】局部作用域的效果#include using namespace std; void fun() /变量num将在每次进入函数fun()时进行初始化 int num = 10; cout num n; num+; / 这个语句没有持续效果 int main() for(int i=0; i 3; i+) fun(); return 0; 【案例5-2】屏蔽效应作用域效果导致的名称隐藏#include using namespace std; int main() int i = 10, j = 30; if(j 0) int
2、 i; / 内部的i 将隐藏或屏蔽外层的i i = j / 2; cout inner variable i: i n; cout outer variable i: i n; return 0; 【案例5-3】筛选偶数文件作用域变量#include using namespace std; int count; /这是一个全局变量 void func1() void func2(); cout count: count n; /可以访问全局变量count func2(); void func2() int count; /这是一个局部变量 for(count=0; count2; count
3、+) cout *; int main() void func1(); void func2(); int i; /这是一个局部变量 for(i=0; i10; i+) count = i+; func1(); return 0; 【案例5-4】求数据序列的平均值static局部变量的持续效果#include using namespace std; int Average(int i) static int sum = 0, count = 0; /声明静态局部变量,具有全局寿命,局部可见 sum = sum + i; count+; return sum/count; int main()
4、 int num; /局部变量,具有动态生存期do cout num; if(num != -1) cout Running average is: Average(num); cout -1); return 0; 【案例5-5】求数据序列的平均值static全局变量的应用#include using namespace std; int Average(int i); void reset(); int main() int num; /局部变量,具有动态生存期 do cout num; if(num = -2) reset(); continue; if(num != -1) cout
5、Running average is: Average(num); cout endl; while(num != -1); return 0; static int sum = 0, count = 0; /静态全局变量,具有静态生存期,全局可见int Average(int i) sum = sum + i; count+; return sum/count; void reset() sum = 0; count = 0; 【案例5-6】时钟类具有静态生存期的全局变量和全局对象#includeusing namespace std;int h=0,m=0,s=0; /声明全局变量,具有静
6、态生存期class Clockpublic: Clock(); void SetTime(int NewH, int NewM, int NewS); /三个形参均具有函数原型作用域 void ShowTime(); Clock()private: int Hour,Minute,Second;Clock:Clock() Hour=h; Minute=m; Second=s; /使用全局变量初始化void Clock:SetTime(int NewH, int NewM, int NewS) Hour=NewH;Minute=NewM;Second=NewS;void Clock:ShowTi
7、me() coutHour:Minute:Secondendl;Clock globClock; /声明对象globClock,具有静态生存期,文件作用域int main() coutInitial time output:endl; /引用具有文件作用域的对象globClock: globClock.ShowTime(); /对象的成员函数具有类作用域 globClock.SetTime(10,20,30); /将时间设置为10:20:30 /调用拷贝构造函数,以globClock为初始值 Clock myClock(globClock); /声明具有块作用域的对象myClock coutS
8、etted time output:endl; myClock.ShowTime(); /引用具有块作用域的对象myClock return 0;【案例5-7】实现数据共享公有静态数据成员#include using namespace std; class PubClass public: static int num; /公有静态数据成员的声明 void shownum() cout The num is:num endl; ; int PubClass:num; /在类外定义num int main() PubClass a, b; PubClass:num = 1000; /通过类名访
9、问静态成员num a.shownum(); b.shownum(); a.num = 2000; /通过对象访问静态成员num a.shownum(); b.shownum(); return 0; 【案例5-8】实现数据共享私有型静态数据成员#include using namespace std; class PriClass static int num; /私有型静态成员public: void setnum(int i) num = i; ; void shownum() cout The num is:num n; ; int PriClass:num; /在类外定义 num in
10、t main() PriClass a, b; a.shownum(); b.shownum(); a.setnum(1000); /设置静态成员num为1000 a.shownum(); b.shownum(); return 0; 【案例5-9】实现函数共享静态函数成员#include using namespace std; class FunClass static int count; /静态数据成员声明public: FunClass() count+; cout Constructing object count endl; FunClass() cout Destroying
11、object count endl; count-; static int GetCount() return count; /静态函数成员; int FunClass:count; /静态数据成员定义int main() FunClass a, b, c; cout From Class, there are now FunClass:GetCount() in existence.n; cout From Object, there are now a.GetCount() in existence.n; return 0; 规划编制单位应当在报送审查的环境影响报告书中附具对公众意见采纳与
12、不采纳情况及其理由的说明。【案例5-10】求最小公因子友元函数的访问类的私有成员#include using namespace std; class FriFunClass int a, b; public: FriFunClass(int i, int j) a=i; b=j; friend int FriFun(FriFunClass x); /友元成员函数 ; int FriFun(FriFunClass x) /注意:FriFun() 不是任何类的成员函数 /由于函数FriFun() 是类FriFunClass的友元函数,所以它不能直接访问a和b int max = x.a x.b
13、? x.a : x.b; for(int i=2; i = max; i+) if(x.a%i)=0 & (x.b%i)=0) return i; return 0; int main() FriFunClass n(10, 20); /声明类对象 if(FriFun(n) cout Common denominator is FriFun(n) n; else cout No common denominator.n; return 0; 2.早期介入原则;(3)旅行费用法【案例5-11】判断圆柱体和立方体的颜色是否相同多个类共享友元函数#include using namespace st
14、d; class Cylinder; / 前向引用声明 enum Colors red, green, yellow ; /定义颜色枚举类型class Cube Colors color; public: Cube(Colors c) color = c; friend bool TestSame(Cube x, Cylinder y); /声明为Cube的友元函数; class Cylinder Colors color; public: Cylinder(Colors c) color= c; friend bool TestSame(Cube x, Cylinder y); /声明为Cy
15、linder的友元函数; bool TestSame(Cube x, Cylinder y) if(x.color = y.color) return true; else return false; int main() Cube cube1(red), cube2(yellow); Cylinder cyl(yellow); /声明对象并初始化 if(TestSame(cube1, cyl) cout The color of cube1 and cyl are the same.n; else cout The color of cube1 and cyl are different.n
16、; if(TestSame(cube2, cyl) cout The color of cube2 and cyl are the same.n; else cout The color of cube2 and cyl are different.n; return 0; 【案例5-12】计算2个三角形之和友元函数的应用#include #include using namespace std;class Trig double x,y,z; double area() double d=(x+y+z)/2; return sqrt(d* (d-x)* (d-y)* (d-z) ; publ
17、ic : Trig (int i,int j,int k) x=i;y=j;z=k; int isTriangle() /判断是否构成三角形 if (x+yz & x+zy & y+zx) return 1 ; else return 0; friend double twoarea(Trig tl,Trig t2) /声明友元函数 return tl.area()+t2.area() ; ;int main() Trig tl (3,5,7) ,t2 (8, 7, 12) ; if (tl.isTriangle() & t2.isTriangle() cout Total area of t
18、wo Triangles: twoarea(tl,t2) endl; else cout Cannot form a Triangle endl; return 0;4.广泛参与原则。(2)评价范围。根据评价机构专业特长和工作能力,确定其相应的评价范围。【案例5-13】数据的单向传递常引用作函数形参#include using namespace std;/常引用作形参,在函数中不能更新z所引用的对象,因此对应的实参不会被破坏。void fun(int x, int& y, const int& z) x += z; y += z; cout x = x , y = y , z = z end
19、l;int main() int a = 20, b = 30, c = 40; cout a = a , b = b , c = c endl; fun(a,b,c); cout a = a , b = b , c = c endl; fun(2*a-3,b,c); cout a = a , b = b , c = c endl; return 0;【案例5-14】成员函数的选择调用常成员函数#includeusing namespace std;class ZRF public: ZRF(int Z1, int Z2)ZRF1=Z1;ZRF2=Z2; void disp(); void d
20、isp() const; /常成员函数声明private: int ZRF1,ZRF2;void ZRF:disp() coutZRF1:ZRF2endl;void ZRF:disp() const /常成员函数定义coutZRF1:ZRF2endl;int main() ZRF a(2,3); a.disp(); /调用void disp() const ZRF b(10,20); b.disp(); /调用void disp() constreturn 0;仍以森林为例,营养循环、水域保护、减少空气污染、小气候调节等都属于间接使用价值的范畴。一、环境影响评价的基础【案例5-15】计算圆周长
21、不带参数的宏定义#include using namespace std;#define PI 3.14159 /宏名PI为符号常量#define n a /宏名n将用a来替换#define LENGTH 2*PI*n / 宏名LENGTH将用2*PI*n来替换int main() int n=1; /int n=1;替换为int a=1; coutLENGTH=LENGTHendl; /替换为coutLENGTH=2*3.14159*aendl; return 0;【案例5-16】求三角形面积带参数的宏函数#include#includeusing namespace std;#define s(a,b,c) (a+b+c)/2 /带参数的宏#define area(a,b,c) sqrt(s(a,b,c)*(s(a,b,c)-a)*(s(a,b,c)-b)*(s(a,b,c)-c)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1