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. 如何在debug模式下,使用正式的签名文件

    有两种方式(在集成第三方库的使用 使用的非常多)  签名配置信息 一是直接按F4,在项目结构面板中进行设置,只要操作两个两个选项卡就好了,signing(生成配置信息)和build types(打包类 ...

  2. microsoft visual studio遇到了问题,需要关闭

    http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13821 装上这个补丁: WindowsXP-KB971513-x86-CH ...

  3. 正则表达式之 /^(\d)$/

    ^(\d)$就是0-9的任意一个数字, ^表示以...开头,\d表示0-9的数字,$表示以...结尾,

  4. Microsoft SQL Server 2008 R2

    1概述 Microsoft SQL Server 2008 R2 提供完整的企业级技术与工具,帮助您以最低的总拥有成本获得最有价值的信息.您可以充分享受高性能,高可用性,高安全性,使用更多的高效管理与 ...

  5. Modify Headers模拟不同地域进行网页测试

    今天要简单讲一下Modify Headers这个Firefox插件,记录一下我是怎么使用它的. Modify Headers: https://addons.mozilla.org/zh-CN/fir ...

  6. 多重采样(MultiSample)下的FBO反锯齿 【转】

    在三维渲染的过程中,锯齿总是让人讨厌的东西.抗锯齿的一种采用方式是多重采样,本文主要小记一下FBO与多重采样的关系.——ZwqXin.com 首先,关于FBO(Frame Buffer Object) ...

  7. 【IE兼容性】background:transparent IE中Bug,不能选中input输入框,出现这个问题主要是IE8

    先解释下,background:transparent,默认在IE上会被解析成 background: none transparent scroll repeat 0% 0% transparent ...

  8. Json杂谈系列------(一)初始json

    1. JSON 是什么 JSON,全称是 JavaScript Object Notation,即 JavaScript 对象标记法.这是一种轻量级(Light-Weight).基于文本的(Text- ...

  9. SRS Audio Sandbox没有声音怎么办

    首先介绍下这款软件呵: SRS Audio Sandbox是一款个人电脑终极音频增强软件.该软件可以提供令人叹为观止的环绕音效,重低音效果并且非常清晰,甚至可以用於桌面扬声器.可以作用於个人电脑上的所 ...

  10. unity Chan!下载

    http://unity-chan.com/download/datadownload.html