码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 类构造函数&析构函数

时间:2018-03-11 21:12:49      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:etl   一段   lin   log   主函数   break   不能   ++   int   

构造函数

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

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

示例:

#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

析构函数

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

  • 析构函数与类的名称相同,只是在前面加了~
  • 析构函数不能有任何返回值和参数。

c++ 类构造函数&析构函数

标签:etl   一段   lin   log   主函数   break   不能   ++   int   

原文地址:https://www.cnblogs.com/litingyu/p/8544585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!