c++学习笔记(十二):构造函数和析构函数

来源:互联网 发布:b超数据看男孩女孩 编辑:程序博客网 时间:2024/03/29 22:52

类的构造函数:

类的构造函数是,每当我们创建该类的新对象时执行一类特殊的成员函数。

构造函数都会有完全相同的名字作为类,它没有任何返回类型可言,甚至没有作废。构造可以为某些成员变量设置的初始值非常有用。

下面的示例说明构造函数的概念:

#include <iostream> using namespace std; class Line{   public:      void setLength( double len );      double getLength( void );      Line();  // This is the constructor    private:      double length;}; // Member functions definitions including constructorLine::Line(void){    cout << "Object is being created" << endl;} void Line::setLength( double len ){    length = len;} double Line::getLength( void ){    return length;}// Main function for the programint main( ){   Line line;    // set line length   line.setLength(6.0);    cout << "Length of line : " << line.getLength() <<endl;    return 0;}

当上述代码被编译和执行时,它产生了以下结果:

Object is being createdLength of line : 6

带参数的构造函数:

默认的构造函数没有任何参数,但如果需要,构造函数可以有参数。这有助于在其创建时的初始值赋给一个对象作为显示,如下面的例子:

#include <iostream> using namespace std; class Line{   public:      void setLength( double len );      double getLength( void );      Line(double len);  // This is the constructor    private:      double length;}; // Member functions definitions including constructorLine::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;}// Main function for the programint main( ){   Line line(10.0);    // get initially set length.   cout << "Length of line : " << line.getLength() <<endl;   // set line length again   line.setLength(6.0);    cout << "Length of line : " << line.getLength() <<endl;    return 0;}

当上述代码被编译和执行时,它产生了以下结果:

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

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

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

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

上面的语法等于如下语法:

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

如果对于一个C类,则具有多个字段的X,Y,Z等,进行初始化,然后用可以使用相同的语法和由逗号分隔字段如下:

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

类析构函数:

析构函数是执行一个类的特殊成员函数时,它的类的对象超出范围或当删除表达式应用到一个指向类的对象。

析构函数将有完全相同的名称作为前缀类带有波浪号(〜),它可以没有返回值,也不能采取任何参数。析构函数可以释放资源,跳出程序很像在关闭文件之前,释放内存等非常有用

以下举例说明析构函数的概念:

#include <iostream> using namespace std; class Line{   public:      void setLength( double len );      double getLength( void );      Line();   // This is the constructor declaration      ~Line();  // This is the destructor: declaration    private:      double length;}; // Member functions definitions including constructorLine::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;}// Main function for the programint main( ){   Line line;    // set line length   line.setLength(6.0);    cout << "Length of line : " << line.getLength() <<endl;    return 0;}

让我们编译和运行上面的程序,这将产生以下结果:

Object is being createdLength of line : 6Object is being deleted



0 0
原创粉丝点击