当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

中国铁道出版社:《C++面向对象程序设计》课程教学资源(PPT课件讲稿)第8讲 构造函数和析构函数

资源类别:文库,文档格式:PPT,文档页数:17,文件大小:480KB,团购合买
1、构造函数; 2、析构函数;
点击下载完整版文档(PPT)

第8讲构造函数和析构函数 教学目的与要求: 了解类对象的创建和撤消过程。 掌握构造函数和析构函数的定义和使用 教学内容提要: 1、构造函数 2、析构函数 教学重点:构造函数和析构函数的定义和使用 教学难点:构造函数的定义和使用。 教学进度:P61~P70 教学过程:

第8讲 构造函数和析构函数 •教学目的与要求: 了解类对象的创建和撤消过程。 掌握构造函数和析构函数的定义和使用。 •教学内容提要: 1、构造函数; 2、析构函数; •教学重点:构造函数和析构函数的定义和使用。 •教学难点:构造函数的定义和使用。 •教学进度:P61~P70 •教学过程:

(81构造函数】 在声明对象时,也可以对其数据成员初始化。对象数据成员的初始化需要 通过类的特殊成员函数来进行,这个成员函数就是构造函数 构造函数是类的特殊成员函数,用于在声明类的对象时对其进行初始化。 构造函数的函数名应与类名相同,构造函数中的参数根据需要可有可无。 不能为其指定返回值类型,甚至连vod也不可以。 构造函数应被声明为公有函数,它只能在创建类的对象时由系统自动调 用,对对象进行初始化,程序的其他部分不能调用 例8-1圆柱体类构造函数不带参数的情形 class cylinder public Cylinder(; void setcylinder(doubler, double h);

在声明对象时,也可以对其数据成员初始化。对象数据成员的初始化需要 通过类的特殊成员函数来进行,这个成员函数就是构造函数。 构造函数是类的特殊成员函数,用于在声明类的对象时对其进行初始化。 构造函数的函数名应与类名相同,构造函数中的参数根据需要可有可无。 不能为其指定返回值类型,甚至连void也不可以。 构造函数应被声明为公有函数,它只能在创建类的对象时由系统自动调 用,对对象进行初始化,程序的其他部分不能调用。 class Cylinder { public: Cylinder( ); void setcylinder(double r,double h); 例 8-1 圆柱体类构造函数不带参数的情形 【 8.1 构造函数】

double getradius(; double getheight0; double volumed; double surface area 0; private: double radius: double height; Cylinder: Cylinder( radius=5.0 height=10.0 例82(圆柱体类构造函数带有参数的情形 class cylinder public: Cylinder(doubler, double h); void setcylinder(double r, double h);

double getradius(); double getheight(); double volume(); double surface_area(); private: double radius; double height; }; Cylinder::Cylinder( ) { radius=5.0; height=10.0; } class Cylinder { public: Cylinder(double r,double h); void setcylinder(double r,double h); 例 8-2 圆柱体类构造函数带有参数的情形

double getradius(; double getheight0; double volumed; double surface area 0; private: double radius: 米 double height; Cylinder: Cylinder(double r, double h radius-r, height=h 在声明对象时,就必须利用实参对对象进行初始化 例如 ylinder cylinder(2.0, 3.0) Cylinder cylinder;∥错误,没有提供用于初始化的实参 在声明对象时,如果相应的构造函数没有形参,括号应该省略 (续)

double getradius(); double getheight(); double volume(); double surface_area(); private: double radius; double height; }; Cylinder::Cylinder(double r,double h) { radius=r; height=h; } 在声明对象时,就必须利用实参对对象进行初始化。 例如: Cylinder cylinder1(2.0,3.0); Cylinder cylinder1; //错误,没有提供用于初始化的实参 在声明对象时,如果相应的构造函数没有形参,括号应该省略。 (续)

若是声明对象数组,应该采用下述形式: Cylinder cylinder [3]= Cylinder (1.0,2.0), Cylinder(3.0,4.0), Cylinder(5.0, 6.0) 构造函数也可以定义为内联函数,可以带默认形参值,也可以重载 例83(构造函数的重载 第1部分 这个程序在【例3-9】的基础上增加了构造函数 /EXAMPLE.H #includesiosteam.h> 说明:对类 Cylinder的构造 class cylinder 函数进行了重载。 public: lindert ∥构造函数 没有形参,函 Cylinder(doubler, double h); /构造函数数体也为空 void setcylinder(double r, double h) double getradius( 有两个形参。 return radius:

若是声明对象数组,应该采用下述形式: Cylinder cylinder1[3]={ Cylinder (1.0,2.0),Cylinder(3.0,4.0),Cylinder(5.0,6.0)}; 构造函数也可以定义为内联函数,可以带默认形参值,也可以重载。 //这个程序在【例3 - 9】的基础上增加了构造函数 //EXAMPLE.H #include class Cylinder{ public: Cylinder(){} //构造函数 Cylinder(double r,double h); //构造函数 void setcylinder(double r,double h); double getradius() { return radius; } 构造函数的重载 第1部分 说明:对类Cylinder的构造 函数进行了重载。 没有形参,函 数体也为空。 有两个形参。 例 8-3

double getheighto 第1部分 return height: double volume double surface area 0; private: double radius: double height /EXAMPLE. CPP include 'EXaMpLe. h 第2部分 const double pl3.1415926: Cylinder: Cylinder(double r, double h radlus-r, height=h (续)

double getheight() { return height; } double volume(); double surface_area(); private: double radius; double height ; }; //EXAMPLE.CPP #include″EXAMPLE.h″ const double PI=3.1415926; Cylinder::Cylinder(double r,double h) { radius=r; height=h; } 第 1部分 第 2部分 (续)

void Cylinder: setcylinder(double r, double h 第2部分 radius=r: height=h double cylinder: volumed double vol: volPIradius * radius*height; return vol; double cylinder: surface areaO double area; area=2plradius*height+2*plradius radius return area; 带有两个实参,系统将自动调用第2个 void maino 构造函数给对象 cylinder初始化。 Cylinder cylinder(5.0, 10.0), cylinder2; ∥d明对象 cylinder2. setcylinder(10. 2, 15.6) 在声明时调用第一个构造函数。 续

void Cylinder::setcylinder(double r,double h) { radius=r; height=h; } double Cylinder:: volume() { double vol; vol=PI*radius*radius*height; return vol; } double Cylinder::surface_area() { double area; area=2*PI*radius*height+2*PI*radius*radius; return area; } void main() { Cylinder cylinder1(5.0,10.0),cylinder2; //声明对象 cylinder2.setcylinder(10.2,15.6); 第2部分 带有两个实参,系统将自动调用第2个 构造函数给对象cylinder1初始化。 在声明时调用第一个构造函数。 (续)

第2部分 cout<<"The radius of the first cylinder is: \t'"<<cylinder. getradius( <<endl cout<<"The height of the first cylinder is: \t"<<cylinder. getheighto<<endl; cout<<"The volume of the first cylinder is: \ t"<<cylinder volume<<endl cout<<"The surface area of the first cylinder is: \ t"<<cylindersurface areao cout<<"The radius of the second cylinder is:\ t"<<cylinder2. getradiusO<<endl; cout<<"The height of the second cylinderis: t"<<cylinder2. getheighto<<endl; cout<<"The volume of the second cylinderis: \t"<<cylinder2 volume<<endl; cout<<"The surface area of the second cylinder is:\t cylinder2 surface area(<<endl 系统在创建每一个对象时都需要自动调用相应的构造函数 注自动生成的默认构造函数不带任何参数,在函数体内也没有任何 语句,不执行任何操作。 其形式如下:类名::类名() (续)

注 自动生成的默认构造函数不带任何参数,在函数体内也没有任何 语句,不执行任何操作。 cout<< ″The radius of the first cylinder is:\t″<<cylinder1.getradius()<<endl; cout<< ″The height of the first cylinder is:\t ″<<cylinder1.getheight()<<endl; cout<< ″The volume of the first cylinder is:\t ″<<cylinder1.volume()<<endl; cout<< ″The surface area of the first cylinder is:\t ″<<cylinder1.surface_area()<<endl; cout<< ″The radius of the second cylinder is:\t ″<<cylinder2.getradius()<<endl; cout<< ″The height of the second cylinder is:\t ″<<cylinder2.getheight()<<endl; cout<< ″The volume of the second cylinder is:\t ″<<cylinder2.volume()<<endl; cout<< ″The surface area of the second cylinder is:\t ″<<cylinder2.surface_area()<<endl; } 第2部分 系统在创建每一个对象时都需要自动调用相应的构造函数。 其形式如下:类名::类名() {} (续)

例84(构造函数的调用 /EXAMPLE3 13.H # include≤ iostream. h> class a public AO cout<<AO is called"<<endl A(int x, int y); private: int a. b: A: A(int x, int y) b-y; cout<<"A(int x, int y) is called"<<endl; cout<<"A="<<a<<'t<<B="<<b<<endl

//EXAMPLE3_13.H #include class A { public: A() { cout<<″A() is called″<<endl; } A(int x,int y); private: int a,b; }; A::A(int x,int y) { a=x; b=y; cout<<″A(int x,int y) is called″<<endl; cout<<″A=″<<a<<′\t′<<″B=″<<b<<endl; } 例 8-4 构造函数的调用

/EXAMPLE3 13. CPP #include EXAMPlE3 13.h void main Ab(10,10); AO is called A(int x, int y) is called A=10B=10 (82析构函数 C++类中还有一个特殊的成员函数,即析构函数。析构函数在对象终止时由 系统自动调用,以释放分配给对象的内存。析构函数的函数名应为类名前加 “~”,析构函数中没有参数,也不能为之指定返回值类型。 注一个类内只能声明一个析构函数。它也是公有的成员函数。如果类 中没有声明,编译器也会自动生成一个带空函数体的析构函数

A() is called A(int x,int y) is called A=10 B=10 //EXAMPLE3_13.CPP #include″EXAMPLE3_13.H″ void main() { A a; A b(10,10); } C++类中还有一个特殊的成员函数,即析构函数。析构函数在对象终止时由 系统自动调用,以释放分配给对象的内存。析构函数的函数名应为类名前加 “~”,析构函数中没有参数,也不能为之指定返回值类型。 注 一个类内只能声明一个析构函数。它也是公有的成员函数。如果类 中没有声明,编译器也会自动生成一个带空函数体的析构函数。 【 8.2 析构函数】

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共17页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有