




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第五章 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 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+) 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() 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 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; /声明全局变量,具有静态生存期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:ShowTime() 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 coutSetted 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; /通过类名访问静态成员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 int 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 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; 【案例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 ? 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; 【案例5-11】判断圆柱体和立方体的颜色是否相同多个类共享友元函数#include using namespace std; 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); /声明为Cylinder的友元函数; 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; 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) ; public : 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 two Triangles: twoarea(tl,t2) endl; else cout Cannot form a Triangle endl; return 0;【案例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 endl;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 disp() 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】计算圆周长不带参数的宏定义#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) /带参数的宏int main() float x=4,y=5,z=6; coutThe area is :area(x,y,z)endl; /编译时发生宏替换 coutThe area is :area(5,4,3)endl; /编译时发生宏替换 return 0;【案例5-17】求最小值带参数的宏函数和内联函数#includeusing namespace std;inline min(int x,int y,int z ) /内联函数 int min=x; if(ymin) min=y; if(zmin) min=z; return min;#define MIN(a,b) (a)(b)?(a):(b) /宏函数#define min(a,b,c) MIN(a,MIN(c,b) /宏函数int main() coutThe min value is:(min)(4,5,3.0)endl; /调用内联函数 coutThe min value is:min(5.0,4,3.0)endl; /调用宏函数 return 0;【案例5-18】程序调试阶段的条件编译#ifndef-#else-#endif#includeusing namespace std;#define RUN /语句1:在调试程序时使之成为注释行int main() int x=1,y=2,z=3; #ifndef RUN /语句2:本行为条件编译命令 coutx=x,y=y,z=z; /语句3:在调试程序时需要输出信息 #endif /语句4:本行为条件编译命令 coutx*y*z=x*y*zendl; return 0;【案例5-19】综合案例统计银行存款问题#include using namespace std;class BBank; /前向引用声明class GBank; /前向引用声明class CBank /声明中国银行类 int balance;public : CBank() balance=0; /默认构造函数 CBank(int b) balance=b; /构造函数 void getbalance() cout balance; void disp() cout T
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030年中国荞麦方便面市场现状分析及前景预测报告
- 2025至2030年中国色织棉涤纶提花床单布市场现状分析及前景预测报告
- 2025至2030年中国自动按平水准仪市场现状分析及前景预测报告
- 2025至2030年中国耐醇硬胶丝印油墨市场现状分析及前景预测报告
- 运动损伤康复班会课件
- 肉类加工工艺流程
- 临床β-羟丁酸(β-Hb)检测方法及意义
- 文身护理查房
- 会咽谷良性肿瘤的治疗及护理
- 压力与情绪管理万育良
- 企业专利管理办法合集
- 非婚生子女抚养权协议书
- 新能源汽车动力系统故障诊断与维护技术创新研究
- 2025村后备干部考试题库(含答案)
- 《电工技能与实训》校本教材
- 安全生产考核巡查办法全文
- 支气管镜并发症应对护理
- 百世快运质量管理制度
- 国军标风险管理制度
- 气道阻塞急救处理方法
- 2025年陕西高考化学试卷试题真题及答案详解(山西宁夏青海适用)
评论
0/150
提交评论