第六章C程序的结构.ppt

上传人:本田雅阁 文档编号:3455419 上传时间:2019-08-27 格式:PPT 页数:62 大小:402.04KB
返回 下载 相关 举报
第六章C程序的结构.ppt_第1页
第1页 / 共62页
第六章C程序的结构.ppt_第2页
第2页 / 共62页
第六章C程序的结构.ppt_第3页
第3页 / 共62页
第六章C程序的结构.ppt_第4页
第4页 / 共62页
第六章C程序的结构.ppt_第5页
第5页 / 共62页
点击查看更多>>
资源描述

《第六章C程序的结构.ppt》由会员分享,可在线阅读,更多相关《第六章C程序的结构.ppt(62页珍藏版)》请在三一文库上搜索。

1、第六章 C+程序的结构,本章主要内容,作用域与可见性 对象的生存期 数据与函数 静态成员 共享数据的保护 友元 编译预处理命令 多文件结构和工程,作用域: 函数原型的作用域 块作用域 类作用域 文件作用域,一、作用域与可见性,1、函数原型的作用域,函数原型中的参数,其作用域始于 “(“,结束于“)“。 例如,设有下列原型声明: double Area(double radius);,radius 的作用域仅在于此,不能用于程序正文其它地方,因而可有可无。,double Area(double);,2、块作用域,在块中声明的标识符,其作用域自声明处起,限于块中,例如: void fun(int

2、a) int b(a); cinb; if (b0) int c; ,3、类作用域,类作用域作用于特定的成员名。 类C的成员M具有类作用域,对M的访问方式如下: 如果在C的成员函数中没有声明同名的局部作用域标识符,那么在该函数内可以访问成员M。 通过表达式c(对象).M或者C:M访问。 通过表达式prt-M class CBox public: double Volume() const return m_Length*m_Width*m_Height; private: double m_Length; / Length of a box in inches double m_Width;

3、/ Width of a box in inches double m_Height; / Height of a box in inches ;,4、文件作用域,不在前述各个作用域中出现的声明,具有文件作用域,这样声明的标识符的作用域开始于声明点,结束于文件尾。 #include using namespace std; int i; /i是全局变量,文件作用域 int main() i= 5; int i= 7; /局部变量,块作用域 coutiendl; return 0; ,可见性: 可见性是从对标识符的引用的角度来谈的概念 可见性表示从内层作用域向外层作用域“看”时能看见什么。 如果

4、标识在某处可见,则就可以在该处引用此标识符。,可见性的一般规则:,标识符应声明在先,引用在后。 如果某个标识符在外层中声明,且在内层中没有同一标识符的声明,则该标识符在内层可见。 对于两个嵌套的作用域,如果在内层作用域内声明了与外层作用域中同名的标识符,则外层作用域的标识符在内层不可见。 在同一作用域内的对象名、函数名、枚举常量名会隐藏同名的类名或枚举类型名。 重载的函数可以有相同的函数名。,#include using namespace std; int i; /文件作用域 int main() i=5; int i; /块作用域 /全局的i被隐藏 i=7; cout“i=“iendl;

5、/输出7 cout“i=“i; /输出5 return 0; ,例 5.1,二、对象的生存期,对象(包括简单变量)从产生到结束的这段时间就是它的生存期。在对象生存期内,对象将保持它的状态(即数据成员的值),变量也将保持它的值,直到被更新为止。对象(包括简单变量)的生存期可以分为静态生存期和动态生存期两种。,静态生存期,静态生存期与程序的运行期相同。 在文件作用域中声明的对象具有这种生存期。(全局变量比较危险,在程序中任何位置都可以访问全局变量,使得他们非常容易被意外修改) 在函数内部声明静态生存期对象,要冠以关键字static 。 例如:声明函数中的静态变量 static int count

6、= 0;,#include using namespace std; int i=5; /文件作用域/i具有静态生存期 void record(); int main() cout“i=“iendl; record(); return 0; void record(void) static int count = 0; /静态生存期 count +; ,例,动态生存期,块作用域中声明的,没有用static修饰的对象是动态生存期的对象(习惯称局部生存期对象)。 开始于程序执行到声明点时,结束于命名该标识符的作用域结束处。,#include using namespace std; void fu

