zoukankan      html  css  js  c++  java
  • c++ 类构造函数&析构函数

    构造函数

    构造函数是一种特殊的函数,它会在每次创建类的新对象的时候才会执行。
    特点:

    • 构造函数的名称与类的名称完全相同
    • 不会返回任何值

    示例:

    #include <iostream>
    
    using namespace std;
    
    class Line
    {
       public:
          void setLength( double len );
          double getLength( void );
          Line();  // 这是构造函数
    
       private:
          double length;
    };
    
    // 成员函数定义,包括构造函数
    Line::Line(void)
    {
        cout << "Object is being created" << endl;
    }
    
    void Line::setLength( double len )
    {
        length = len;
    }
    
    double Line::getLength( void )
    {
        return length;
    }
    // 程序的主函数
    int main( )
    {
       Line line; //此句运行会产生“Object is being created”
       double line_length;
       // 设置长度
       line.setLength(6.0);
       cout << "Length of line : " << line.getLength() <<endl;
       //第二次设置长度
       Line line2;//此句运行会产生“Object is being created”
       line2.setLength(8.0);
       cout << "Length of line2 : " << line2.getLength() << endl;
    
       return 0;
    }
    

    以上程序编译和运行后,结果为:

    Object is being created
    Length of line : 6
    Object is being created
    Length of line2 : 8
    

    当然,构造函数也可以带参数。

    使用初始化列表来初始化字段

    在构造函数里面可以初始化字段,具体语法为:

    C::C( double a, double b, double c): X(a), Y(b), Z(c)
    {
      ....
    }
    

    相当于

    C::C( double a, double b, double c)
    {
    X = a;
    Y = b;
    Z = c;
    ....
    }
    

    下面以一段函数作为示例:

    #include <iostream>
    using namespace std;
    
    class Test
    {
            public:
                    Test(int len1, int len2, int len3);
                    void setLength(int len, int sel);
                    int getLength(int sel);
            private:
                    int length1;
                    int length2;
                    int length3;
    };
    
    Test::Test(int len1, int len2, int len3):length1(len1), length2(len2), length3(len3)
    {
            cout << "creating object" << endl;
    }
    
    void Test::setLength(int len, int sel){
            switch(sel){
                    case 1 :
                            length1 = len;
                            break;
                    case 2 :
                            length2 = len;
                            break;
                    case 3 :
                            length3 = len;
                            break;
                    default:
                            length1 = len;
            }
    }
    
    int Test::getLength(int sel){
           switch(sel){
                   case 1 :
                           return length1;
                           break;
                   case 2 :
                           return length2;
                           break;
                   case 3 :
                           return length3;
                           break;
                   default:
                           return length1;
    
           }
    }
    
    int main(){
            Test test(1,2,3);
    //      int sel;
            cout << "length1 : " << test.getLength(1) << endl;
            cout << "length2 : " << test.getLength(2) << endl;
            cout << "length3 : " << test.getLength(3) << endl;
    
            return 0;
    }
    

    该段代码的编译运行结果为

    creating object
    length1 : 1
    length2 : 2
    length3 : 3
    

    析构函数

    析构函数与类的创建函数对应,用于删除所创建的对象,以此来释放资源。
    特点:

    • 析构函数与类的名称相同,只是在前面加了~
    • 析构函数不能有任何返回值和参数。
  • 相关阅读:
    如何在eclipse+pydev运行scrapy项目
    QT下发布APP 文件(Mac)
    QT调用python脚本
    Python-Mac 安装 PyQt4-转
    <转载>在Sublime Text 2/3 中使用Git插件连接GitHub
    python+Eclipse+pydev环境搭建
    [codeforces1270G]Subset with Zero Sum 数学 建图
    [计算机网络]学习笔记
    [ubuntu] VMware Tools 安装详细过程与使用 ——主机和ubuntu虚拟机之间的文本和文件传递
    [codeforces1221D] Make The Fence Great Again dp
  • 原文地址:https://www.cnblogs.com/litingyu/p/8544585.html
Copyright © 2011-2022 走看看