类的构造和析构

类的构造和析构

一. 构造函数

  1. 成员变量的初始值
    在栈上创建对象,成员变量初始为随机值
    在堆和静态存储区上创建对象,成员变量初始值为0

  2. 一般而言,对象都需要一个确定的初始值

  3. 解决方案,定义初始化函数
    存在的问题

    ​ initialize只是一个普通函数,必须显式调用

    ​ 如果未调用initialize函数,运行结果是不确定的

    #include<iostream>
    using namespace std;
    
    class Test
    {
        int i;
    public:
        void print()
        {
           cout << " i = " << i << endl;
       }
    };
    
    Test g_t;
    
    int main(int argc, const char *argv[])
    {
      Test l_t;
      Test *pt = new Test;
      cout << "global var: ";
       g_t.print();
    
        cout << "local var: ";
       l_t.print();
       
      cout << "heap var: ";
      pt->print();
    
    
          return 0;
    }
    
  4. 构造函数

    C++中可以定义与类名相同的特殊成员函数
    这种特殊的成员函数叫做构造函数
    构造函数没有任何返回类型的声明
    构造函数在对象定义时自动被调用
    
    #include<iostream>
    using namespace std;
    
    class Test
    {
       int i;
    public:
      Test()
      {
          i = 10;
    	    cout << "Test()" << endl;
      }
       void print()
      {
         cout << " i = " << i << endl;
      }
    };
    
    Test g_t;
    
    int main(int argc, const char *argv[])
    {
        Test l_t;
       Test *pt = new Test;
     cout << "global var: ";
      g_t.print();
    
     cout << "local var: ";
      l_t.print();
      
       cout << "heap var: ";
       pt->print();
    
    
        return 0;
    }
    

二. 带有参数的构造函数

构造函数也遵循C++的重载规则
如果用户定义对象的同时想自己指定成员变量的值

#include<iostream>
using namespace std;

class Test
{
    int i;
public:
    Test()
    {
        i = 10;
        cout << "Test()" << endl;
    }
    
    Test(int a)
    {
        i = a;
        cout << "Test(int a)" << endl;
    }
    
    void print()
    {
        cout << " i = " << i << endl;
    }
};

三. 析构函数

  1. 一般而言,需要销毁的对象都应该做清理

  2. 解决方案

    为每个类都提供一个public的free函数
    对象不再需要时立即调用free函数进行清理
    
  3. 定义:

    释放资源时析构函数会被自动的调用
    析构函数的函数名和类名一致,前边加“~”
    析构函数的形参为“void”类型
    
    class 类名{
        ~类名(void{
          函数体
      }
    }
  4. 特点

     析构函数是对象销毁时进行清理的特殊函数
     析构函数在对象销毁时自动被调用
     析构函数是对象释放系统资源的保障
    
    #include<iostream>
    using namespace std;
    
    class Test
    {
        int i;
    public:
        Test()
      {
         i = 10;
    	    cout << "Test()" << endl;
      }
     void print()
     {
         cout << " i = " << i << endl;
     }
    
     ~Test()
    {
    	    cout << "~Test()" <<endl;
      }
    };
    
    Test g_t;
    
    int main(int argc, const char *argv[])
    {
        Test l_t;
        Test *pt = new Test;
       cout << "global var: ";
       g_t.print();
    
      cout << "local var: ";
      l_t.print();
      
      cout << "heap var: ";
      pt->print();
    
      delete pt;
    
         return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值