// classes and uniform initialization
#include <iostream>
using namespace std; class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
}; int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like cout << "foo's circumference: " << foo.circum() << '\n';
cout << "bar circumference: " << bar.circum() << '\n';
cout << "bazcircumference: " << baz.circum() << '\n';
cout << "qux circumference: " << qux.circum() << '\n';
return 0;
}

  

Rectangle rectb;   // default constructor called
Rectangle rectc(); // function declaration (default constructor NOT called)
Rectangle rectd{}; // default constructor called

  

Member initialization in constructors
When a constructor is used to initialize other members, these other members can be initialized directly, without resorting to statements in its body. This is done by inserting, before the constructor's body, a colon (:) and a list of initializations for class members. For example, consider a class with the following declaration:

class Rectangle {
int width,height;
public:
Rectangle(int,int);
int area() {return width*height;}
};

The constructor for this class could be defined, as usual, as:

Rectangle::Rectangle (int x, int y) { width=x; height=y; }

But it could also be defined using member initialization as:

Rectangle::Rectangle (int x, int y) : width(x) { height=y; }

Or even:

Rectangle::Rectangle (int x, int y) : width(x), height(y) { }

Note how in this last case, the constructor does nothing else than initialize its members, hence it has an empty function body.

最新文章

  1. AD域修改组策略
  2. JSP网站开发基础总结《五》
  3. 【转】Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
  4. tomcat 部署web项目异常
  5. [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换
  6. Roslyn 编译平台概述
  7. 数据恢复软件Extundelete
  8. XML文件生成——借助JDOM
  9. 利用HTTP-only Cookie缓解XSS之痛
  10. [POJ 1410] Intersection(线段与矩形交)
  11. java使用selenium版本不兼容解决汇总
  12. BZOJ4556 [Tjoi2016&amp;Heoi2016]字符串 SA ST表 二分答案 主席树
  13. 上传input中file文件到云端,并返回链接
  14. YOCVM
  15. Queue depth
  16. Flask请求上下文request
  17. Js的substring和C#的Substring
  18. webp格式
  19. C++源码里没有./configure文件的问题
  20. DataTrigger

热门文章

  1. java常见数据结构举例
  2. android 开发AlertDialog.builder对话框的实现
  3. 关于@webFilter使用@Order无效问题
  4. InteliJ idea import project 找不到文件结构解决办法
  5. &lt;rhel6 mysql replication&gt;
  6. JavaSE之Java基础(5)
  7. 用python计算直角三角形斜边长
  8. vue-elem-stylus 的mixin用法
  9. linux打开文件数测试
  10. hdu-1757 A Simple Math Problem---矩阵快速幂模板题