7、n(); int main() fun(); fun(); void fun() static int a=1; int i=5; a+; i+; cout“i=“i“,a=“aendl; ,运行结果: i=6, a=2 i=6, a=3 i是动态生存期 a是静态生存期,例,例5-2 变量的生存期与可见性,#include using namespace std; int i=1; / i 为全局变量,具有静态生存期。 int main() static int a; / 静态局部变量,有全局寿命,局部可见。 默认初值始终是0 int b=-10; / b, c为局部变量,具有动态生存期。 i

8、nt c=0; void other(void); /函数原型声明 cout“-MAIN-n“; cout“ i: “i“ a: “a“ b: “b“ c: “cendl; /1 0 -10 0 c=c+8; other(); /33 4 0 15 cout“-MAIN-n“; cout“ i: “i“ a: “a“ b: “b“ c: “cendl; /33 0 -10 8 i=i+10; /43 other(); /75 6 4 15 return 0; ,运行结果: -MAIN- i: 1 a: 0 b: -10 c: 0 -OTHER- i: 33 a: 4 b: 0 c: 15 -M

9、AIN- i: 33 a: 0 b: -10 c: 8 -OTHER- i: 75 a: 6 b: 4 c: 15,void other(void) static int a=2; static int b; / a,b为静态局部变量,具有全局寿命,局部可见。 /只第一次进入函数时被初始化。 int c=10; / C为局部变量,具有动态生存期, /每次进入函数时都初始化。 a=a+2; /第一次调用,a = 4 i=i+32; /第一次调用,i = 33 c=c+5; /第一次调用,c = 15 cout“-OTHER-n“; cout“ i: “i“ a: “a“ b: “b“ c: “c

10、endl; /第一次调用,i: 33 a:4 b: 0 c: 15 b=a; /第一次调用b =4 ,17,例5-3具有静态、动态生存期对象的时钟程序,#include using namespace std; class Clock /时钟类声明 public: /外部接口 Clock(); void SetTime(int NewH, int NewM, int NewS); /三个形参均具有函数原型作用域 void ShowTime(); Clock() private: /私有数据成员 int m_Hour, m_Minute, m_Second; ;,/时钟类成员函数实现 Clock

11、:Clock() /构造函数 m_Hour=0; m_Minute=0; m_Second=0; void Clock:SetTime(int NewH, int NewM, int NewS) m_Hour=NewH; m_Minute=NewM; m_Second=NewS; void Clock:ShowTime() coutm_Hour“:“m_Minute“:“ m_Secondendl; ,20,Clock globClock; /声明对象globClock, /具有静态生存期,文件作用域 int main() /主函数 cout“First time output:“endl;

12、/引用具有文件作用域的对象: globClock.ShowTime(); /对象的成员函数具有类作用域 globClock.SetTime(8,30,30); Clock myClock(globClock); /声明具有块作用域的对象myClock cout“Second time output:“endl; myClock.ShowTime(); /引用具有块作用域的对象 ,21,程序的运行结果为: First time output: 0:0:0 Second time output: 8:30:30,22,三、类的静态成员,类的数据成员和函数成员都可以被声明为static。 类的静态数

13、据成员 类的静态函数成员,静态数据成员 用关键字static声明 该类的所有对象维护该成员的一个实例,Class CBox public: static int objectCount; private: double m_length; double m_width; double m_height; ,m_length m_width m_height,m_length m_width m_height,m_length m_width m_height,对象3,对象2,对象1,objectCount,每个静态数据成员的副本在所有的类 类型对象间共享,- 必须在类外定义和初始化,用(:)来

14、指明所属的类。初始化必须在类定义外部进行。 int CBox:objectCount = 0;(在声明任何对象前初始化static成员objectCount 的) 例 给CBox类中添加一个static成员,以计算生成对象的数目)这个成员是public型的 #include using std:cout; using std:endl;,class CBox / Class definition at global scope public: static int objectCount; / Count of objects in existence / Constructor defini

