类型转换构造函数
2016-03-03 11:36
344 查看
目的:
实现类型的自动转换特点:
只有一个参数不是复制构造函数
#include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; class complex{ private: double real,imag; public: complex(int i)//类型转换构造函数 { cout<<"Intconstructor is called"<<endl; real=i; imag=0; } complex() { cout<<"no parameter constructor is called"<<endl; } complex(double x,double y) { real=x; imag=y; cout<<"constructor is called"<<endl; } complex(const complex&c) { real=c.real; imag=c.imag; cout<<"copy constuctor is called."<<endl; } void get_complex() { cout<<real<<"+"<<imag<<"i"<<endl; } }; complex fun() { complex a(1,2); return a; } int main() { complex c1(7,8); complex c2=12;//初始化语句,直接调用complex(int)初始化c2 c1=9;//赋值语句,9被自动转化为一个临时的complex对象,然后将临时对象的值赋值给c1 c1.get_complex(); return 0; }
程序结果:
constructor is called Intconstructor is called Intconstructor is called 9+0i
相关文章推荐
- 初步使用bootstrap快速创建页面
- 苹果个人开发者账号如何升级成公司账号
- 抓取软件输出的特定内容并按特定格式赋值给变量
- iOS-lazyLoading懒加载
- ubuntu安装etcd
- 虚拟路由器冗余协议【原理篇】VRRP详解
- 参数传递的四种形式----- URL,超链接,js,form表单
- 字节和字符
- HTML5课堂作业1
- Python和Perl绘制中国北京跑步地图的方法
- Inversion of Control(控制反转)之 Dependency Injection(依赖注入)
- linux 非root用户安装 jdk 和 tomcat
- 找回密码功能逻辑
- 中医养生口诀
- [Android 性能优化系列]内存之基础篇--Android如何管理内存
- 解决/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found的问题方法总结
- height:100%;元素撑满整个浏览器
- openssl 版本号区别
- 实现类似QQ的折叠效果
- hibernate笔记--双向一对多映射方法