• C++类构造函数和析构函数


    一、构造函数

    1、构造函数初始化类对象的类的特殊成员函数。构造函数名称与类名称相同,并且没有返回类型。

    在讲述构造函数时,我先引用一段代码来供大家分析和借鉴:

    1. #include
    2. using namespace std;
    3. class constructorDemo{
    4. public:
    5. int num;
    6. char ch;
    7. /* This is a default constructor of the
    8. * class, do note that it's name is same as
    9. * class name and it doesn't have return type.
    10. */
    11. constructorDemo() {
    12. num = 100; ch = 'A';
    13. }
    14. };
    15. int main(){
    16. /* This is how we create the object of class,
    17. * I have given the object name as obj, you can
    18. * give any name, just remember the syntax:
    19. * class_name object_name;
    20. */
    21. constructorDemo obj;
    22. /* This is how we access data members using object
    23. * we are just checking that the value we have
    24. * initialized in constructor are reflecting or not.
    25. */
    26. cout<<"num: "<
    27. cout<<"ch: "<
    28. return 0;
    29. }

     输出结果:

    num: 100
    ch: A

    由上面的引例,现在分析一下构造函数与类的成员函数的不同之处。

    1)构造函数没有返回类型。成员函数具有返回类型。

    2)创建类的对象时,会自动调用构造函数。需要使用类的对象显式调用成员函数。

    3)不在类中创建任何构造函数时,C++ 编译器生成一个默认构造函数并将其插入到我们的代码中。这同样适用于成员函数。

    编译器生成的默认构造函数的外观:

    1. class XYZ
    2. {
    3. ....
    4. XYZ()
    5. {
    6. //Empty no code
    7. }
    8. };

    2、构造函数的类型

    1)默认构造函数

    默认构造函数没有任何参数(或参数)。例如:

    1. #include
    2. using namespace std;
    3. class Website{
    4. public:
    5. //Default constructor
    6. Website() {
    7. cout<<"Welcome to BeginnersBook"<
    8. }
    9. };
    10. int main(void){
    11. /*creating two objects of class Website.
    12. * This means that the default constructor
    13. * should have been invoked twice.
    14. */
    15. Website obj1;
    16. Website obj2;
    17. return 0;
    18. }

    输出:

    Welcome to BeginnersBook

    Welcome to BeginnersBook

    注:

    如果未在类中指定任何构造函数,则编译器将在代码中插入没有代码(空体)的默认构造函数。

     2)参数化构造函数

    带参数的构造函数称为参数化构造函数。这些类型的构造函数允许在创建对象时传递参数。

    假设类名是XYZ,则默认构造函数为:

    1. XYZ() {
    2. }
    3. ....
    4. XYZ obj;
    5. ....

    参数化构造函数:

    1. XYZ(int a, int b) {
    2. }
    3. ...
    4. XYZ obj(10, 20);

    如:

    1. #include
    2. using namespace std;
    3. class Add{
    4. public:
    5. //Parameterized constructor
    6. Add(int num1, int num2) {
    7. cout<<(num1+num2)<
    8. }
    9. };
    10. int main(void){
    11. /* One way of creating object. Also
    12. * known as implicit call to the
    13. * constructor
    14. */
    15. Add obj1(10, 20);
    16. /* Another way of creating object. This
    17. * is known as explicit calling the
    18. * constructor.
    19. */
    20. Add obj2 = Add(50, 60);
    21. return 0;
    22. }

    输出:

    30
    110

    二、类的析构函数

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

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

    下面引用的实例有助于更好地理解析构函数的概念:

    #include
     
    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() <  
       return 0;
    }

    输出:

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

    补充一个知识点:

    在C++中,"::"是作用域运算符,它可以用来访问命名空间、类、成员变量、成员函数等。在类的作用域内部,"::"可以用来定义和实现类的成员函数,例如Line类中就定义了一个构造函数Line(double len),它的完整声明为:Line::Line(double len)。

    这里的"Line::"表示该构造函数属于Line类的作用域,"Line(double len)"表示该函数的名称及参数列表。因此在代码中出现Line::Line(double len),表示定义并实现Line类的构造函数。

  • 相关阅读:
    可编程 USB 转串口适配器开发板 DS1302 时钟芯片参数读取与修改
    【牛客网面试必刷TOP101】二叉树篇(三)
    在进行自动化测试,遇到验证码的问题,怎么办?
    DAO 与存储库Repository模式
    什么是Vue.js的响应式系统(reactivity system)?如何实现数据的双向绑定?
    ​力扣解法汇总641-设计循环双端队列
    Linux第4课 Linux的基本操作
    css常见问题处理
    linux-如何用起来ubuntu
    Pangolin安装报错解决
  • 原文地址:https://blog.csdn.net/m0_73931287/article/details/133531806