资源描述:
《【例2.12】重载函数应用举例[1]》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、重载函数应用举例#includeintadd(intx,inty){intsum;sum=x+y;returnsum;}intadd(intx,inty,intz){intsum;sum=x+y+z;returnsum;}voidmain(){inta,b;a=add(5,10);b=add(5,10,20);cout<<”a=”<#includeclassStudent{public:Stu
2、dent(char*pName="noname",intssId=0){strncpy(name,pName,40);name[39]=' ';id=ssId;cout<<"Constructingnewstudent"<3、harname[40];intid;};voidfn(Students){cout<<"Infunctionfn()";//fn函数调用结束时,析构对象s}voidmain(){Studentrandy("Randy",1234);//调用构造函数,创建对象randyStudentwang("wang",5678);//调用构造函数,创建对象wangcout<<"Callingfn()";fn(randy);//调用fn函数,参数传递时调用拷贝构造函数cout<<"Returnedfromfn()";}//主函数调用结束时,先析构对象wang,再析构对象randy程序的运行结果为:
4、ConstructingnewstudentRandyConstructingnewstudentwangCallingfn()ConstructingcopyofRandyInfunctionfn()DestructingcopyofRandyReturnedfromfn()DestructingwangDestructingRandy缺省构造函数#include#includeusingnamespacestd;classStudent{public:Student(char*pName){cout<<"calloneparameterconstruct
5、or"<