c++知识学习 (2)

析构函数

析构函数是在对象所占内存释放时调用的,通常用来释放相关的资源。

析构函数就是一个特殊的类成员函数,与构造函数相反。

析构函数的名字是在类名前面加上波浪线~。

它不返回任何值也没有任何参数,不能被重载。

类如:

~Person()

Person.h

 1 //
 2 //  Person.h
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-8.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 //防止重复引用
10 #ifndef __ArrayTest__Person__
11 #define __ArrayTest__Person__
12 
13 #include <iostream>
14 using namespace std;
15 class Person{
16 //---------成员变量--------
17 public :
18     
19     int age;
20     
21 private :
22     int weight;
23     char  * name;
24     char sex;
25 //---------成员方法--------
26 public:
27     //----构造函数-------
28     Person();
29     //-----构造函数重载-----
30     Person(int age);
31     
32     void setWeight(int weight);
33     int getWeight() const;
34     //char * getName() const;
35     //const 指针,保证指针不被修改
36     const char * getName() const;
37   
38     void info() const;
39     ~Person();
40 };
41 #endif /* defined(__ArrayTest__Person__) */

Person.cpp

 1 //
 2 //  Person.cpp
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-8.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 #include "Person.h"
10 //------方法实现格式------
11 //----返回值 类::方法名 (参数)------
12 void Person::setWeight(int weight){
13     //-----this 代表
14     this->weight=weight;
15 }
16 //--------const 编译限制-------
17 int  Person::getWeight() const{
18     //weight++;报错
19     return weight;
20 }
21 const char * Person::getName() const{
22     
23     return name;
24     
25 }
26 
27 void Person::info() const{
28 
29     printf("%s\n%d\n%c\n%d\n",name,age,sex,weight);
30 }
31 //--------构造函数:初始化成员变量------------
32 Person::Person(){
33     printf("call the functon Person()\n");
34     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
35     //strcpy(name, "a man");
36     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
37     name = new char[255];
38     strcpy(name, "a man");
39     weight=60;
40     age=20;
41     sex='m';
42 }
43 //--------构造函数:重载------------
44 Person::Person(int age){
45     printf("call the functon Person(int age)\n");
46     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
47     //strcpy(name, "a man");
48     name = new char[255];
49     strcpy(name, "a man");
50     weight=60;
51     this->age=age;
52     sex='m';
53     
54 }
55 
56 //-----------析构函数---------
57 Person::~Person(){
58     //在析构函数里面释放内存
59     delete [] name;
60     printf("call the functon ~Person()析构函数 \n");
61 }

 

main.cpp

 1 //
 2 //  main.cpp
 3 //  ArrayTest
 4 //
 5 //  Created by 张学院 on 14-1-6.
 6 //  Copyright (c) 2014年 com.mix. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 
11 #include <string>
12 #include "Person.h";
13 int main()
14 {
15     //---栈分配:函数结束,per出栈,内存释放之前,调用析构函数-------
16     Person per= Person();
17     per.info();
18    
19     //-----堆分配:必须手动是否内存-------
20     
21     Person *per1= new Person();
22     per1->info();
23     
24     //不调用delete  per1;不会调用析构函数
25     delete  per1;
26     return 0;
27 }

输出:

call the functon Person()

a man

20

m

60

call the functon Person()

a man

20

m

60

call the functon ~Person()析构函数 

call the functon ~Person()析构函数 

 

一定要记得在释放堆中分配的内存。

posted @ 2014-01-08 18:27  aosting  阅读(215)  评论(0编辑  收藏  举报