1 / 27

C++ 面向对象程序设计

C++ 面向对象程序设计. 第 十 讲. 构造函数 -- 特殊公用成员函数. 作用 : 创造类对象时自动调用构造函数给类对象赋初值 特征 : 1 )函数名:与类名相同 2 )无返回类型 无返回值 3 )公用成员 4 )用户可以自己定义,但不能调用 默认构造函数 : 如果程序中未定义出,则系统自动产生出一个缺省形式的构造函数. 构造函数举例. class watch { public: watch(int initH,int initM,int initS); // 构造函数

edena
Télécharger la présentation

C++ 面向对象程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++面向对象程序设计 第 十 讲

  2. 构造函数-- 特殊公用成员函数 作用: 创造类对象时自动调用构造函数给类对象赋初值 特征: 1)函数名:与类名相同 2)无返回类型 无返回值 3)公用成员 4)用户可以自己定义,但不能调用 默认构造函数: 如果程序中未定义出,则系统自动产生出一个缺省形式的构造函数

  3. 构造函数举例 class watch { public: watch(int initH,int initM,int initS); //构造函数 void SetTime(int NewH,int NewM, int NewS); void ShowTime(); private: int Hour, Minute, Second; };

  4. // 实现构造函数 watch::watch(int initH,int initM,int initS) { Hour = initH; Minute= initM; Second = initS; }

  5. // 主函数 void main(void) { watch myWatch(05,06,07); //定义对象 watch watch01(03,04,05); myWatch.SetTime(); //按默认值设置 myWatch.ShowTime(); cout<<endl; myWatch.SetTime(8,30,30); myWatch.ShowTime(); cout<<endl; watch01.ShowTime(); cout<<endl; }

  6. 构造函数主要是两个功能:为对象开辟空间;为对象中的数据成员赋初值。构造函数主要是两个功能:为对象开辟空间;为对象中的数据成员赋初值。 • 对上例中构造函数的默认构造函数是: watch::watch() { } 只能用来设置对象的存储空间,不能赋初值。

  7. 含有缺省参数的构造函数: class watch { public: watch(int initH=0,int initM=0,int initS=0); //含有缺省参数构造函数 void SetTime(int NewH, int NewM, int NewS); void ShowTime(); private: int Hour, Minute, Second; } ;

  8. 含有缺省参数的构造函数: void main(void) { watch myWatch; //按缺省值定义对象 watch watch01(8,7); //只传递两个参数 watch watch01(03,04,05); } 注:缺省值不能在构造函数实现时规定。

  9. 拷贝构造函数 拷贝构造函数是一种特殊的构造函数,其形参为本类的对象引用 class 类名 { public : 类名(形参);//构造函数 类名(类名 &对象名);//拷贝构造函数 ... }; 类名:: 类名(类名 &对象名)//拷贝构造函数的实现 { 函数体 }

  10. 拷贝构造函数举例 class Location { public: Location(int xx=0,int yy=0){X=xx; Y=yy;} Location(Location & p); int GetX() {return X;} int GetY() {return Y;} private: int X,Y; };

  11. Location::Location (Location &p) { X=2*p.X; Y=2*p.Y; cout<<"拷贝构造函数被调用"<<endl; }

  12. 当用类的一个对象去初始化该类的另一个对象时系统自动调用它实现拷贝赋值。 例: int main(void) { Location C; //按默认值定义 Location A(1,2); Location B(A); //拷贝构造函数被调用 cout<<B.GetX()<<““<< B.GetY() <<endl; return 0; }

  13. 若函数的形参为类对象,调用函数时,实参赋值给形参,系统自动调用拷贝构造函数。若函数的形参为类对象,调用函数时,实参赋值给形参,系统自动调用拷贝构造函数。 例如: void f(Location p) { cout<<p.GetX()<<endl; } int main() { Location A(3,4); f(A); //调用拷贝构造函数 return 0; }

  14. 执行时,调用拷贝构造函数,将对象A拷贝到形参p,然后再执行函数f。执行时,调用拷贝构造函数,将对象A拷贝到形参p,然后再执行函数f。 执行结果: 拷贝构造函数被调用 6

  15. 当函数的返回值是类对象时,系统自动调用拷贝构造函数。当函数的返回值是类对象时,系统自动调用拷贝构造函数。 例如: Location g() { Location A(1,2); return A; //调用拷贝构造函数 } int main() { Location B; B=g(); return 0; }

  16. 如果程序员没有为类声明拷贝初始化构造函数,则编译器自己生成一个拷贝构造函数。如果程序员没有为类声明拷贝初始化构造函数,则编译器自己生成一个拷贝构造函数。 类名(类名 &对象名) • { • };//默认拷贝构造函数

  17. 构造初始化表 • 构造函数也可使用构造初始化表对对数据成员进行初始化 • 如: Circle::Circle(float r) {radius=r;} • 可改写为: Circle::Circle(floatr):radius(r){}

  18. 构造初始化表 • 相当于: class A { int i; float f; public: A(int X,float Y) :i(X),f(Y){}; }; • 又如: class A { int i; float f; public: A(int X,float Y) {i=X; f=Y;}; };

  19. 析 构 函 数 • 完成对象被删除前的一些清理工作。 • 在对象的生存期结束的时刻系统自动调用它,然后再释放此对象所属的空间。 • 如果程序中未定义析构函数,编译器将自动产生一个缺省的析构函数。 • 特征: 1)函数名:与类名相同 2)无返回类型 无返回值 无参数表 3)公用成员 4)用户可以自己定义,但不能调用,不能重载 • 格式:~类名(){ };

  20. 构造函数和析构函数举例 #include "iostream.h" class box { public: box(int l , int w , int h) ; ~box( ) {cout<<"析构函数被调用!"<<endl;} double volume(); double surface_area(); private: int length,width,height; } ; box::box(int l , int w , int h) { length=l; width=w; height=h; } double box::volume( ) { return length*width*height; } double box::surface_area( ) { return 2*length*width+4*length*height; }

  21. void main( ) { box ob(8,6,3); cout<<“盒子的体积是:“ <<ob.volume()<<endl; cout<<"盒子的表面积是:“ <<ob.surface_area()<<endl; }

  22. 例2、 #include "iostream.h" #include "string.h" class string { public: string(char s[ ]) { strcpy(str,s); cout<<"构造函数被调用! “<<endl; } ~string( ) {cout<<"析构函数被调用!"<<endl;}; void print( ) { cout<<str<<endl; } operator const char *( ); private: char str[20]; }; string::operator const char *( ) { return str; }

  23. void main( ) { char *s1,*s2; cout<<"Please input two string:"<<endl; s1=new char[20]; s2=new char[20]; cin>>s1>>s2; string o1(s1),o2(s2); o1.print( ); o2.print( ); if(strcmp(o1,o2)==0) cout<<o1<<" is equal to "<<o2<<endl; else cout<<o1<<" is not equal to "<<o2<<endl; }

  24. 第12章 静态成员、友元 • 静态成员 一个类的静态数据成员是用来表示类的属性的成员,而不是对象的属性的成员。 • 定义格式:在要定义的静态成员前加static 静态数据成员定义 例:class file { //…… public: static int filecount ; //…… };

  25. 静态函数成员定义 例:class file { //…… public: static int getfilecount() {return filecount;} //…… };

  26. 静态数据成员的初始化 在所有的函数定义体外进行 int file:filecount=0;

  27. 类的静态成员访问: void main() { file file1; cout<<file::filecount ; //…… }

More Related