下载此文档

重载运算符.doc


文档分类:IT计算机 | 页数:约33页 举报非法文档有奖
1/33
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/33 下载此文档
文档列表 文档介绍
1001:ProblemAProblemDescription重载运算符“>”,用于对给定的两个字符串进行比较,返回值为一个整数:s1>s2,返回1;s1<s2,返回-1;s1==s2,返回0。类的结构参考如下:classstring{private:char*str;public:string();string(constchar*);~string(){}intoperator>(conststring&);};将类定义完整,并在主函数中进行测试。Input输入数据有多行,每行包括要比较的两个字符串。Output输出有多行,对应每行输入输出一行。SampleInputabcdabCd09abab09abcdefghhelhelSampleOutput1-1-10#include<iostream>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,); returnk; }};intmain(){ chara1[100],a2[100]; intn; while(cin>>a1>>a2) { tringA1(a1); tringA2(a2); n=A1>A2; cout<<n<<endl; } return0;}1002:ProblemBProblemDescription定义一个字符串类,该类包括一个字符型指针数据成员,构造函数、析构函数、显示字符串函数,以及重载=运算符函数(用于字符串赋值)。在主函数中对字符串对象的赋值运算进行测试。Input输入数据有多行,每行包括一个字符串。Output输出有多行,对应每个输入数据要求输出两行,第一行是该输入的字符串对象,第二行是定义另一个对象进行赋值运算,将第一个对象赋值给第二个对象。SampleInputHello!530ithinkyou!SampleOutputHello!Hello!530ithinkyou!530ithinkyou!#include<iostream>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()+1]; strcpy(arr,); return*this; } voidprint() { cout<<arr<<endl; }};intmain(){ chara[100]; while((a,100,'\n')) { Aa1(a); Aa2; (); a2=a1; (); } return0;}1003:ProblemCProblemDescription设计一个计数器Counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。Input输入数据有多组,每组包含两个整数。第一个数m为计数器的基数,第二个数n(n>0为计数器增加,n<0计数器减少)的数。Output输出计数器自增或自减的结果。每组输出占一行。SampleInput1105-4SampleOutput111#include<iostream>usingnamespacestd;classcounter{private: intn;public: counter(intx){n=x;}counteroperator++(){++n;return*this;}counteroperator++(int){countert=*this;n++;returnt;} friendcounteroperator--(counter&p){--;returnp;} friendcounteroperator--(counter&p,int){--;returnp;}voidprint(){cout<<n<<endl;}};intmain(){intm,n,i;while(cin>>m>>n){counterc(m);if(n>0) for(i=0;i<n;i++;)c++;elsefor(i=0;i>n;i--)c--;();return0;}运算符重载——重载+和-对复数类对象进行加减运算ProblemDesc

重载运算符 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数33
  • 收藏数0 收藏
  • 顶次数0
  • 上传人ogthpsa
  • 文件大小49 KB
  • 时间2020-05-29