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

}

constructors and destructors的更多相关文章

  1. C++ std::array

    std::array template < class T, size_t N > class array; Code Example #include <iostream> ...

  2. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  3. Devexpress 等待窗体

    加载窗体以及等待窗体 程序加载时,需要等待加载完成后在显示 窗体显示顺序 1. 给用户看的等待窗体 2. 加载完成后的主窗体 代码如下: 1. 等待窗体代码 #region using using S ...

  4. 【源码笔记】BlogEngine.Net 中的权限管理

    BlogEngine.Net 是个功能点很全面的开源博客系统,容易安装和实现定制,开放接口支持TrackBack,可以定义主题配置数据源等等.可谓五脏俱全,这里先记录一下它基于Membership的权 ...

  5. [Under the hood]---Matt Pietrek October 1996 MSJ

    Matt Pietrek October 1996 MSJ Matt Pietrek is the author of Windows 95 System Programming Secrets (I ...

  6. [under the hood]Reduce EXE and DLL Size with LIBCTINY.LIB

    Matt Pietrek Download the code for this article: Hood0101.exe (45KB) W ay back in my October 1996 co ...

  7. C++ Copy Elision

    故事得从 copy/move constructor 说起: The default constructor (12.1), copy constructor and copy assignment ...

  8. 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证

    背景 在需要进行表单认证的Asp.NET 5 MVC项目被创建后,往往需要根据项目的实际需求做一系列的工作对MVC 5内建的身份验证机制(Asp.NET Identity)进行扩展和定制: Asp.N ...

  9. C# 键盘钩子类

    键盘钩子类代码如下 class globalKeyboardHook { #region Constant, Structure and Delegate Definitions /// <su ...

随机推荐

  1. mysql-启动、关闭与重启

    启动 service mysqld start mysql.server start 停止 service mysqld stop mysql.server stop 重启 mysql.server ...

  2. WPF附加属性的Set函数不调用的问题

    今天写程序的时候用到了附加属性,我是用VS内置的propa的代码段来实现的,代码如下: class Attach    {        public static bool GetIsEnabled ...

  3. Nginx流量带宽请求状态统计(ngx_req_status)

    介绍           ngx_req_status 用来展示 nginx 请求状态信息,类似于 apache 的 status, nginx 自带的模块只能显示连接数等等 信息,我们并不能知道到底 ...

  4. RedisTemplate SerializationFailedException: Failed to deserialize payload 异常解决

    问题描述: 使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调 ...

  5. 直接拿来用!最火的iOS开源项目(三)

    相比Android,GitHub上的iOS开源项目更可谓是姹紫嫣红.尽管效果各异,但究其根源,却都是因为开发者本身对于某种效果的需求以及热爱.在“直接拿来用!最火的iOS开源项目”系列文章(一).(二 ...

  6. 2017.11.15 String、StringBuffer、StringBuilder的比较(todo)

    参考来自:http://blog.csdn.net/jeffleo/article/details/52194433 1.速度 一般来说,三者的速度是:StringBuilder > Strin ...

  7. Robomongo与MongoDB的故事

    Robomongo,Mongo可视化工具 哇唔,事实上她是三(阴险脸). 你看你看,界面清新,让人家心旷神怡(害羞).谁还想win+R+mongo呀呀呀?! 哎呀呀,继续···说正事. 在这里···借 ...

  8. HTML5 Canvas 绘制库存变化折线 计算出库存周转率

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  9. mysql增量备份(2/2)

    前言 这是在百度文库里看到的文章,原名叫做<MYSQL 完全与增量备份及恢复文档 >,是关于完全备份和增量备份以及恢复文档的...... 文档介绍 本文档采用 mysqldump  对数据 ...

  10. 【DP】【单调队列】【NOI2005】瑰丽华尔兹

    340. [NOI2005] 瑰丽华尔兹 ★★★ 输入文件:adv1900.in 输出文件:adv1900.out 简单对照 时间限制:1 s 内存限制:128 MB [任务描写叙述] 你跳过华尔兹吗 ...