// 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.

C++构造函数使用的多种方法的更多相关文章

  1. CSS导航菜单水平居中的多种方法

    CSS导航菜单水平居中的多种方法 在网页设计中,水平导航菜单使用是十分广泛的,在CSS样式中,我们一般会用Float元素或是「display:inline-block」来解决.而今天主要讲解如何让未知 ...

  2. 用 Python 排序数据的多种方法

    用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...

  3. [C#解惑] #1 在构造函数内调用虚方法

    谜题 在C#中,用virtual关键字修饰的方法(属性.事件)称为虚方法(属性.事件),表示该方法可以由派生类重写(override).虚方法是.NET中的重要概念,可以说在某种程度上,虚方法使得多态 ...

  4. js判断移动端是否安装某款app的多种方法

    本文实例讲解了js判断移动端是否安装某款app的多种方法,分享给大家供大家参考,具体内容如下 第一种方法: 一:判断是那种设备 ? || u.indexOf(; //android终端或者uc浏览器 ...

  5. Gradle学习系列之二——创建Task的多种方法

    在本系列的上篇文章中,我们讲到了Gradle入门,在本篇文章中我们将讲到创建Task的多种方法. 请通过以下方式下载本系列文章的Github示例代码: git clone https://github ...

  6. SQL语句的添加、删除、修改多种方法

    SQL语句的添加.删除.修改虽然有如下很多种方法,但在使用过程中还是不够用,不知是否有高手把更多灵活的使用方法贡献出来? 添加.删除.修改使用db.Execute(Sql)命令执行操作╔------- ...

  7. 给ul中的li添加事件的多种方法

    给ul中的li添加事件的多种方法 这是一个常见,而且典型的前端面试题 <ul> <li>11111</li> <li>22222</li> ...

  8. PHP获取时间日期的多种方法

    分享下PHP获取时间日期的多种方法. <?php echo "今天:".date("Y-m-d")."<br>";     ...

  9. 转载“启动\关闭Oracle数据库的多种方法”--来自百度#Oracle

    启动\关闭Oracle数据库的多种方法 启动和关闭oracle有很多种方法. 这里只给出3种方法: l         Sql*plus l         OEM控制台 l         Wind ...

随机推荐

  1. SpringBoot | 第二十五章:日志管理之自定义Appender

    前言 前面两章节我们介绍了一些日志框架的常见配置及使用实践.一般上,在开发过程中,像log4j2.logback日志框架都提供了很多Appender,基本上可以满足大部分的业务需求了.但在一些特殊需求 ...

  2. spring mvc 注解扫描问题 ,扫描不到controller, use-default-filters="false"

    今天搭了个spring mvc项目,怎么也扫描不到controller,最后发现问题在use-default-filters="false"上面,乱copy出的问题 (默认值是tr ...

  3. <邮件的反垃圾反病毒>

    本章——发送接收邮件的工具为雷鸟 安装 # yum install dovecot-mysql.x86_64 dovecot.x86_64 -y 编辑文件 vim 10-mail.conf mail_ ...

  4. iOS开发ReactiveCocoa学习笔记(三)

    RAC常用用法: 1.监听按钮的点击事件: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame ...

  5. angular 学习笔记(3) ng-repeat遍历json并且给样式

    视图: <div ng-app="myapp" ng-controller="myctrl"> <ul> <li ng-repea ...

  6. JAXB介绍二

    链接上一遍 JAXB介绍一 , 本节主要介绍解析xml的步骤, 下面的例子是在实际项目中运用的, 把它拿出来单独写一个java运行程序. 5. 测试实例 先给出我的代码结构图: 再给出要解析的Scri ...

  7. <logger>和<root>

    <logger>用来设置某一个包或者具体一个类的日志打印级别.以及制定<appender>.<logger>可以包含零个或者多个<appender-ref&g ...

  8. WPF学习二:TextBlock和Label的区别

    TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...

  9. redis在Windows下以后台服务一键搭建集群(单机--伪集群)

    redis在Windows下以后台服务一键搭建集群(单机--伪集群) 一.概述 此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行.布置以脚本的形式,一键 ...

  10. LeetCode Reverse Words in a String 将串中的字翻转

    class Solution { public: void reverseWords(string &s) { string end="",tem="" ...