15、tion CBox(double lv, double bv = 1.0, double hv = 1.0) cout endl “Constructor called.“; m_Length = lv; / Set values of m_Width = bv; / data members m_Height = hv; objectCount+; CBox() / Default constructor cout endl “Default constructor called.“; m_Length = m_Width = m_Height = 1.0; objectCount+; /

16、Function to calculate the volume of a box double Volume() const return m_Length*m_Width*m_Height; private: double m_Length; / Length of a box in inches double m_Width; / Width of a box in inches double m_Height; / Height of a box in inches ; int CBox:objectCount = 0; / Initialize static member of CB

17、ox class,int main() CBox boxes5; / Array of CBox objects declared CBox cigar(8.0, 5.0, 1.0); / Declare cigar box cout endl endl “Number of objects (through class) = “ CBox:objectCount; cout endl “Number of objects (through object) = “ boxes2.objectCount; cout endl; return 0; ,Default constructor cal

18、led Default constructor called Default constructor called Default constructor called Default constructor called Constructor called Number of objects (through class)=6 boxes2.objectCount=6,例5-4 具有静态数据成员的 Point类,#include /private访问类型的静态数据成员 using namespace std; class Point public: Point(int xx=0, int

19、yy=0) X=xx; Y=yy; countP+; Point(Point ,静态成员,非静态函数访问静态数据成员,Point:Point(Point ,28,静态成员函数 类外代码可以使用类名和作用域操作符来调用静态成员函数。 静态成员函数只能引用属于该类的静态数据成员或静态成员函数。引用普通的非静态类成员,必须通过参数传递得到对象名,通过对象名来访问。 即使本类的任何对象都不存在,它们也能存在并被调用。这种情况下,只能使用静态数据成员。 Static void Afunction(int n) aBox .Afunction(10); CBox:Afunction(10);,具有静态数据

20、、函数成员的 Point类,#include using namespace std; class Point /Point类声明 public: /外部接口 Point(int xx=0, int yy=0) X=xx;Y=yy;countP+; Point(Point ,静态函数访问静态数据成员,Point:Point(Point /输出对象号,类名引用 ,32,class A public: static void f(A a); private: int x; void A:f(A a) couta.x; ,静态函数访问非静态数据成员,类中成员之间的访问,静态数据成员,非静态数据成员,

21、非静态函数成员,静态函数成员,Ok,Ok,通过对象名作为参数访问,非内联,且函数体的定义应该与静态成员的初始化在同一个源程序文件中,四、类的友元,友元是C+提供的一种破坏数据封装和数据隐藏的机制。 通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。 可以使用友元函数和友元类。 为了确保数据的完整性,及数据封装与隐藏的原则,建议尽量不使用或少使用友元。,友 元,1、友元函数,有些情况下,由于这样或那样的原因,我们希望某些虽然不是类成员的函数能够访问类的所有成员他们拥有特殊权限。友元函数是在类声明中由关键字friend修饰说明的非成员函数,在它的函数体中能够通过

22、对象名访问 private 和 protected成员。 作用:增加灵活性,使程序员可以在封装和快速性方面做合理选择。 访问对象中的成员必须通过对象名。 注意:友元函数不是类的成员,因此访问特性不适用于他们,这些函数只是拥有特殊权限的普通全局函数。,例5-6 使用友元函数计算两点距离,#include #include using namespace std; class Point /Point类声明 public: /外部接口 Point(int xx=0, int yy=0) X=xx;Y=yy; int GetX() return X; int GetY() return Y; fri

23、end float Distance(Point ,友 元,double Distance( Point ,36,例:生成友元函数计算表面积 / Creating a friend function of a class计算表面积 #include using std:cout; using std:endl; class CBox / Class definition at global scope public: / Constructor definition CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) cout end

24、l “Constructor called.“; m_Length = lv; / Set values of m_Width = bv; / data members m_Height = hv; / Function to calculate the volume of a box double Volume() return m_Length*m_Width*m_Height; ,private: double m_Length; / Length of a box in inches double m_Width; / Width of a box in inches double m

25、_Height; / Height of a box in inches / Friend function friend double BoxSurface(CBox aBox); ; / friend function to calculate the surface area of a Box object double BoxSurface(CBox aBox) return 2.0*(aBox.m_Length*aBox.m_Width + aBox.m_Length*aBox.m_Height + aBox.m_Height*aBox.m_Width); ,int main() C

26、Box match(2.2, 1.1, 0.5); / Declare match box CBox box2; / Declare box2 - no initial values cout endl “Volume of match = “ match.Volume(); cout endl “Surface area of match = “ BoxSurface(match); cout endl “Volume of box2 = “ box2.Volume(); cout endl “Surface area of box2 = “ BoxSurface(box2); cout e

27、ndl; return 0; ,2、友元类,若一个类A为另一个类B的友元,则此类A的所有成员函数都能访问对方类的私有和保护成员。 声明语法:将友元类名在另一个类中使用friend修饰说明。 class B friend class A; /声明A为B的友元类 ,假如已经定义了一个表示瓶子的CBottle类: class CBottle 、 public: CBottle(double height,double diameter) m_ height = height; m_ diameter = diameter; private: double m_height; double m_ di

28、ameter; friend CCarton:CCarton(const CBottle / Height of a box in inches ,出错,CBottle类的数据成员是private外部不能访问!,友元关系是单向的,友元关系不是互惠的。如果声明B类是A类的友元,B类的成员函数就可以访问A类的私有和保护数据,但A类的成员函数却不能访问B类的私有、保护数据。 友元关系也是不可继承的。,五、共享数据的保护,对于既需要共享,又需要防止改变的数据应该声明为常量。 const引用 类的const对象 类的const 成员函数,常类型,常类型的对象必须进行初始化,而且不能被更新。 常引用:被引

29、用的对象不能被更新。 const 类型说明符 &引用名 常对象:必须进行初始化,不能被更新。 const 类名 对象名 常数组:数组元素不能被更新。 类型说明符 const 数组名大小. 常指针:指向常量的指针。,例5-7常引用做形参,#include using namespace std; void display(const double ,共享数据的保护,常对象举例,class A public: A(int i,int j) x=i; y=j; . private: int x,y; ; const A a(3,4); /a是常对象,不能被更新,#include using std:

30、cout; using std:endl; class CBox / Class definition at global scope public: / Constructor definition CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) cout Volume() xBox.Volume(); private: double m_Length; / Length of a box in inches double m_Width; / Width of a box in inches double m_Height;

31、/ Height of a box in inches ;,int main() CBox match(2.2, 1.1, 0.5); / Declare match box CBox cigar(8.0, 5.0,1.0); / Declare cigar box const CBox cigar(8.0, 5.0,1.0); if(cigar.Compare(match) cout endl “match is smaller than cigar“; else cout endl “match is equal to or larger than cigar“; cout endl; r

32、eturn 0; 被声明为const的对象其this指针也是const,因此不允许调用任何没有将传递给它的this指针指定为const的成员函数。如何使成员函数中的this指针成为const?,为了使某个成员函数中的this指针成为const,必须在类定义内将该函数声明为const。 常成员函数 使用const关键字说明的函数。 常成员函数不更新对象的数据成员。 常成员函数说明格式: 类型说明符 函数名(参数表)const; 这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。 const关键字可以被用于参与对重载函数的区分 通过常对象只能调用它的常成员函数。常成员

33、函数不能调用同类的非const成员函数,因为那样有可能修改当前对象. 常数据成员 使用const说明的数据成员。,Class CBox double Volume() const return m_Length*m_Width*m_Height; / Function to compare two boxes which returns true (1) / if the first is greater than the second, and false (0) otherwise int Compare(CBox xBox) const return this-Volume() xBox

34、.Volume(); 如果const成员函数的定义出现在类的外部,那么: double Volume() const; doubleCBox: Volume() const ,应该总是将所有不修改当前类对象的成员函数声明为const,六、多文件结构和编译预处理命令,1、一个源程序可以划分为多个源文件: 类声明文件(.h文件) 类实现文件(.cpp文件) 类的使用文件(main()所在的.cpp文件) 利用工程来组合各个文件。 例如:建立CBox类,用Box.h和Box.cpp实现,在Box.cpp开头放入#include,在主函数文件开头放入#include,这样Box.cpp和main.cp

35、p文件分别编译生成各自的目标文件.obj。再连接生成可执行文件.exe。,2、外部变量和外部函数,外部变量是可以为多个源文件所共享的全局变量。用extern关键字来说明。 /1.cpp int i=3; int main() i+; /2.Cpp extern int i;/声明一个在其他文件中定义的外部变量。 如果在全局作用域中定义一个static变量,那么它的作用范围仅限于定义它的编译单元。 外部函数 在类之外定义的函数,都是全局的,可以在不同的文件中使用,但调用前要声明函数原型。或者用extern修饰。,3、编译预处理命令,#include 文件包含指令 将一个源文件嵌入到当前源文件中该

36、点处。 #include 按标准方式搜索,文件位于C+系统目录的include子目录下 #include“文件名“ 首先在当前目录中搜索,若没有,再按标准方式搜索。 #define 宏定义指令 定义符号常量,很多情况下已被const定义语句取代。 定义带参数宏,已被内联函数取代。 #undef 删除由#define定义的宏,使之不再起作用。,如何使用多文件结构,文件-新建-工程-windows console Application 在class view中,选择工程,右键选择Add class. 在弹出的对话框中写入类的名字。系统自动生成.h(头文件)及.cpp文件。 在.h文件中有一条预处

37、理指令: #pragma once 该指令的作用是防止编译器在编译过程中多次将该文件打开并嵌入到源代码中。 这样不会出现某个类的多次定义。这条指令时Microsoft特有的指令。在其他开发环境中不支持。 如果需要在其他环境中编译,可以在头文件中使用: #ifndef BOX_H #define BOX_H code #endif,#include using namespace std; enum ErrorType invalidArraySize,memoryAllocationError,indexOutOfRange; char* errorMsg = “invalid Array S

38、ize“,“memory Allocation Error“,“indexOutOfRange“; class Point public: Point() X=Y=0; cout“Default Constructor called.“endl; Point(int xx,int yy) X=xx; Y=yy; cout “Constructor called.“endl; Point() cout“Destructor called.“endl; int GetX() return X; int GetY() return Y; void Move(int x,int y) X=x; Y=y

39、; private: int X,Y; ;,class ArrayOfPoints public: void Error(ErrorType erroe,int badIndex = 0) const; ArrayOfPoints(int n) numberOfPoints=n; points=new Pointn; ArrayOfPoints(ArrayOfPoints,void ArrayOfPoints: Error(ErrorType error,int badIndex ) const coutnumber; ArrayOfPoints pointsArray1(number); /

40、创建对象数组 pointsArray1.Element(0).Move(5,10); /通过指针访问数组元素的成员 pointsArray1.Element(1).Move(15,20); /通过指针访问数组元素的成员 ArrayOfPoints pointsArray2(pointsArray1); /创建对象数组副本 coutaa; ,/* STRCPY.C: This program uses strcpy * and strcat to build a phrase. */ #include #include void main( void ) char message80; strc

41、py(message, “Hello world from “ ); strcat(message, “strcpy “ ); strcat(message, “and “ ); strcat(message, “strcat!“ ); cout message ; Output Hello world from strcpy and strcat!,strcat strcpy,Value Relationship of string1 to string2 0 string1 greater than string2 char* a = new char20; a = “I aike cak

42、es“; char* b = new char21; b = “I like cakes“; char* c = new char22; c = “I Aike cakes“; int result = strcmp(a,b); coutresultendl; int result2 = strcmp(a,c); coutresult2endl;,output: -1 1,strcmp,Cstr(const char* initstr=“s“)/构造函数 pstr =new charstrlen(initstr)+1; strcpy(pstr,initstr); len = strlen(pstr)+1; cout“constructor called“endl; ,strlen,char ss50 =“Copywrite 1999-2000 GGV Technologies“; coutstrlwr(ss);,copywrite 1999-2000 ggv technologies“;,strlwr 返回指向实参的指针,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1