重载运算符.docx
- 文档编号:8292097
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:25
- 大小:20.05KB
重载运算符.docx
《重载运算符.docx》由会员分享,可在线阅读,更多相关《重载运算符.docx(25页珍藏版)》请在冰豆网上搜索。
重载运算符
1001:
ProblemA
ProblemDescription
重载运算符“>”,用于对给定的两个字符串进行比较,返回值为一个整数:
s1>s2,返回1;s1 类的结构参考如下: classstring { private: char*str; public: string(); string(constchar*); ~string(){} intoperator>(conststring&); }; 将类定义完整,并在主函数中进行测试。 Input 输入数据有多行,每行包括要比较的两个字符串。 Output 输出有多行,对应每行输入输出一行。 SampleInput abcdabCd 09abab09 abcdefgh helhel SampleOutput 1 -1 -1 0 #include usingnamespacestd; classtring { private: char*str; public: tring(){} tring(char*s) { str=newchar[strlen(s)+1]; strcpy(str,s); } intoperator>(consttring&op) { intk; k=strcmp(str,op.str); returnk; } }; intmain() { chara1[100],a2[100]; intn; while(cin>>a1>>a2) { tringA1(a1); tringA2(a2); n=A1>A2; cout< } return0; } 1002: ProblemB ProblemDescription 定义一个字符串类,该类包括一个字符型指针数据成员,构造函数、析构函数、显示字符串函数,以及重载=运算符函数(用于字符串赋值)。 在主函数中对字符串对象的赋值运算进行测试。 Input 输入数据有多行,每行包括一个字符串。 Output 输出有多行,对应每个输入数据要求输出两行,第一行是该输入的字符串对象,第二行是定义另一个对象进行赋值运算,将第一个对象赋值给第二个对象。 SampleInput Hello! 530ithinkyou! SampleOutput Hello! Hello! 530ithinkyou! 530ithinkyou! #include usingnamespacestd; classA { private: char*arr; public: A(){} A(char*s) { arr=newchar[strlen(s)+1]; strcpy(arr,s); } ~A() {} A&operator=(constA&s) { arr=newchar[strlen(s.arr)+1]; strcpy(arr,s.arr); return*this; } voidprint() { cout< } }; intmain() { chara[100]; while(cin.getline(a,100,'\n')) { Aa1(a); Aa2; a1.print(); a2=a1; a2.print(); } return0; } 1003: ProblemC ProblemDescription 设计一个计数器Counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。 Input 输入数据有多组,每组包含两个整数。 第一个数m为计数器的基数,第二个数n(n>0为计数器增加,n<0计数器减少)的数。 Output 输出计数器自增或自减的结果。 每组输出占一行。 SampleInput 110 5-4 SampleOutput 11 1 #include usingnamespacestd; classcounter { private: intn; public: counter(intx){n=x;} counteroperator++(){++n;return*this;} counteroperator++(int){countert=*this;n++;returnt;} friendcounteroperator--(counter&p){--p.n;returnp;} friendcounteroperator--(counter&p,int){p.n--;returnp;} voidprint() {cout< } }; intmain() { intm,n,i; while(cin>>m>>n) { counterc(m); if(n>0) for(i=0;i c++; elsefor(i=0;i>n;i--) c--; c.print(); return0; } 运算符重载——重载+和-对复数类对象进行加减运算 ProblemDescription 定义一个复数类,该类包含两个double型的数据成员代表复数的实部和虚部,包含构造函数(默认值为0,0),和显示函数,现重载运算符+和-,使其能对复数类对象进行加和减运算。 在主函数中进行测试 Input 输入数据有多行,每行包括4个数,前两个代表参与运算的第一个复数对象的实部和虚部,后两个代表第二个复数对象的实部和虚部。 Output 输出多行,每行包括了加和减运算后的结果。 SampleInput 10020-1020 10232034 2.563.51.3 SampleOutput (90,40)(110,0) (30,57)(-10,-11) (6,7.3)(-1,4.7) #include classComplex { public: Complex(doubler=0.0,doublei=0.0); friendComplexoperator+(Complex&a,Complex&b); friendComplexoperator-(Complex&a,Complex&b); voidprint1() { cout<<"("< } voidprint2() { cout<<"("< } private: doublereal; doubleimag; }; Complex: : Complex(doubler,doublei) { real=r; imag=i; } Complexoperator+(Complex&a,Complex&b) { Complextemp; temp.real=a.real+b.real; temp.imag=a.imag+b.imag; returntemp; } Complexoperator-(Complex&a,Complex&b) { Complextemp; temp.real=a.real-b.real; temp.imag=a.imag-b.imag; returntemp; } intmain() { doublea1,b1; doublea2,b2; while(cin>>a1>>b1>>a2>>b2) { Complexop1(a1,b1); Complexop2(a2,b2); Complexop3,op4; op3=op1+op2; op3.print1(); op4=op1-op2; op4.print2(); } return0; } #include #include usingnamespacestd; classPoint { doublea,b,c,d; public: Point(doublei=0,doublej=0,doublek=0,doublel=0) { a=i;b=j;c=k;d=l; } voidsd() {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 重载 运算