zoukankan      html  css  js  c++  java
  • constructors and destructors

    A constructor is a method that gets called immediately when an object is allocated (on the stack or the heap).
    It is the constructor’s job to initialize the object’s attributes to sensible initial values.
    A constructor may have parameters that can inform the initial values. A constructor has the same name as the class.
    You can have more than one constructor.
    A constructor has no return type and no return statement.
    C++ gives every class a default (implicit) constructor that takes no arguments, and does nothing.




    A destructor is a method that gets called immediately when an object is de-allocated.
    It is the destructor’s job tidy up. It may need to deallocate memory on the heap, or close a file.
    A destructor may not have parameters.
    A destructor has the same name as the class, preceded with a “∼”. You can have only one constructor.
    A constructor has no return type and no return statement.

    C++ gives every class a default (implicit) destructor that does nothing.



    class Point 

    // sample class private:
    float x; // stores the x coordinate 
    float y; // stores the y coordinate
    public:
    Point(); //the constructor 
    void setX(float newX); 
    void setY(float newY); 
    float getX();
    float getY();
    ~Point(); //the destructor
    };


    Point::Point()
    {
    x = 0;
    y = 0;
    }






    Point::~Point()

    //do nothing
    }

  • 相关阅读:
    (五)Ajax修改购物车单品数量
    (四)加入购物车和购物车操作
    flask blueprint
    2.1.2 BCD码
    2.1.1进位计数制
    1.2.3 计算机系统的层次结构
    flask的宏 macro
    计算机组成原理习题
    flask模版继承和include
    flask自定义过滤器
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7280086.html
Copyright © 2011-2022 走看看