• 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
    }

  • 相关阅读:
    000_linux之Ubuntu安装
    001_linux基础命令
    018_linux驱动之_阻塞和非阻塞
    019_linux驱动之_定时器的引入
    017_linux驱动之_信号量
    016_linux驱动之_原子操作
    python logging模块整理
    python sys与shutil模块
    python configparser模块
    python os模块
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7280086.html
Copyright © 2020-2023  润新知