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. winform如何保持TreeView节点展开和折叠的状态

    转载:http://blog.sina.com.cn/s/blog_6abcacf5010138q5.html private Hashtable NodesStatus = new Hashtabl ...

  2. JavaScript 中函数节流和函数去抖的讲解

    JavaScript 中函数节流和函数去抖的讲解 我们都知道频繁触发执行一段js逻辑代码对性能会有很大的影响,尤其是在做一些效果实现方面,或者逻辑中需要进行后端请求,更是会导致卡顿,效果失效等结果,所 ...

  3. ZK的数据结构特点

    ZK的数据结构特点 ZooKeeper这种数据结构有如下这些特点: 1. 每个子目录项如NameService都被称作znode,这个znode是被它所在的路径唯一标识,如Server1这个znode ...

  4. mysql group_concat函数

    函数语法: group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ) 下面举例说明: select * ...

  5. Leagal or Not —— 拓扑排序(王道)

    Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...

  6. Eclipse+Maven远程部署项目到Tomcat中

    使用maven的自动部署功能可以很方便的将maven工程自动打包并且部署到远程tomcat服务器,省去一些繁琐的操作,节省大量时间. 我使用的tomcat版本是8.5,tomcat7和tomcat8都 ...

  7. new Thread(new ThreadStart(this.StartServer))

    Thread .new thUdpServer thUdpServer = new Thread(new ThreadStart(this.StartServer))

  8. transition状态下Mecanim动画的跳转

    来自: http://blog.csdn.net/o_oxo_o/article/details/21325901 Unity中Mecanim里面动画状态的变化,是通过设置参数(Parameter)或 ...

  9. 你应该将应用迁移到Spring 4的五个原因

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/12/five-reasons-to-migrate-spring4 Rafa ...

  10. .net Lock用法(转)

    lock就是把一段代码定义为临界区,所谓临界区就是同一时刻只能有一个线程来操作临界区的代码,当一个线程位于代码的临界区时,另一个线程不能进入临界区,如果试图进入临界区,则只能一直等待(即被阻止),直到 ...