c++课件第九章关于类和对象的进一步讨论.doc_第1页
c++课件第九章关于类和对象的进一步讨论.doc_第2页
c++课件第九章关于类和对象的进一步讨论.doc_第3页
c++课件第九章关于类和对象的进一步讨论.doc_第4页
c++课件第九章关于类和对象的进一步讨论.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第9章 关于类和对象的进一步讨论9.1构造函数9.1.1对象的初始化在建立一个对象时,常常要做一些初始化工作,例如数据成员赋初值等。类的数据成员是不能在声明类时初始化的。#include using namespace std;class Timeint hour;/int hour=0;int minute;int sec;void main()Time t1=14,56,30;error C2552: t1 : non-aggregates cannot be initialized with initializer list上面这种写法是错误的。如果数据成员是公有的,则可以用初始化列表形式初始化。#include using namespace std;class Timepublic:int hour;int minute;int sec;void main()Time t1=14,56,30;9.1.2构造函数的作用C+提供构造函数来处理对象的初始化,构造函数是一种特殊的成员函数,不需要用户来调用它,而是建立对象时自动执行。构造函数与类同名,并且没有返回类型。构造函数可以根据需要进行重载。例9.1在例8.3基础上定义构造成员函数。#include using namespace 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_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;int main()Time 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 有两个长方体,其长、宽、高分别为(1)12,20,25(2)15,30,21。求它们的体积。编一个基于对象的程序,在类中用带参数的构造函数。#include using namespace std;class Boxpublic:Box(int,int,int);int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return(height*width*length); int main()Box box1(12,25,30);coutThe volume of box1 is box1.volume()endl;Box box2(15,30,21);coutThe volume of box2 is box2.volume()endl;return 0;The volume of box1 is 9000The volume of box2 is 94509.1.4用参数初始化表对数据成员初始化#include using namespace std;class Boxpublic:Box(int h,int w ,int len): height(h),width(w),length(len)int volume();private:int height;int width;int length;int Box:volume()return(height*width*length); int main()Box box1(12,25,30);coutThe volume of box1 is box1.volume()endl;Box box2(15,30,21);coutThe volume of box2 is box2.volume()endl;return 0;9.1.5构造函数的重载在一个类中可以定义多个构造函数,以便对类对象提供不同的初始化方法。例9.3#include using namespace std;class Boxpublic:Box();Box(int h,int w ,int len): height(h),width(w),length(len)int volume();private:int height;int width;int length;Box:Box()height=10;width=10;length=10;int Box:volume()return(height*width*length); int main()Box box1;coutThe volume of box1 is box1.volume()endl;Box box2(15,30,21);coutThe 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 using namespace std;class Boxpublic:Box(int w=10,int h=10,int len=10);int volume();private:int height;int width;int length;Box:Box(int w,int h,int len)height=h;width=w;length=len;int Box:volume()return(height*width*length); int main()Box box1;coutbox1:box1.volume()endl;Box box2(15);coutbox2:box2.volume()endl;Box box3(15,30);coutbox3:box3.volume()endl;Box box4(15,30,20);coutbox4:box4.volume()endl;return 0;box1:1000box2:1500box3:4500box4:9000构造函数的二义性问题:(1) 定义下面两个构造函数Box();Box(int h=10,int w=10,int len=10);在执行 Box box1;时产生二义性。(2) 定义下面构造函数Box();Box(int h=10,int w=10,int len=10);Box(int,int);在执行 Box box1;Box box2(15,30);时产生二义性。(3) 定义下面两个构造函数Box();Box(int h,int w=10,int len=10);Box(int,int); 在执行 Box box1;/正确 Box box2(15);/调用第二个构造函数。 Box box3(15,30);/产生二义性。9.2析构函数析构函数的作用是在撤销对象占用的内存之前完成一些清理工作。它不返回任何值,没有函数类型,没有函数参数,不能被重载。例9.5 包含构造函数和析构函数的C+程序。#include #include using namespace std;class Studentpublic:Student(int n,string nam,char s)num=n;name=nam;sex=s;coutConstructor called.endl; Student()coutDestructor called.numendl;void display()coutnum:numendl;coutname:nameendl;coutsex:sexendlendl;private: int num; string name; char sex;int main()Student stud1(10010,Wang_li,f);stud1.display();Student stud2(10011,Zhang_fun,m);stud2.display();return 0;Constructor called.num:10010name: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 对象数组的使用方法。#include using namespace std;class Boxpublic:Box(int h=10,int w=12,int len=15) :height(h),width(w),length(len) int volume();private:int height;int width;int length;int Box:volume()return(height*width*length);int main()Boxa3=Box(10,12,15),Box(15,18,20),Box(16,20,26);coutvolume of a0 is a0.volume()endl;coutvolume of a0 is a1.volume()endl;coutvolume 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.7 有关对象指针的使用方法。#include using namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;void get_time();Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void Time:get_time()couthour:minute:secendl;int main()Time t1(10,13,56);int *p1=&t1.hour;/指向对象数据成员的指针cout*p1get_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 指针在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的,称为this,它是指向被调用对象的指针。a为Box类的一个对象。int Box:volume()return (height*width*length);a.volume();被C+把它处理为:int Box:volume(Box *this)return (this-height*this-width*this-length);a. volume(&a);这样,各个对象在成员函数中都使用自己的数据,互不干扰。9.6共用数据的保护9.6.1常对象定义对象时指定对象为常对象。常对象的所有数据成员的值不能改,所以常对象必须有初值。 Time const t1(12,34,46);等价写法:const Time t1(12,34,46);9.6.2常对象成员1. 常数据成员在类体中定义了常数据成员hour:const int hour;则只能通过构造函数的参数初始化列表对常数据成员进行初始化。Time:Time(int h)hour=h;/非法Time:Time(int h):hour(h)/合法2. 常成员函数int get_hour()constreturn hour;常成员函数,只能引用本类中的数据成员,而不能修改数据成员。#include using namespace std;class Timepublic:Time(int,int,int);const int hour;int minute;int sec;int get_hour()constreturn hour;void test()couthour;Time:Time(int h,int m,int s):hour(h)minute=m;sec=s;void fun(Time &t)t.minute=18;int main()Time t1(10,13,56);Time const t2(12,34,46);fun(t1);coutt2.get_hour()minute=88;/不能通过pt修改所指对象的数据成员 /error C2166: l-value specifies const objectpt=&t2;t1.minute=88;/合法(3)指向常对象的指针变量应用于函数参数(好处是省空间)。void main()void fun(const Time *p);Time t1(10,13,56);fun(&t1);void fun(const Time *p)p-minute=19;/错误coutminuteendl;9.6.5对象的常引用例9.8 对象的常引用#include using namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void fun(const Time &t) /形参t是实参的引用,形参tt.hour=18;/不占空间。const保证不修改实参数据成员/error C2166: l-value specifies const objectint main()Time t1(10,13,56);fun(t1);coutt1.hourendl;return 0;9.6.6 const型数据的小结9.7对象的动态建立和释放Box *pt=new Box;delete pt;pt=new Box(12,15,18);delete pt;9.8对象的赋值和复制9.8.1对象的赋值例9.9 对象的赋值。#include using namespace std;class Boxpublic:Box(int=10,int=10,int=10);Box(const Box& b)/复制构造函数height=b.height;/也称拷贝初始化构造函数width=b.width;length=b.length;int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return(height*width*length);int main()Box box1(15,30,25),box2; coutbox1:box1.volume()endl;box2=box1; coutbox2:box2.volume()endl;Box box3(box1);coutbox3:box2.volume()endl;return 0;box1:11250box2:11250box3:11250特别声明:类的数据成员不能包含动态分配数据,否则在赋值时会产生严重后果。9.8.2对象的复制复制构造函数用在用一个已有对象去建立一个新对象。(1) 一般用途见上例。(2) 当函数的参数为类的对象时。void fun(Box b) void main()Box box1(12,15,18);fun(box1);/此时调用复制构造函数生成形参b(3) 当函数的返回值是类的对象时。Box f()Box box1(12,15,18);return box1;void main()Box box2;box2=f();调用函数f,在f的return语句中,由 /box1调用复制构造函数生成临时对象赋值给box2 (4)用户没有编制自己的复制构造函数,则系统自动生成一个复制构造函数。在没使用动态数据成员,用户没有必要编制自己的复制构造函数。9.9静态成员9.9.1静态数据成员静态数据成员是以关键字static开头,为该类所有对象所共享,在类的作用域范围内,类体外使用int Box:height=10;语句,在内存中申请一份空间。例9.10 引用静态数据成员#include using namespace std;class Boxpublic:Box(int,int);int volume();static int height;int width;int length;Box:Box(int w,int len) width=w; length=len; int Box:volume()return(height*width*length); int Box:height=10;void main()Box a(15,20),b(20,30);couta.heightendl;Box:height=5;coutb.heightendl;b.height=10;coutBox:heightendl;couta.volume()endl;1051030009.9.2静态成员函数静态成员函数没有this指针,所以静态成员函数主要用来访问静态数据成员。#include using namespace std;class Studentpublic:Student(int,int,int);void total();static float average();private:int num;int age;float score;static float sum;static int count;Student:Student(int m,int a,int s)num=m; age=a; score=s;void Student:total()sum+=score;count+;float Student:average() return(sum/count); float Student:sum=0;int Student:count=0;int main()Student stud3=Student(1001,18,70),Student(1002,19,79),Student(1005,20,98); coutn;for(int i=0;in;i+)studi.total();coutThe average score of n 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 using namespace std;class Timepublic:Time(int,int,int);friend void display(Time &);private:int hour;int minute;int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void display(Time &t)coutt.hour:t.minute:t.secendl;void main()Time t1(10,13,56);display(t1);10:13:562. 友元成员函数例9.13 友元成员函数的简单例子。#include using namespace std;class Date;class Timepublic:Time(int,int,int);void display(const Date&);private:int hour,minute,sec;class Datepublic:Date(int,int,int);friend void Time:display(const Date &);private:int month,day,year;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void Time:display(const Date &da)coutda.month/da.day/da.yearendl;couthour:minute:secendl;Date:Date(int m,int d,int y)month=m;day=d; year=y;void main()Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);12/25/200410:13:563. 一个函数(包括普通函数和成员函数)可以被多个类声明为“朋友”,这样它就可以引用多个类中的私有数据。9.10.2友元类可以将一个类说明为另一个类的友元。例如:#includeclass oneprivate:int x;friend class two;public:one(int k)x=k;class twopublic:void fun(one& o);void two : fun(one& o) couto.xendl;void main()two t;one o(88);t.fun(o);这样,类two的所有成员函数都是类one的友元。(1)友元关系是单向的而不是双向的。#includeclass oneprivate:int x;friend class two;public:one(int k)x=k;void fun_one(two& t);class twoint y;/friend class one;public: void fun(one& o);two(int k)y=k;void two:fun(one& o)couto.xendl;void one:fun_one(two& t)coutt.yendl;/error C2248: y : cannot access private member/ declared in class twovoid main()two t(99);one o(88);t.fun(o);o.fun_one(t);(2)友元关系不能传递,如果B类是A类的友元,C类是B类的友员,不等于C类是A类的友元。#includeclass zeroprivate:int z;friend class one;public:zer

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论