第五章 特殊函数与成员

上传人:豆浆 文档编号:48589193 上传时间:2018-07-17 格式:PPT 页数:37 大小:273.50KB
返回 下载 相关 举报
第五章 特殊函数与成员_第1页
第1页 / 共37页
第五章 特殊函数与成员_第2页
第2页 / 共37页
第五章 特殊函数与成员_第3页
第3页 / 共37页
第五章 特殊函数与成员_第4页
第4页 / 共37页
第五章 特殊函数与成员_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《第五章 特殊函数与成员》由会员分享,可在线阅读,更多相关《第五章 特殊函数与成员(37页珍藏版)》请在金锄头文库上搜索。

1、第五章 特殊函数与成员主讲:冯传胜 Email: 5.1 对象成员的初始化 5.2 静态成员 5.3 友元函数 5.4 const对象 5.5 数组和类 5.6 指向类成员函数的指针 5.7 求解一元二次方程5.1 对象成员的初始化 可以在一个类中说明具有某个类的类型的数据成 员,这些成员称为对象成员。 在类A中说明对象成员的一般形式如下:class A 类名1 成员名1;类名2 成员名2;类名n 成员名n;;说明对象成员是在类名之后给出对象成员的名字。 为初始化对象成员,A类的构造函数要调用这些对象 成员所在类的构造函数,A类的构造函数的定义形式 如下:A:A(参数表0):成员1(参数表1)

2、,成员2(参数表2),成员n(参数表n) /其它操作 冒号“:”后由逗号隔开的项组成成员初始化列表 参数表给出了为调用相应成员所在类的构造函 数时应提供的参数 如果初始化列表某项的参数表为空,则列表中 相应的项可以省略 对象成员构造函数的调用顺序取决于这些 对象成员在类中说明的顺序,与它们在成 员初始化列表中给出的顺序无关。 当建立A类对象时,先调用对象成员的构造函 数,初始化对象成员,然后才执行A类的构造 函数,初始化A类中的其它成员。 析构函数的调用顺序与构造函数相反。例5.1:分析下面程序中析构函数与构造函数的调用顺序 。#include using namespace std; cla

3、ss Object private:int val; public:Object():val(0)cout using namespace std; class Test private: static int x; int n; public: Test() Test(int n,int b) x=n;n=b; static int func()return x; static void sfunc(Test int Getn()return n; ; int Test:x=25; void main() cout using namespace std; class test privat

4、e:int n; public:test(int i=0)n=i;cout #include using namespace std; class Point private: double x,y; public: Point(double xx,double yy) :x(xx),y(yy) double getx()return x; double gety()return y; friend double dist(Point ; double dist(Pointdouble dy=a.y-b.y;return sqrt(dx*dx+dy*dy); void main() Point

5、 A(1,2),B(3,4);cout using namespace std; class Two; class One private: int x; public: One(int a)x=a; int Getx()return x; void func(Two; class Two private: int y; public: Two(int b)y=b; int Gety()return y; friend void One:func(Two; void One:func(Two void main() One Obj1(1);Two Obj2(2);cout using name

6、space std; class One;class Two private: int y; public: friend class One; class One private: int x; public: One(int a,Twor.y=b; void Display(Two class Base private: int x; const int a; static const int b; const int public: Base(int,int); void show() cout using namespace std; class Base private: doubl

7、e x,y; const double p; public: Base(double m,double n,double d): p(d) x=m;y=n; void show(); void show() const; ; void Base:show() cout using namespace std; class ConstFun public: int f5( ) const return 5; int Obj( ) return 45; ; void main( ) ConstFun s;const int i=s.f5( );const int j=s.Obj( );int x=

8、s.Obj( );int y=s.f5( );cout using namespace std; class Test private: int num; double fl; public: Test(int n)num=n;fl=0; Test(int n,double f):num(n),fl(f) int GetNum()return num; float GetF()return fl; void main() Test one2=1,2,*p; Test two2=Test(1,1.1),Test(2,2.2); for(int i=0;iGetNum() GetF()GetNum

9、() GetF()GetNum() GetF()*”。例5.12:使用指向类成员函数的指针。#include using namespace std; class A private: int val; public: A(int i):val(i) int value(int a)return val+a; ; void main() int(A:*pf)(int); pf=A:value;A a(20);cout*pf)(55)*pf)”中 的括号不能省略。5.7 求解一元二次方程 本节给出一个使用多文件编制求解一元二 次方程的例子。 5.7.1 设计代表方程的类 类名:FindRoot

10、属性: 三个系数(a,b,c) 描述根(x1,x2) 解的判定条件d 操作: 求方程的根Find 输出方程的根DisplayFindRoota:floatb:floatc:floatd:floatx1:doublex2:doubleFindRoot Find:void Display:void 5.7.2 设计成员函数 设计构造函数 FindRoot:FindRoot(float x,float y,float z) :a(x), b(y), c(z) d=b*b-4*a*c; 设计成员函数 Find用来根据d的数据进行求解: if(d0) /两个不同的实数解 else if(d=0) /两个

11、相同的实数解 else /两个不同的复数解 Display根据d的数据输出x1和x2的值。 5.7.3 编程实现 设计工程和文件 工程equation 头文件equation.h:声明FindRoot类并包含需要头文 件 源程序文件equation.cpp:FindRoot类的实现,应包 含头文件equation.h。 源程序文件find.cpp:主函数实现 主程序求解思想: 建立FindRoot对象Obj:三个系数属性赋初值,同时 计算解的判定条件。 对象Obj调用成员函数Find求解,结果存到相应属性 中。 对象Obj调用成员函数Display输出计算结果。 头文件equation.h/头

12、文件equation.h中的内容#if !defined(EQUATION_H) #define EQUATION_H #include #include /求解时用于数学函数sqrt using namespace std; class FindRoot private: float a,b,c; float d; double x1,x2; public: FindRoot(float,float,float); void Find(); void Display(); ; #endif注意头文件内的格式预编译指令的应用类的声明 源文件equation.cpp#include “equat

13、ion.h“ FindRoot:FindRoot(float x, float y,float z) :a(x),b(y),c(z) d=b*b-4*a*c; void FindRoot:Find() if(d0) x1=(-b+sqrt(d)/(2*a); x2=(-b-sqrt(d)/(2*a); else if(d=0) x1=x2=-b/(2*a); else x1=-b/(2*a); x2=sqrt(-d)/(2*a); void FindRoot:Display() if(d0) couta;if(a=0)getchar();return;coutb;coutc; 5.7.4 使用VC编制完整的文件 结果演示

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号