C++ | C++ 类 & 对象 | 类构造函数 & 析构函数

C++ | C++ 类 & 对象 | 类构造函数 & 析构函数

类的构造函数

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

构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。
构造函数可用于为某些成员变量设置初始值。

实例1:

/*******************************************************************
 *   > File Name: classConstructor.cpp
 *   > Create Time: 2021年09月 2日 22:17:46
 ******************************************************************/
#include <iostream>
using namespace std;

class Line{
    public:
        void setLength(double len);
        double getLenght(void);
        Line();
    private:
        double lenght;
};

Line::Line(void)
{
    cout << "Object is being created." << endl;
}

void Line::setLength(double len)
{
    lenght = len;
}

double Line::getLenght(void)
{
    return lenght;
}

int main(void)
{
    Line line;

    line.setLength(10.0);
    cout << "The length of line: " << line.getLenght() << endl;
    
    return 0;
}

编译、运行:

PS D:\study\cplusplus\day4> make
g++ -o classConstructor classConstructor.cpp -g -Wall
PS D:\study\cplusplus\day4> .\classConstructor.exe
Object is being created.
The length of line: 10

带参数的构造函数

默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值。

实例2:

/*******************************************************************
 *   > File Name: classConstructor1.cpp
 *   > Create Time: 2021年09月 2日 22:26:54
 ******************************************************************/
#include <iostream>
using namespace std;

class Line{
    public:
        void setLength(double len);
        double getLenght(void);
        Line(double len);
    private:
        double lenght;
};

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

void Line::setLength(double len)
{
    lenght = len;
}

double Line::getLenght(void)
{
    return lenght;
}

int main(void)
{
    Line line(99.0);

    cout << "The length of line: " << line.getLenght() << endl;
    line.setLength(10.0);
    cout << "The length of line: " << line.getLenght() << endl;
    
    return 0;
}

编译、运行:

PS D:\study\cplusplus\day4> make
g++ -o classConstructor1 classConstructor1.cpp -g -Wall
PS D:\study\cplusplus\day4> .\classConstructor1.exe
Object is being created, len = 99
The length of line: 99
The length of line: 10

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

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

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

等价于:

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

假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化。
同理地,可以使用上面的语法,只需要在不同的字段使用逗号进行分隔。

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

类的析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

实例3:

/*******************************************************************
 *   > File Name: classDestructor.cpp
 *   > Create Time: 2021年09月 2日 22:40:34
 ******************************************************************/
#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(void)
{
    Line line;

    line.setLength(6.0);
    cout << "the length of line: " << line.getLength() << endl;

    return 0;
}

编译、运行:

PS D:\study\cplusplus\day4> make
g++ -o classDestructor classDestructor.cpp -g -Wall
PS D:\study\cplusplus\day4> .\classDestructor.exe
Object is being created.
the length of line: 6
Object is being deleted.

来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值