Latest web development tutorials

C ++ class constructor & destructor

C ++ Class & Objects C ++ Class & Objects

Constructor of the class

Constructor of the class is a special class member function, it executes each time a new object class is created.

Name The name of the class constructor is identical, and will not return any type, it will not return void. The constructor can be used to set the initial values ​​for certain member variable.

The following examples help to better understand the concept of the constructor:

#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;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Object is being created
Length of line : 6

The constructor with parameters

The default constructor without any arguments, but if necessary, the constructor can also have parameters. So when you create the object to the object will be assigned an initial value, as shown in the following example:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line(double len);  // 这是构造函数
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    length = len;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line(10.0);
 
   // 获取默认设置的长度
   cout << "Length of line : " << line.getLength() <<endl;
   // 再次设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Object is being created, length = 10
Length of line : 10
Length of line : 6

Using the initialization list to initialize field

Using the initialization list to initialize fields:

Line::Line( double len): length(len)
{
    cout << "Object is being created, length = " << len << endl;
}

The above syntax is equivalent to the following syntax:

Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    length = len;
}

Suppose you have a class C, having a plurality of fields X, Y, Z, etc. need to be initialized to the same reason, you can use the above syntax, just use a comma separated in different fields, as follows:

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

Class destructor

Classdestructor is a special class member function, it will be executed each time the object is created deleted.

Name The name of the class destructor is identical, only preceded by a tilde (~) as a prefix, it does not return any value, it can not have any arguments. Destructor help out of the program (such as closing files to free memory, etc.) prior to the release of resources.

The following examples help to better resolve the concept of geographical structure function:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // 这是构造函数声明
      ~Line();  // 这是析构函数声明
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Object is being created
Length of line : 6
Object is being deleted

C ++ Class & Objects C ++ Class & Objects