《C++程序设计简明教程(第二版)》电子教案 第5章 类

上传人:E**** 文档编号:89399536 上传时间:2019-05-24 格式:PPT 页数:26 大小:126KB
返回 下载 相关 举报
《C++程序设计简明教程(第二版)》电子教案 第5章 类_第1页
第1页 / 共26页
《C++程序设计简明教程(第二版)》电子教案 第5章 类_第2页
第2页 / 共26页
《C++程序设计简明教程(第二版)》电子教案 第5章 类_第3页
第3页 / 共26页
《C++程序设计简明教程(第二版)》电子教案 第5章 类_第4页
第4页 / 共26页
《C++程序设计简明教程(第二版)》电子教案 第5章 类_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《《C++程序设计简明教程(第二版)》电子教案 第5章 类》由会员分享,可在线阅读,更多相关《《C++程序设计简明教程(第二版)》电子教案 第5章 类(26页珍藏版)》请在金锄头文库上搜索。

1、1,第5章 C+类,面向对象程序设计特征是类的定义和使用 类 对象,2,定义类的格式1,class 类名 private: 数据类型 变量名1 ; /定义数据成员(变量),用于存储数据 public: 返回值类型 函数名1(形式参数列表)/定义函数,实现类的功能 若干语句 ;,3,class cube private: int height, width, depth ; /私有数据成员,表征立方体长宽高 public: void getdata(int x, int y, int z) /成员函数,用于获得立方体的长宽高参数。 height=x; width=y; depth=z; int

2、volume() /成员函数,用于计算立方体的体积。 return height*width*depth; ;,例 定义计算立方体体积的cube类,4,在类外定义类的成员函数 class 类名 private: 数据类型 变量名1 ; /定义数据成员(变量),用于存储数据 public: 返回值类型 函数名1(形式参数列表); /只定义函数头,不写函数内容 ; 类名: 函数名1(形式参数列表) /在成员函数名前加上类名和分隔符“:” 若干语句 类名: 函数名2(形式参数列表) 若干语句 ,定义类的格式2,5,class cube private: int height, width, dept

3、h ; public: void getdata(int x, int y, int z); /只定义函数头,不写函数内容 int volume() ; ; cube:getdata(int x, int y, int z) /编写getdata函数内容 height=x; width=y; depth=z; cube:volume() /编写volume函数内容 return height*width*depth; ,例 在类外定义cube类的成员函数,6,一般来说,首先为类创建对象,然后使用类。 void main() cube a; /创建对象 a.getdata(10,20,30);

4、/获得对象a的长宽高 couta.volume(); /计算和输出对象a的体积 class cube private: int height, width, depth; public: void getdata(int x, int y, int z) height=x; width=y; depth=z; int volume() return height*width*depth; ;,对象与类的使用,7,构造函数:类名( ) 用于初始化新建对象。 析构函数:类名( ) 用于删除对象。 一个类中只能有一个析构函数。析构函数没有参数,不返回任何值。 程序5.1 构造函数和析构函数举例 cl

5、ass cube public: cube(int ht, int wd, int dp); /构造函数 height=ht; width=wd;depth=dp; cout“Constructor called“endl; cube()cout“Destructor called“endl; /析构函数 int volume()return height* width*depth; void main() cube cubeone(10,20,30); /创建cube类对象,自动调用构造函数。 coutcubeone.volume()endl;/计算并输出立方体的值 ,构造函数与析构函数,8

6、,为类定义多个构造函数称为构造函数重载。 class cube /程序5.2 构造函数重载举例 int height=0,width=0,depth=0; public: cube()cout“Constructor1 called“endl; /定义无参数构造函数 cube(int ht, int wd, int dp) /定义有参数构造函数 height=ht; width=wd; depth=dp; cout“Constructor2 called“endl; int volume()return height*width*depth; ; void main() cube cubeon

7、e(10,20,30); /调用带参数的构造函数 cube cubetwo; /调用不参数的构造函数 coutcubeone.volume()endl; coutcubetwo.volume()endl; ,构造函数重载,9,程序5.3 带缺省值的构造函数。 class cube int height,width,depth; public: cube(int ht=1, int wd=2, int dp=3) /缺省值 height=ht; width=wd; depth=dp; cout“Constructor called“endl; int volume() return height

