带有默认参数的构造函数

上传人:206****923 文档编号:51714495 上传时间:2018-08-16 格式:PPT 页数:4 大小:93KB
返回 下载 相关 举报
带有默认参数的构造函数_第1页
第1页 / 共4页
带有默认参数的构造函数_第2页
第2页 / 共4页
带有默认参数的构造函数_第3页
第3页 / 共4页
带有默认参数的构造函数_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《带有默认参数的构造函数》由会员分享,可在线阅读,更多相关《带有默认参数的构造函数(4页珍藏版)》请在金锄头文库上搜索。

1、#include #include using namespace std; class complex public: complex(double r=0.0,double i=0.0);/在声明构造函数时指定默认参数值 double abscomplex(); private: double real; double imag; ; complex:complex(double r,double i)/定义构造函数 real=r; imag=i; double complex:abscomplex() double t; t=real*real+imag*imag; return sqr

2、t(t); int main() complex s1;/不传递参数 complex s2(1.1);/仅传递一个参数 complex s3(1.1,2.2);/传递两个参数 cout #include using namespace std; class complex public: complex();/默认构造函数 complex(double r=0.0,double i=0.0);/在声明构造函数时指定默认参数值,亦属于默认构造函数 double abscomplex(); private: double real; double imag; ; Complex:complex(d

3、ouble r,double i) real=r; imag=i; double complex:abscomplex() double t; t=real*real+imag*imag; return sqrt(t); int main() complex s1;/系统无法识别,产生二义性 complex s2(1.1);/系统无法识别,产生二义性 complex s3(1.1,2.2); couts1.abscomplex()endl; couts2.abscomplex()endl; couts3.abscomplex()endl; return 0; 一个类只能 有一个默认 构造函数warning C4520: complex : multiple default constructors specified默认构造 函数指定多个 C:Program FilesMicrosoft Visual StudioMyProjectsg16.cpp(21) : error C2668: complex:complex : ambiguous call to overloaded function 执行 cl.exe 时出错.16.obj - 1 error(s), 0 warning(s)错误提示

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号