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
    }

  • 相关阅读:
    java序列化和反序列化使用总结
    什么是N+1查询?
    Oracle insert /*+ APPEND */原理解析
    Oracle redo与undo
    MongoDB(三)-- 执行JS、界面工具
    几种Bean的复制方法性能比较
    Kafka(三)-- Kafka主要参数
    Kafka(二)-- 安装配置
    Kafka(一)-- 初体验
    Nginx(十二)-- Nginx+keepalived实现高可用
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7280086.html
Copyright © 2011-2022 走看看