C++ class constructor & destructor

C++ Class Constructor & Destructor
Class Constructor
A class constructor is a special member function of a class that is executed every time a new object of the class is created.
Constructors can be used to set initial values ​​for some member variables.
The name of the constructor is exactly the same as the name of the class, and it does not return any type, so it does not represent any type
��
Default default constructor
The following example helps to better understand the concept of constructor:
instance

#include <iostream>
using namespace std;
class Line��
//
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();  // 这是构造函数
   private:
      double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
    length=1;
//可以直接初始化成员数据
    cout << "Object is being created" << endl;
}
void Line::setLength( double len )
{
    length = len;
}
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
//初始化方式一样
   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 of line : 1
Length of line : 6

Constructors with Parameters The
default constructor does not have any parameters, but the constructor can also take parameters if desired. This will assign an initial value to the object when it is created, 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 result:
Object is being created, length = 10
Length of line : 10
Length of line : 6


Initialize fields using an initialization list Use an initialization list to initialize fields:
Line::Line( double len): length(len)
{
cout << “Object is being created, length = ” << len << endl;
}
The above syntax Equivalent to the following syntax:
Line::Line( double len)
{
cout << “Object is being created, length = ” << len << endl;
length = len;
}
Suppose there is a class C with multiple fields X, Y, Z, etc. need to be initialized. Similarly, you can use the above syntax, just use commas to separate the different fields, as follows:
C::C( double a, double b, double c): X (a), Y(b), Z(c)
{
….
}

Destructor of a
class A destructor of a class is a special member function of a class that is executed each time an object created is deleted.
The name of the destructor is exactly the same as the name of the class, just prefixed with a tilde (~), it does not return any value and cannot take any parameters. Destructors help free resources before exiting the program (such as closing files, freeing memory, etc.).
The following examples help to better understand the concept of constructors:
Example

#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 result:
Object is being created
Length of line : 6
Object is being deleted

Constructor application example:

#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
    string name;
    string number;
    char X;
    int year;
    Student(string,string,char,int);      //构造函数声明
    void xianshi(void);     //用于输出类成员的值
};

//成员函数定义,包括构造函数
Student::Student(string N,string n,char x,int y)    //利用构造函数给类的成员赋值
{
    name = N;
    number = n;
    X = x;
    year = y;
}

void Student::xianshi()     //输出成员的值
{
    cout<<name<<endl;
    cout<<number<<endl;
    cout<<X<<endl;
    cout<<year<<endl;
}

int main()                             //主函数
{
    cout<<"输入姓名:";
    string N;
    cin>>N;
    cout<<"输入学号:";
    string n;
    cin>>n;
    cout<<"输入性别(M 或 W):";
    char x;
    cin>>x;
    cout<<"输入年龄:";
    int y;
    cin>>y;
    Student S(N,n,x,y);               //定义对象并对构造函数赋值
    S.xianshi();                           //引用输出函数
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325381762&siteId=291194637