• ~~~~~~~~~~~析构函数~~~~~~~~


     1 // 析构函数 也称 destructor装饰器(Python) 是一个特殊的成员函数 , 它的作用和构造函数相反 , 她的名字是类名
     2 //前面加一个 '~' 在 C++中这是一个  位取反运算符 , 从这点也可以想到: 析构函数是和构造函数作用相反的函数
     3 // 当对象的生命期结束的时候,会自动执行析构函数.具体地说如果出现一下四种情况,程序就会自动执行析构函数 .
     4 //析构函数的作用并不是删除对象,而是在撤销对象占用的内存前完成一些清理工作,使这部分的内存可以重新分配给新对象使用.
     5 // 程序设计者 可以实现设计好析构函数,已完成所需的功能只要对象的生命期结束,程序就自动执行析构函数来完成这些工作 .
     6 //析构函数不返回任何值,没有函数类型没有函数参数,一个类可以有多个和构造函数,但是只能有一个析构函数 .
     7 // 最重要的是,析构函数的周勇并不仅仅限于释放资源方面的,它还可以用来执行 " 用户希望在最后一次使用对象之后 所执行的的任何操作 "
     8 //一般情况下,类的设计者应当在声明类的同时定义析构函数 , 一直盯如何完成清理的工作 , 如果用户没有定义析构函数,C艹 编译系统会自动生成一个析构函数,
     9 //但是他什么都不自信,想让析构函数完成任何工作,都必须在析构函数内指定 .
    10 #include<stdio.h>
    11 #include<string.h>
    12 #include<math.h>
    13 #include<iostream>
    14 #include<algorithm>
    15 #include<queue>
    16 #include<vector>
    17 #include<set>
    18 #include<stack>
    19 #include<string>
    20 #include<sstream>
    21 #include<map>
    22 #include<cctype>
    23 #include<limits.h>
    24 using namespace std;
    25 class student          //  声明一个 student 类
    26 {
    27 public:
    28     student(int n=10000000,string nam="蹦擦擦",char s='A')  //构造一个  有参函数
    29     {
    30         num=n;
    31         name=nam;
    32         sex=s;
    33         cout<<"Constructor called ."<<endl; //  输出 有关信息
    34     }
    35     ~student()              //定义  析构函数
    36     {
    37         cout<<" 程序 结束  调用 析构函数  "<<num<<endl;
    38     }
    39     void display()          //定义 成员函数
    40     {
    41         cout<<"num: "<<num<<endl;
    42         cout<<"name: "<<name<<endl;
    43         cout<<"sex: "<<sex<<endl;
    44     }
    45 private:       //   你懂得
    46     int num;
    47     string name;
    48     char sex;
    49 };
    50 int main()
    51 {
    52     student stud1;//   析构函数的调用顺序采用的是栈的顺序 .
    53     stud1.display();
    54     student stud2(10011,"zhang_feng",'m');
    55     stud2.display();
    56     return 0;
    57 }
  • 相关阅读:
    curl continue
    actor
    nginx
    jmx additional port
    diff
    lsof
    zk reconnect
    Python:Python基础(一)
    Python:初识Python(二)
    Python:初识Python(一)
  • 原文地址:https://www.cnblogs.com/A-FM/p/5228844.html
Copyright © 2020-2023  润新知