欢迎来到天天文库
浏览记录
ID:34361240
大小:62.00 KB
页数:10页
时间:2019-03-05
《【例2.12】重载函数应用举例》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
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=”<2、stream.h>#includeclassStudent{public:Student(char*pName="noname",intssId=0){strncpy(name,pName,40);name[39]=' ';id=ssId;cout<<"Constructingnewstudent"<3、ame,"copyof");----strcat(name,s.name);id=s.id;}~Student(){cout<<"Destructing"<4、ang",5678);//调用构造函数,创建对象wangcout<<"Callingfn()";fn(randy);//调用fn函数,参数传递时调用拷贝构造函数cout<<"Returnedfromfn()";}//主函数调用结束时,先析构对象wang,再析构对象randy程序的运行结果为:ConstructingnewstudentRandyConstructingnewstudentwangCallingfn()ConstructingcopyofRandyInfunctionfn()D5、estructingcopyofRandyReturnedfromfn()DestructingwangDestructingRandy缺省构造函数#include#includeusingnamespacestd;classStudent{public:----Student(char*pName){cout<<"calloneparameterconstructor"<6、zeof(name)-1]=' ';cout<<"thenameis"<7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
2、stream.h>#includeclassStudent{public:Student(char*pName="noname",intssId=0){strncpy(name,pName,40);name[39]=' ';id=ssId;cout<<"Constructingnewstudent"<3、ame,"copyof");----strcat(name,s.name);id=s.id;}~Student(){cout<<"Destructing"<4、ang",5678);//调用构造函数,创建对象wangcout<<"Callingfn()";fn(randy);//调用fn函数,参数传递时调用拷贝构造函数cout<<"Returnedfromfn()";}//主函数调用结束时,先析构对象wang,再析构对象randy程序的运行结果为:ConstructingnewstudentRandyConstructingnewstudentwangCallingfn()ConstructingcopyofRandyInfunctionfn()D5、estructingcopyofRandyReturnedfromfn()DestructingwangDestructingRandy缺省构造函数#include#includeusingnamespacestd;classStudent{public:----Student(char*pName){cout<<"calloneparameterconstructor"<6、zeof(name)-1]=' ';cout<<"thenameis"<7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
3、ame,"copyof");----strcat(name,s.name);id=s.id;}~Student(){cout<<"Destructing"<4、ang",5678);//调用构造函数,创建对象wangcout<<"Callingfn()";fn(randy);//调用fn函数,参数传递时调用拷贝构造函数cout<<"Returnedfromfn()";}//主函数调用结束时,先析构对象wang,再析构对象randy程序的运行结果为:ConstructingnewstudentRandyConstructingnewstudentwangCallingfn()ConstructingcopyofRandyInfunctionfn()D5、estructingcopyofRandyReturnedfromfn()DestructingwangDestructingRandy缺省构造函数#include#includeusingnamespacestd;classStudent{public:----Student(char*pName){cout<<"calloneparameterconstructor"<6、zeof(name)-1]=' ';cout<<"thenameis"<7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
4、ang",5678);//调用构造函数,创建对象wangcout<<"Callingfn()";fn(randy);//调用fn函数,参数传递时调用拷贝构造函数cout<<"Returnedfromfn()";}//主函数调用结束时,先析构对象wang,再析构对象randy程序的运行结果为:ConstructingnewstudentRandyConstructingnewstudentwangCallingfn()ConstructingcopyofRandyInfunctionfn()D
5、estructingcopyofRandyReturnedfromfn()DestructingwangDestructingRandy缺省构造函数#include#includeusingnamespacestd;classStudent{public:----Student(char*pName){cout<<"calloneparameterconstructor"<6、zeof(name)-1]=' ';cout<<"thenameis"<7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
6、zeof(name)-1]=' ';cout<<"thenameis"<7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
7、thenameisJenny友元函数的定义和使用#definestrmax32#include#includeclassmyclass{intx,y;char*string;public:myclass(intvx,intvy,char*str);friendintsum(myclassc1);//友元函数的声明friendintstrlength(myclassc1);//友元函数的声明friendvoidprint(myclassc1);//友元函数的
8、声明};myclass::myclass(intvx,intvy,char*str){x=vx;y=vy;string=newchar[strmax];strcpy(string,str);----}intsum(myclassc1)//友元函数的定义{returnc1.x+c1.y;}intstrlength(myclassc1)//友元函数的定义{returnstrlen(c1.string);}voidprint(myclassc1)//友元函数的定义{cout
此文档下载收益归作者所有