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

}

最新文章

  1. ORACLE导入导出数据dmp
  2. Oracle数据库的导入导出
  3. Father Christmas flymouse--POJ3160Tarjan
  4. UESTC 395 Dynamic Query System --Treap
  5. Java学习-043-获取文件在目录中的路径
  6. Codeforces 720A. Closing ceremony
  7. 浅谈对ECharts的使用
  8. leetcode:Valid Parentheses
  9. 使用Redis做MyBatis的二级缓存
  10. Angularjs总结(二)过滤器使用
  11. javascript——闭包
  12. .bind.apply() 解决 new 操作符不能用与 apply 或 call 同时使用
  13. webpack打包vue
  14. IE浏览器解决无法识别js中getElementsByClassName问题
  15. js 构建map 和list
  16. 八一八cvs vss svn和git比较
  17. Publish to a Linux Production Environment
  18. SSH连接与自动化部署工具paramiko与Fabric
  19. ZooKeeper学习2---ZooKeeper安装配置
  20. wifi设置

热门文章

  1. jvm-监控指令-jstack
  2. 阿里云(ECS)Linux客户端SSH会话连接超时OperationTimedOut
  3. Saga的实现模式——进化(Saga implementation patterns – variations)
  4. MathType如何插入连乘
  5. 更合理的 setState()
  6. git如何打补丁?
  7. Java笔记4:JDBC纯驱动方式连接Oracle
  8. 倍福TwinCAT(贝福Beckhoff)基础教程7.1 TwinCAT 如何简单执行NC功能块 TC3
  9. NSight统计数据的颜色,缩写意义是什么?来自NV Jeff Kiel 比较官方的解释!
  10. Oracle,跳出游标循环