带有默认参数的构造函数.ppt

上传人:牧羊曲112 文档编号:6279034 上传时间:2023-10-12 格式:PPT 页数:4 大小:219.49KB
返回 下载 相关 举报
带有默认参数的构造函数.ppt_第1页
第1页 / 共4页
带有默认参数的构造函数.ppt_第2页
第2页 / 共4页
带有默认参数的构造函数.ppt_第3页
第3页 / 共4页
带有默认参数的构造函数.ppt_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《带有默认参数的构造函数.ppt》由会员分享,可在线阅读,更多相关《带有默认参数的构造函数.ppt(4页珍藏版)》请在三一办公上搜索。

1、#include#includeusing namespace std;class complexpublic: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 sqrt(t);int main()com

2、plex s1;/不传递参数complex s2(1.1);/仅传递一个参数complex s3(1.1,2.2);/传递两个参数couts1.abscomplex()endl;couts2.abscomplex()endl;couts3.abscomplex()endl;return 0;,对于带有参数的构造函数,在定义对象时必须给构造函数的形参传递参数的值,否则构造函数将不被执行。,如果构造函数的全部参数都指定了默认值,这时的构造函数也属于默认构造函数。一个类只能有一个默认构造函数不能同时再声明无参的构造函数。,#include#includeusing namespace std;cla

3、ss complexpublic:complex();/默认构造函数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 sqrt(t);int main()complex s1;/系统无法识别,产生二

4、义性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备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号