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
}
constructors and destructors的更多相关文章
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- Devexpress 等待窗体
加载窗体以及等待窗体 程序加载时,需要等待加载完成后在显示 窗体显示顺序 1. 给用户看的等待窗体 2. 加载完成后的主窗体 代码如下: 1. 等待窗体代码 #region using using S ...
- 【源码笔记】BlogEngine.Net 中的权限管理
BlogEngine.Net 是个功能点很全面的开源博客系统,容易安装和实现定制,开放接口支持TrackBack,可以定义主题配置数据源等等.可谓五脏俱全,这里先记录一下它基于Membership的权 ...
- [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 ...
- [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 ...
- C++ Copy Elision
故事得从 copy/move constructor 说起: The default constructor (12.1), copy constructor and copy assignment ...
- 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证
背景 在需要进行表单认证的Asp.NET 5 MVC项目被创建后,往往需要根据项目的实际需求做一系列的工作对MVC 5内建的身份验证机制(Asp.NET Identity)进行扩展和定制: Asp.N ...
- C# 键盘钩子类
键盘钩子类代码如下 class globalKeyboardHook { #region Constant, Structure and Delegate Definitions /// <su ...
随机推荐
- C语言实现括号配对问题
代码如下: #include<stdio.h> #include<string.h> #include<stdlib.h> // 写一个判断的括号是否匹配的函数 i ...
- Android Studio使用过程中Java类突然报红,但项目可运行解决方案
1.点击File->Invalidate Caches / Restart... 2.重启Gradle,清除缓存 3.Clean Project
- 搭建redis集群环境
Redis的集群机制 ============================= 转自http://lib.csdn.net/article/redis/39999 别人写的,写得不错,转了. Red ...
- SQL Server在存储过程中编写事务处理代码的三种方法
SQL Server中数据库事务处理是相当有用的,鉴于很多SQL初学者编写的事务处理代码存往往存在漏洞,本文我们介绍了三种不同的方法,举例说明了如何在存储过程事务处理中编写正确的代码.希望能够对您有所 ...
- 初识JdbcTemplate
1.spring配置文件里注冊:參照使用 Spring jdbcTemplate 进一步简化 JDBC 操作 2.写javabean 3.写rowmapper(依据javabean来封装结果集) 4. ...
- Kubernetes用户指南(一)--快速开始、使用k8s配置文件
一.快速开始 1.启动一个简单的容器. 一旦在container中打包好应用并将其commit为image之后,你就可以将其部署在k8s集群上. 一个简单的nginx服务器例子: 先决条件:你需要拥有 ...
- hdu 5105 Math Problem(数学)
pid=5105" target="_blank" style="">题目链接:hdu 5105 Math Problem 题目大意:给定a.b ...
- 在eclipse导入Java 的jar包的方法 JDBC
在使用JDBC编程时需要连接数据库,导入JAR包是必须的,导入其它的jar包方法同样如此,导入的方法是 打开eclipse 1.右击要导入jar包的项目,点properties 2.左边选择java ...
- 【 D3.js 入门系列 --- 5 】 怎样加入坐标轴
本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处.谢谢. 第3节中做了一个图标.但没有为它加入一个对应的坐标轴. ...
- Python实时语音识别控制
代码地址如下:http://www.demodashi.com/demo/12946.html Python实时语音识别控制 概述 本文中的语音识别功能采用 百度语音识别库 ,首先利用 PyAudio ...