版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第6章 类与对象 P216,类(Class)是面向对象程序设计(OOP)实现信息封装的基础。 类是用户定义类型,也称为类类型。 每个类包含数据说明和一组操作数据或传递消息的函数。 类的实例称为对象。,6.1 类和对象的定义与访问 P216,在面向对象程序设计中,类是程序基本单位。 类是数据和操作数据的函数的封装。 类的对象使用自己的方法完成对数据的操作。 类可以隐藏数据和操作细节,对象通过类接口与外部通信。,例1-2 用面向对象方法编程,求圆的周长和面积 P4,圆类 数据成员 半径 成员函数 置半径值 求圆的半径 求周长 求面积,实例化,/ 例 1-2 输入圆的半径,求圆的周长和面积 (用面向
2、对象方法编程) P4 #include using namespace std ; class Circle private: double radius ; public : void Set_Radius( double r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * 3.14f * radius ; double Get_Area() return 3.14f * radius * radius ; ; int main() Circle A, B ; A.Set_Rad
3、ius( 6.23 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 10.5 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; return(0); ,例1-2 用面向对象方法编程,求圆的周长和面积,#include usi
4、ng namespace std ; class Circle double radius ; public : void Set_Radius( double r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * 3.14 * radius ; double Get_Area() return 3.14 * radius * radius ; ; int main() Circle A, B ; A.Set_Radius( 6.23 ) ; cout A.Radius = A.Ge
5、t_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 10.5 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; ,数据成员,例1-2 用面向对象方法编程,求圆的周长和面积,#include using namespace std ; class Circle double r
6、adius ; public : void Set_Radius( double r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * 3.14 * radius ; double Get_Area() return 3.14 * radius * radius ; ; int main() Circle A, B ; A.Set_Radius( 6.23 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_G
7、irth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 10.5 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; ,成员函数,例1-2 用面向对象方法编程,求圆的周长和面积,#include using namespace std ; class Circle double radius ; public : void Set_Radius( double
8、 r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * 3.14 * radius ; double Get_Area() return 3.14 * radius * radius ; ; int main() Circle A, B ; A.Set_Radius( 6.23 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area()
9、 endl ; B.Set_Radius( 10.5 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; ,建立对象 (类类型变量),例1-2 用面向对象方法编程,求圆的周长和面积,#include using namespace std ; class Circle double radius ; public : void Set_Radius( double r ) radius = r ; double Get_Rad
10、ius() return radius ; double Get_Girth() return 2 * 3.14 * radius ; double Get_Area() return 3.14 * radius * radius ; ; int main() Circle A, B ; A.Set_Radius( 6.23 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 10.5 ) ; c
11、out B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; ,通过对象 调用类的成员函数,数组类型定义与排序操作 P217,/ 排序函数原型 void Sort (int , int ) ; / 数组相加函数原型 void Add ( int , int , int ) ; / int main() int a 10 , b 10 ; /. Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10
12、) ; /. ,“数组类”类型 P217,设计一个“数组类”类型,把数组类型定义和对数组排序操作封装在一起。 这样“排序”就成了所有数组类对象自己做的操作。,class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ; / 排序 / 重载算符 +函数 Array operaor + (const Array / 数组相加 . ,/ 排序函数原型 void Sort (int , int ) ; / 数组相加函数原型 voi
13、d Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; . ,数组与数组类,封装,6.1 类与对象,class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array /
14、 数组相加 . ,/ 排序函数原型 void Sort (int , int ) ; / 数组相加函数原型 void Add ( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; . ,数组与数组类,class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/
15、 排序 / 重载算符 +函数 Array operaor + (const Array ,类是数据和 操作数据的函数的封装,封装,6.1 类与对象,class Array/定义数组类 int *ap ; int len ; public: Array( int size )/ 建立数组 len= size ; ap = new int size ; void Sort ( ) ;/ 排序 / 重载算符 +函数 Array operaor + (const Array / 数组相加 . ,/ 排序函数原型 void Sort (int , int ) ; / 数组相加函数原型 void Add
16、( int , int , int ) ; int main() int a 10 , b 10 ; . Sort ( a , 10 ) ; Sort ( b , 10 ) ; Add ( a , b , 10) ; . ,数组与数组类,对象使用自己的方法 对数据操作,a . Sort() ; b . Sort() ; / 调用排序方法 a = a + b ; / 数组相加,封装,6.1 类与对象,6.1.1 定义类与对象 P218,C+中,类定义的说明语句一般形式为: class 类名 public: 公有性质数据成员和成员函数 ; protected: 保护性质数据成员和成员函数 ; pr
17、ivate: 私有性质数据成员和成员函数 ; ;,说 明 P218,其中,class是定义类的关键字。 “类名”是用户自定义的标识符,用于标识类型名字。通常第一个字母大写。 一对花括号相括说明类成员,以分号结束类定义语句。,*成员的性质,类成员用关键字指定不同访问特性,决定其在类体系中或类外的可见性。 private声明私有成员:私有性质成员仅在类中可见。 不能在类外或派生类中使用。如果私有类成员放在放在第一段,则可以省略关键字private。(建议不省) protected声明保护成员:保护性质成员在本类和它的派生类中可见。 public声明公有成员:公有性质的成员是的接口,在类中和类外可见
18、。 各段中既可以包含数据成员,也可以包含成员函数。,成员函数可在类外定义 P219,成员函数在类外定义,使用作用域区分符进行说明,此时函数头形式为: 返回类型 类名:函数名(参数表) 简单的成员函数实现可以在类中定义,此时编译器作内联函数处理。,关键字 struct 也可定义类 P218,除了关键字class 外,关键字 struct 也可定义类。 用关键字 struct 也可定义类时,若不特别指出,则所有成员都是公有成员。,关于类声明的进一步说明1.允许已定义类名出现在类的说明中,允许已定义类名出现在类的说明中 例: class X ; class Y X dataMember ; ;,允许
19、已定义类名出现在类的说明中,例: class link link * next ; / 声明一个指向link类类型的指针 ;,2. 类可以无名,用于直接声明对象,例: class mydate ;,3. 类是一个程序包,可以只有数据成员或只有成员函数,或者为空 #include using namespace std ; class empty ; int main() empty e1 ; cout ,强调,注: 1. 允许已定义类名出现在类的说明中,2. 类可以无名,用于直接声明对象,/例 #include using namespace std ; class empty ; int m
20、ain() empty e1 ; cout ,3. 类是一个程序包。可以只有数据成员或只有成员函数,或者为空。,“类”类型变量的定义 P219,“类”类型变量的定义,定义方法与普通变量相同。 说明一个类类型的对象(变量)后,编译器为每个对象的数据成员分配内存。 对象没有成员函数的副本,类成员函数可以被对象调用。,6.1.2 访问对象成员 P220,公有成员是提供给外部的接口。 类外用 “ . ” 和 “ - ” 运算符访问对象成员。,访问对象的公有成员 P220,/例6-1 访问对象的公有成员 P220 #include using namespace std ; class Tclass p
21、ublic: int x, y ; void print( ) cout x “,” y ; ; ; int main( ) Tclass test ; test.x = 100 ; test.y = 200 ; test.print() ; ,/例6-2 用指针访问对象成员 #include using namespace std ; class Tclass public : int x, y ; void print() cout x + ptf-y ) ; int main() Tclass test, * pt = new(Tclass) ; pt-x = 100 ; pt-y =
22、200 ; pt-print() ; test.x = 150 ; test.y = 450 ; test.print() ; cout x+y= add( ,6.1.3 this指针 P221,C+中,同一类的各个对象都有自己的数据成员的存储空间,系统不为每个类的对象建立成员函数副本,但类的成员函数可以被各个对象调用。 C+为成员函数提供了一个称为this的隐含指针参数,所以常常称成员函数拥有 this 指针。 当一个对象调用类的成员函数时,对象的地址被传递给this指针,即this指针指向了该对象。 this 指针不能显式声明,但可以显式使用。,说明,this 指针是一个常指针,相当于:
23、class_Type *const this; 其中: class_Type是用户定义的类类型标识符。 这里,this指针一旦初始化(成员函数被调用)之后,获取了对象地址,指针值就不能再修改和赋值,以保证不会指向其它对象。 this 指针的显式使用主要在运算符重载、自引用等场合。,this指针,#include using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main
24、() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,6.1.3 this指针,#include using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY
25、() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int
26、 a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,向哪个对象 的数据成员赋值?,6.1.3 this指针,#include /例6-5 using namespace s
27、td ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,void s
28、etXY ( int a, int b, Simple * const this ) this-x = a ; this-y = b ; ,obj1 . setXY ( 10, 15, ,成员函数隐含定义 this 指针 接受调用对象的地址,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simpl
29、e obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,obj1 . setXY ( 10, 15, ,void setXY ( int a, int b, Simple * const this ) this-x = a ; this-y = b ; ,this 指针不能显式声明,6.1.3 this指针,#include /例6-
30、5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . p
31、rintXY () ; ,void setXY ( int a, int b, Simple * const this ) this-x = a ; this-y = b ; ,this 指针不能显式声明 可以显式使用,obj1 . setXY ( 10, 15, ,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int
32、 main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,obj1.x,obj1.y,通过调用函数的对象 this 指针获取对象地址,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void s
33、etXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,6.1.3 this指针,#include /例6-5 us
34、ing namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . print
35、XY () ; ,10,15,obj1.x,obj1.y,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ;
36、 obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,在 obj1 上操作,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Sim
37、ple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY
38、( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,10 , 15 20 , 25 30 , 35,在 obj2 上操作,
39、6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . s
40、etXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,10 , 15 20 , 25 30 , 35,在 obj2 上操作,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1,
41、 obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public :
42、void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,10 ,
43、 15 20 , 25 30 , 35,在 obj2 上操作,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 )
44、 ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,10 , 15 20 , 25 30 , 35,在 obj2 上操作,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y end
45、l ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; cl
46、ass Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,
47、obj1.y,20,25,obj2.x,obj2.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj
48、2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,30,35,obj3.x,obj3.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ;
49、void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,30,35,obj3.x,obj3.y,10 , 15 20 , 25 30 , 35,6.1
50、.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setX
51、Y ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,30,35,obj3.x,obj3.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include /例6-5 using namespace std ; class Simple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple
52、 obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.y,20,25,obj2.x,obj2.y,30,35,obj3.x,obj3.y,10 , 15 20 , 25 30 , 35,6.1.3 this指针,#include using namespace std ; /例6-5 class S
53、imple int x, y ; public : void setXY ( int a, int b) x = a ; y = b ; void printXY() cout x , y endl ; ; ; int main() Simple obj1, obj2, obj3 ; obj1 . setXY ( 10, 15 ) ; obj1 . printXY () ; obj2 . setXY ( 20, 25 ) ; obj2 . printXY () ; obj3 . setXY ( 30, 35 ) ; obj3 . printXY () ; ,10,15,obj1.x,obj1.
54、y,20,25,obj2.x,obj2.y,30,35,obj3.x,obj3.y,10 , 15 20 , 25 30 , 35,成员函数拥有 this 指针,6.2 构造函数和析构函数 P222基本概念,在类中构造函数和析构函数都是特殊函数。 构造函数用于建立类的实例; 析构函数用于撤消类的实例。 程序并不显式调用这些函数,当程序创建一个对象时,系统自动调用构造函数;当程序撤消一个对象时,系统自动调用析构函数。 这些都是编译器自动调用的。,构造函数 P222,构造函数是用于创建对象的特殊成员函数。 当创建对象时,系统自动调用构造函数。 构造函数的作用是: 为对象分配空间; 对数据成员赋初值
55、; 请求其他资源。,说明,构造函数名与类名相同:类名 构造函数可以有任意类型的参数,但没有返回类型。 构造函数可以由用户提供,也可使用系统的默认版本。 如果没有用户定义的构造函数时,系统提供缺省版本的构造函数,只负责对象的创建,不能为对象中的数据成员赋初值。(不做数据成员初始化工作) 构造函数可以重载,何时调用构造函数,何时调用构造函数十分明确: 定义类的对象时; 调用new或者new 运算符动态开辟内存空间时。 在对象被创建的时候就会调用构造函数, 比如: 声明一个类class A., 当你在main函数中 A a ; 定义一个对象a的时候, 就调用构造函数。 默认构造函数都是无参数的。,何
56、时调用析构函数,何时调用构造函数要十分明确: 当函数调用结束的时候,会自动调用析构函数,析构的作用就是释放对象占用的资源。 以为默认析构什么也不打印,所以你不会直观看出析构函数什么时候执行,自己在析构中加上打印的语言你就会看到了。,例: class AA public: AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; ,构造函数和析构函数,例: class AA public: AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; ,构造函数,构造函数和析构函数,例: class AA public: AA() AA() ; void test() int x , y ; AA t1, t2 ; return ; ,析构
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 墨西哥智能家居行业市场现状供需分析及投资评估规划分析研究报告
- 2026三上数学第一单元课件
- 吸痰护理技术要点分析
- 精神科护理中的移动医疗应用
- 吞咽障碍患者的吞咽安全监测与评估
- 2025届甘肃省陇南地区两当县数学三年级第二学期期中达标检测试题含答案解析
- 精神障碍患者的护理未来趋势
- 危重患者安全管理措施
- 糖尿病患者的旅游护理评估
- 2025-2026学年学生活动教学设计数学
- 2026年国企中层干部竞聘笔考试题与答案
- 2026年演出经纪人考试题库及答案(真题)
- 2026中国氢能储运装备安全标准与国际对标报告
- 乡镇(街道功能区)党政领导干部离任经济事项交接表(开发区和园区适用本表-修订)
- 2025贵州六盘水市盘州市教育局机关所属事业单位考调19人备考题库必考题
- TSG D0001-2009:压力管道安全技术监察规程-工业管道
- 钠离子电池热失控机理及其防火阻燃策略研究
- 2025年希望杯夏令营竞赛四年级下学期数学S卷
- 2025年部编版新教材语文小学二年级上册全册单元检测题带答案(共8单元)
- 测绘单位安全生产培训
- (高清版)DG∕TJ 08-15-2020 绿地设计标准 附条文说明
评论
0/150
提交评论