![c++课件第九章关于类和对象的进一步讨论_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-4/14/9ecac610-3753-418c-bed7-529648e38915/9ecac610-3753-418c-bed7-529648e389151.gif)
![c++课件第九章关于类和对象的进一步讨论_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-4/14/9ecac610-3753-418c-bed7-529648e38915/9ecac610-3753-418c-bed7-529648e389152.gif)
![c++课件第九章关于类和对象的进一步讨论_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-4/14/9ecac610-3753-418c-bed7-529648e38915/9ecac610-3753-418c-bed7-529648e389153.gif)
![c++课件第九章关于类和对象的进一步讨论_第4页](http://file3.renrendoc.com/fileroot_temp3/2022-4/14/9ecac610-3753-418c-bed7-529648e38915/9ecac610-3753-418c-bed7-529648e389154.gif)
![c++课件第九章关于类和对象的进一步讨论_第5页](http://file3.renrendoc.com/fileroot_temp3/2022-4/14/9ecac610-3753-418c-bed7-529648e38915/9ecac610-3753-418c-bed7-529648e389155.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.第9章 关于类和对象的进一步讨论9.1构造函数9.1.1对象的初始化在建立一个对象时,常常要做一些初始化工作,例如数据成员赋初值等。类的数据成员是不能在声明类时初始化的。#include <iostream>using namespace std;class Timeint hour;/int hour=0;int minute;int sec;void mainTime t1=14,56,30;error C2552: 't1' : non-aggregates cannot be initialized with initializer list上面这种写法是
2、错误的。假设数据成员是公有的,那么可以用初始化列表形式初始化。#include <iostream>using namespace std;class Timepublic:int hour;int minute;int sec;void mainTime t1=14,56,30;9.1.2构造函数的作用C+提供构造函数来处理对象的初始化,构造函数是一种特殊的成员函数,不需要用户来调用它,而是建立对象时自动执行。构造函数与类同名,并且没有返回类型。构造函数可以根据需要进展重载。例9.1在例8.3根底上定义构造成员函数。#include <iostream>using n
3、amespace std;class Timepublic: Time hour=0;minute=0;sec=0; void set_time; void show_time;private: int hour; int minute; int sec;void Time:set_timecin>>hour;cin>>minute;cin>>sec;void Time:show_timecout<<hour<<":"<<minute<<":"<<sec<
4、;<endl;int mainTime t1;t1.set_time;t1.show_time;Time t2;t2.show_time;return 0;10 25 5410:25:540:0:09.1.3带参数的构造函数例9.2 有两个长方体,其长、宽、高分别为112,20,25215,30,21。求它们的体积。编一个基于对象的程序,在类中用带参数的构造函数。#include <iostream>using namespace std;class Boxpublic:Boxint,int,int;int volume;private:int height;int widt
5、h;int length;Box:Boxint h,int w,int lenheight=h;width=w;length=len;int Box:volumereturnheight*width*length; int mainBox box112,25,30;cout<<"The volume of box1 is " <<box1.volume<<endl;Box box215,30,21;cout<<"The volume of box2 is " <<box2.volume<&
6、lt;endl;return 0;The volume of box1 is 9000The volume of box2 is 94509.1.4用参数初始化表对数据成员初始化#include <iostream>using namespace std;class Boxpublic:Boxint h,int w ,int len: heighth,widthw,lengthlenint volume;private:int height;int width;int length;int Box:volumereturnheight*width*length; int mainB
7、ox box112,25,30;cout<<"The volume of box1 is " <<box1.volume<<endl;Box box215,30,21;cout<<"The volume of box2 is " <<box2.volume<<endl;return 0;9.1.5构造函数的重载在一个类中可以定义多个构造函数,以便对类对象提供不同的初始化方法。例9.3#include <iostream>using namespace std;class
8、Boxpublic:Box;Boxint h,int w ,int len: heighth,widthw,lengthlenint volume;private:int height;int width;int length;Box:Boxheight=10;width=10;length=10;int Box:volumereturnheight*width*length; int mainBox box1;cout<<"The volume of box1 is " <<box1.volume<<endl;Box box215,30
9、,21;cout<<"The volume of box2 is " <<box2.volume<<endl;return 0;The volume of box1 is 1000The volume of box2 is 94509.1.6使用默认参数的构造函数例9.4#include <iostream>using namespace std;class Boxpublic:Boxint w=10,int h=10,int len=10;int volume;private:int height;int width;int
10、 length;Box:Boxint w,int h,int lenheight=h;width=w;length=len;int Box:volumereturnheight*width*length; int mainBox box1;cout<<"box1:"<<box1.volume<<endl;Box box215;cout<<"box2:"<<box2.volume<<endl;Box box315,30;cout<<"box3:"<
11、<box3.volume<<endl;Box box415,30,20;cout<<"box4:"<<box4.volume<<endl;return 0;box1:1000box2:1500box3:4500box4:9000构造函数的二义性问题:(1) 定义下面两个构造函数Box;Boxint h=10,int w=10,int len=10;在执行 Box box1;时产生二义性。(2) 定义下面构造函数Box;Boxint h=10,int w=10,int len=10;Boxint,int;在执行 Box b
12、ox1;Box box215,30;时产生二义性。(3) 定义下面两个构造函数Box;Boxint h,int w=10,int len=10;Boxint,int; 在执行 Box box1;/正确 Box box215;/调用第二个构造函数。 Box box315,30;/产生二义性。9.2析构函数析构函数的作用是在撤销对象占用的内存之前完成一些清理工作。它不返回任何值,没有函数类型,没有函数参数,不能被重载。例9.5 包含构造函数和析构函数的C+程序。#include <iostream>#include <string>using namespace std;c
13、lass Studentpublic:Studentint n,string nam,char snum=n;name=nam;sex=s;cout<<"Constructor called."<<endl; Studentcout<<"Destructor called."<<num<<endl;void displaycout<<"num:"<<num<<endl;cout<<"name:"<<
14、;name<<endl;cout<<"sex:"<<sex<<endl<<endl;private: int num; string name; char sex;int mainStudent stud110010,"Wang_li",'f'stud1.display;Student stud210011,"Zhang_fun",'m'stud2.display;return 0;Constructor called.num:10010nam
15、e:Wang_lisex:fConstructor called.num:10011name:Zhang_funsex:mDestructor called.10011Destructor called.100109.3调用构造函数和析构函数的顺序先构造的后析构,后构造的先析构。参见上例1在全局范围中定义的对象,它的构造函数在所有函数包括main执行之前调用。当main函数执行完毕或调用exit函数时,调用析构函数。2假设在函数中定义静态部分对象那么只在程序第一次调用此函数时调用构造函数。当main函数执行完毕或调用exit函数时,调用析构函数。9.4对象数组例9.6 对象数组的使用方法。#i
16、nclude <iostream>using namespace std;class Boxpublic:Boxint h=10,int w=12,int len=15 :heighth,widthw,lengthlen int volume;private:int height;int width;int length;int Box:volumereturnheight*width*length;int mainBoxa3=Box10,12,15,Box15,18,20,Box16,20,26;cout<<"volume of a0 is "&l
17、t;<a0.volume<<endl;cout<<"volume of a0 is "<<a1.volume<<endl;cout<<"volume of a0 is "<<a2.volume<<endl;return 0;volume of a0 is 1800volume of a0 is 5400volume of a0 is 83209.5对象指针9.5.1指向对象的指针9.5.2指向对象成员的指针1. 指向对象数据成员的指针2. 指向对象成员函数的指针例9
18、.7 有关对象指针的使用方法。#include <iostream>using namespace std;class Timepublic:Timeint,int,int;int hour;int minute;int sec;void get_time;Time:Timeint h,int m,int shour=h;minute=m;sec=s;void Time:get_timecout<<hour<<":"<<minute<<":"<<sec<<endl;int
19、 mainTime t110,13,56;int *p1=&t1.hour;/指向对象数据成员的指针cout<<*p1<<endl;t1.get_time;Time *p2=&t1;/定义指向Time类对象的指针p2->get_time;void Time:*p3;/定义指向Time类成员函数的指针p3=&Time:get_time;t1.*p3;/ p2->*p3;return 0;1010:13:5610:13:5610:13:569.5.3 this 指针在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的,称为thi
20、s,它是指向被调用对象的指针。a为Box类的一个对象。int Box:volumereturn height*width*length;a.volume;被C+把它处理为:int Box:volumeBox *thisreturn this->height*this->width*this->length;a. volume&a;这样,各个对象在成员函数中都使用自己的数据,互不干扰。9.6共用数据的保护9.6.1常对象定义对象时指定对象为常对象。常对象的所有数据成员的值不能改,所以常对象必须有初值。 Time const t112,34,46;等价写法:const T
21、ime t112,34,46;9.6.2常对象成员1. 常数据成员在类体中定义了常数据成员hour:const int hour;那么只能通过构造函数的参数初始化列表对常数据成员进展初始化。Time:Timeint hhour=h;/非法Time:Timeint h:hourh/合法2. 常成员函数int get_hourconstreturn hour;常成员函数,只能引用本类中的数据成员,而不能修改数据成员。#include <iostream>using namespace std;class Timepublic:Timeint,int,int;const int hour
22、;int minute;int sec;int get_hourconstreturn hour;void testcout<<hour;Time:Timeint h,int m,int s:hourhminute=m;sec=s;void funTime &tt.minute=18;int mainTime t110,13,56;Time const t212,34,46;funt1;cout<<t2.get_hour<<endl;t1.test;t2.test;/错误return 0;实验题:利用以上程序,进展上表的各项实验。9.6.3指向对象的
23、常指针指针变量声明为const型,这样指针值始终保持其初值,不能改变。Time t110,12,15,t2;Time * const ptr1;/这样可以吗?上机实验ptr1=&t1;Time * const ptr1=&t1;/必须这样,ptr1永远指向t1。ptr1=&t2;/错误9.6.4指向常对象的指针变量1指向常变量的指针变量不确切const 类型名 * 指针变量名;int i=10;const int k=99;const int * pi;/不能通过*pi来改变pi所指地址的内容pi=&i;/*pi=88; /error C2166: l-valu
24、e specifies const objectpi=&k;/说明pi可以指向常变量,也可以指向一般变量2指向常对象的指针变量:不确切const Time * pt;Time t17,8,9;const Time t23,4,5;pt=&t1;pt->minute=88;/不能通过pt修改所指对象的数据成员 /error C2166: l-value specifies const objectpt=&t2;t1.minute=88;/合法3指向常对象的指针变量应用于函数参数好处是省空间。void mainvoid funconst Time *p;Time t1
25、10,13,56;fun&t1;void funconst Time *pp->minute=19;/错误cout<<p->minute<<endl;9.6.5对象的常引用例9.8 对象的常引用#include <iostream>using namespace std;class Timepublic:Timeint,int,int;int hour;int minute;int sec;Time:Timeint h,int m,int shour=h;minute=m;sec=s;void funconst Time &t /
26、形参t是实参的引用,形参tt.hour=18;/不占空间。const保证不修改实参数据成员/error C2166: l-value specifies const objectint mainTime t110,13,56;funt1;cout<<t1.hour<<endl;return 0;9.6.6 const型数据的小结9.7对象的动态建立和释放Box *pt=new Box;delete pt;pt=new Box12,15,18;delete pt;9.8对象的赋值和复制9.8.1对象的赋值例9.9 对象的赋值。#include <iostream&g
27、t;using namespace std;class Boxpublic:Boxint=10,int=10,int=10;Boxconst Box& b/复制构造函数height=b.height;/也称拷贝初始化构造函数width=b.width;length=b.length;int volume;private:int height;int width;int length;Box:Boxint h,int w,int lenheight=h;width=w;length=len;int Box:volumereturnheight*width*length;int mainB
28、ox box115,30,25,box2; cout<<"box1:"<<box1.volume<<endl;box2=box1; cout<<"box2:"<<box2.volume<<endl;Box box3box1;cout<<"box3:"<<box2.volume<<endl;return 0;box1:11250box2:11250box3:11250特别声明:类的数据成员不能包含动态分配数据,否那么在赋值时会产生
29、严重后果。9.8.2对象的复制复制构造函数用在用一个已有对象去建立一个新对象。(1) 一般用处见上例。(2) 当函数的参数为类的对象时。void funBox b void mainBox box112,15,18;funbox1;/此时调用复制构造函数生成形参b(3) 当函数的返回值是类的对象时。Box fBox box112,15,18;return box1;void mainBox box2;box2=f;调用函数f,在f的return语句中,由 /box1调用复制构造函数生成临时对象赋值给box2 4用户没有编制自己的复制构造函数,那么系统自动生成一个复制构造函数。在没使用动态数据成
30、员,用户没有必要编制自己的复制构造函数。9.9静态成员9.9.1静态数据成员静态数据成员是以关键字static开头,为该类所有对象所共享,在类的作用域范围内,类体外使用int Box:height=10;语句,在内存中申请一份空间。例9.10 引用静态数据成员#include <iostream>using namespace std;class Boxpublic:Boxint,int;int volume;static int height;int width;int length;Box:Boxint w,int len width=w; length=len; int Bo
31、x:volumereturnheight*width*length; int Box:height=10;void mainBox a15,20,b20,30;cout<<a.height<<endl;Box:height=5;cout<<b.height<<endl;b.height=10;cout<<Box:height<<endl;cout<<a.volume<<endl;1051030009.9.2静态成员函数静态成员函数没有this指针,所以静态成员函数主要用来访问静态数据成员。#incl
32、ude <iostream>using namespace std;class Studentpublic:Studentint,int,int;void total;static float average;private:int num;int age;float score;static float sum;static int count;Student:Studentint m,int a,int snum=m; age=a; score=s;void Student:totalsum+=score;count+;float Student:average returns
33、um/count; float Student:sum=0;int Student:count=0;int mainStudent stud3=Student1001,18,70,Student1002,19,79,Student1005,20,98; cout<<"please input the number of students:"int n;cin>>n;forint i=0;i<n;i+studi.total;cout<<"The average score of "<<n<<
34、" students is "<<stud0.average<<endl; return 0;please input the number of students:3The average score of 3 students is 82.33339.10友元9.10.1友元函数1. 将普通函数声明为友元函数例9.12 友元函数的简单例子。#include <iostream>using namespace std;class Timepublic:Timeint,int,int;friend void displayTime &am
35、p;private:int hour;int minute;int sec;Time:Timeint h,int m,int shour=h;minute=m;sec=s;void displayTime &tcout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;void mainTime t110,13,56;displayt1;10:13:562. 友元成员函数例9.13 友元成员函数的简单例子。#include <iostre
36、am>using namespace std;class Date;class Timepublic:Timeint,int,int;void displayconst Date&private:int hour,minute,sec;class Datepublic:Dateint,int,int;friend void Time:displayconst Date &private:int month,day,year;Time:Timeint h,int m,int shour=h;minute=m;sec=s;void Time:displayconst Date
37、 &dacout<<da.month<<"/"<<da.day<<"/"<<da.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;Date:Dateint m,int d,int ymonth=m;day=d; year=y;void mainTime t110,13,56;Date d112,25,2004;t
38、1.displayd1;12/25/200410:13:563. 一个函数包括普通函数和成员函数可以被多个类声明为“朋友,这样它就可以引用多个类中的私有数据。9.10.2友元类可以将一个类说明为另一个类的友元。例如:#include<iostream.h>class oneprivate:int x;friend class two;public:oneint kx=k;class twopublic:void funone& o;void two : funone& o cout<<o.x<<endl;void maintwo t;one
39、o88;t.funo;这样,类two的所有成员函数都是类one的友元。1友元关系是单向的而不是双向的。#include<iostream.h>class oneprivate:int x;friend class two;public:oneint kx=k;void fun_onetwo& t;class twoint y;/friend class one;public: void funone& o;twoint ky=k;void two:funone& ocout<<o.x<<endl;void one:fun_onetwo
40、& tcout<<t.y<<endl;/error C2248: 'y' : cannot access private member/ declared in class 'two'void maintwo t99;one o88;t.funo;o.fun_onet;2友元关系不能传递,假设B类是A类的友元,C类是B类的友员,不等于C类是A类的友元。#include<iostream.h>class zeroprivate:int z;friend class one;public:zeroint kz=k;class oneprivate:int x;friend class two;public:oneint kx=k;void fun_onezero& ze;void one:fun_oneze
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 三农信息人才培养计划及实施方案
- 多领域融合的智能科技项目开发协议
- 工程项目居间的合同
- 化工厂员工劳动协议
- 经营承包合同
- 劳务输出合作协议书
- 第2课 奖品数量好计算(教学设计)2024-2025学年五年级上册信息技术泰山版
- 新媒体主播合约协议书8篇
- Unit 8 Knowing the world Lesson 2 My home country 教学设计 2024-2025学年冀教版英语七年级上册
- 甘肃幼儿园塑胶施工方案
- 胃癌影像诊断课件
- 建筑工程劳务作业服务方案
- 探究水垢的主要成份
- (完整版)小学生心理健康教育课件
- 军队文职专用简历(2023年)
- 建筑装饰工程施工总平面布置图
- 铁路基本建设工程设计概(预)算编制办法-国铁科法(2017)30号
- 颜真卿《劝学》ppt课件1
- 特种设备安全技术档案(附表格)
- (完整版)中国古代书法史课件
- 人教版英语八年级上册单词默写表
评论
0/150
提交评论