8、*width*depth; ; void main() cube cubeone(10,20,30); cube cubetwo; coutcubeone.volume()endl; coutcubetwo.volume()endl; ,有缺省值的构造函数,10,用关键字inline修饰的函数称为在线函数或内联扩展函数。 在程序中多次调用inline函数时,函数源代码被复制到调用处。 inline函数可以避免调用函数的开销,提高程序运行速度。 程序5.6 inline函数举例。 inline void voutput() cout“This is an inline function!“end

9、l; void main() int t; for(t=0;t2;t+) voutput(); 程序输出结果: This is an inline function! This is an inline function!,在线函数(inline),11,class cube /程序5.7 public: cube(int ht , int wd , int dp) height=ht; width=wd; depth=dp; inline int cube:volume() return height*width*depth; private: int height,width,depth

10、; ; void main() cube cubeone(10,20,30); coutcubeone.volume()endl; 程序输出结果: 6000,inline成员函数举例,12,在类中可以定义多个名称相同、参数不同的方法,称为方法重载。 参数不同可以是个数不同或类型不同。 程序5.8 成员函数重载举例。 class cube public: int volume(int ht, int wd ) return ht*wd; int volume(int ht, int wd, int dp) height=ht;width=wd;depth=dp;return height*wid

11、th*depth; private: int height,width,depth ; ; void main() cube cubeone; coutcubeone.volume(10,20)cubeone.volume(10,20,30)endl; ,成员函数重载,13,用static修饰的类成员为静态成员。有静态数据成员和静态成员函数。 静态成员只有一个副本,是共享的成员。 程序5.10 静态数据成员举例。 class count static int counter; public: void set_count(int k) counter=k; void display() cou

12、tcounterendl; ; int count:counter=0; void main() count a,b; a.display(); b.display(); a.set_count(10); a.display(); b.display(); ,静态类成员,程序输出结果: 0 0 10 10,14,当函数只访问类的静态数据成员时,可定义为静态成员函数。 静态成员函数没有this指针,所以不能访问非静态数据成员 要访问非静态数据成员,需要通过对象访问,静态成员函数,15,class score /统计一个班的成绩 int volume; static int totalvolume

13、; public: score(int) volume=x; totalvolume+=x; int fvolume() return volume; static int ftotalvolume() return totalvolume; ; int score:totalvolume=0;,程序5.12 静态成员函数举例,void main() int s,count=0; score *p; while(1) cins; if(s=-1) break; p=new score(s); delete p; count+; coutscore:ftotalvolume(); cout en

14、dl; coutscore:ftotalvolume()/count; coutendl; ,16,this指针,this是指向正被操作对象的指针。 class location /程序5.18 使用this指针举例。 int x,y; public: location(int xx,int yy) x=xx; y=yy; void assign(location p) if (this!= ,17,new运算符,用new运算符动态分配存储空间 使用delete运算符释放存储空间 new运算符分配存储空间的格式: 指针标识符=new 类型名(初始化值); 例如: int *p,*q; p=ne

15、w int; q=new int(10); 例如:创建数组类型的指针变量 int *p; p=new int10; /创建有十个元素的数组。 例如:创建多维数组类型的指针变量 int *p; p=new int23;/创建2行3列的数组。,18,new运算符优越性,(1)new运算符自动计算分配的存储空间的大小。 (2)new运算符自动返回正确的指针类型。 (3)new运算符可以对分配的对象进行初,19,delete运算符,使用delete运算符释放存储空间。 格式:delete 指针名; 例如: int *q; q=new int(10); delete q; /释放q指向的存储空间 再如: int *p; p=new int10; /创建有十个元素的数组。 delete p; /释放p数组。,20,new和delete与构造函数和析构函数的关系,class date /程序5.20 int mo,da,yr; public: date() cout“date constructor“endl; date()cout“date destructor“e

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

当前位置:首页 > 高等教育 > 大学课件

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