1,关于重载运算符应该是把 String的首地址返回吧,字符串都只存首地址,所以要用&引用地址对于MyString a[10];char ch;正常的操作有ch = a[0];或者a[0] = ch;返回char的话,前者没什么问题 , 但是后者就出问题了,所以必须返回char&
2,下面的重载运算符部分为什么要使用ostream类型另外重载运算符时如果实际使用时只有a << 1 , 即后面只跟一个<<时,输出什么类型无所谓 。但要a << 1 << 2,即后面不止一<<时,就得输出a的类型的引用 。而这个a是ostream的 , 所以要用ostream & 。具体一点说,假设a<<1返回的是b,那么接着就是b<<2,这样明白了吧 。举个例子:class achar k[8];public:a & operator<<(const char & data)for (int i=0;i<7;++i)k[i]=k[i+1];k[7]=data;return *this;}};这个例子原型是我的AVR单片机的显示缓冲,重载<<往里移要显示的代码 。这样做就很灵活,可以移1位或者更多位 。当然如果是电脑里要输出的屏幕上的那就只能是ostream &了 。有点混乱,希望能帮到你外面operator的定义把fuck::去掉,因为它是fuck类的友元函数而不是成员函数 。友元函数是公有函数 。iostream& operator<<(iostream & out,const fuck & f)out<return out; }
3,操作符重载#include <iostream.h>class CFraction//分数类的定义public: void SetCFraction(); CFraction operator+(CFraction); CFraction operator-(CFraction); CFraction operator*(CFraction); CFraction operator/(CFraction); void Show();private: int Numerator,Denominator; //分子和分母};void CFraction::SetCFraction()//初始化一个分数 int fz,fm; cout<<"请输入两个整数分别作为分数的分子和分母,其中分母不为0:"; cin>>fz>>fm; Numerator=fz; Denominator=fm;}CFraction CFraction::operator+(CFraction B) //重载+ CFraction C; C.Denominator=Denominator*B.Denominator; C.Numerator=Numerator*B.Denominator+Denominator*B.Numerator; return C;}CFraction CFraction::operator-(CFraction B)//重载- CFraction C; C.Denominator=Denominator*B.Denominator; C.Numerator=Numerator*B.Denominator-Denominator*B.Numerator; return C;}CFraction CFraction::operator*(CFraction B)//重载* CFraction C; C.Denominator=Denominator*B.Denominator; C.Numerator=Numerator*B.Numerator; return C;}CFraction CFraction::operator/(CFraction B)//重载/ CFraction C; C.Denominator=Denominator*B.Numerator; C.Numerator=Numerator*B.Denominator; return C;}void CFraction::Show() cout<<Numerator<<"/"<<Denominator<<endl;}void main()//用于测试的函数 CFraction num1,num2,num3; num1.SetCFraction(); num2.SetCFraction(); cout<<"当前的两个分数为:"<<endl; num1.Show(); num2.Show(); num3=num1+num2;//测试重载+ cout<<"两数相加的结果:"; num3.Show(); num3=num1-num2;//测试重载- cout<<"两数相减的结果:"; num3.Show(); num3=num1*num2;//测试重载* cout<<"两数相乘的结果:"; num3.Show(); num3=num1/num2;//测试重载/ cout<<"两数相除的结果:"; num3.Show(); }我没有对结果进行化简,关于化简分数的函数,你可以自己写一个,然后在每个重载函数里面调用一下就好了 , Study is very happy!【iostream重载运算符,关于重载运算符】
4,运算符重载#include <iostream.h>using namespace std;class rationalNumberprivate: int a,b;public: rationalNumber() rationalNumber(int m,int n) if(m>0) a=m; b=n; }} void display() cout<<b<<"/"<<a<<endl; } friend rationalNumber operator +(rationalNumber r1,rationalNumber r2); friend rationalNumber operator -(rationalNumber r1,rationalNumber r2); friend rationalNumber operator *(rationalNumber r1,rationalNumber r2); friend rationalNumber operator /(rationalNumber r1,rationalNumber r2); friend rationalNumber operator >(rationalNumber r1,rationalNumber r2); friend rationalNumber operator <(rationalNumber r1,rationalNumber r2); friend rationalNumber operator ==(rationalNumber r1,rationalNumber r2);};rationalNumber operator +(rationalNumber r1,rationalNumber r2) rationalNumber r; r.a=r1.a*r2.a; r.b=(r1.b*r2.a)+(r2.b*r1.a); cout<<r.b<<"/"<<r.a<<endl;}rationalNumber operator -(rationalNumber r1,rationalNumber r2) rationalNumber r; r.a=r1.a*r2.a; r.b=(r1.b*r2.a)-(r2.b*r1.a); cout<<r.b<<"/"<<r.a<<endl;}rationalNumber operator *(rationalNumber r1,rationalNumber r2) rationalNumber r; r.a=r1.a*r2.a; r.b=r1.b*r2.b; cout<<r.b<<"/"<<r.a<<endl;}rationalNumber operator /(rationalNumber r1,rationalNumber r2) rationalNumber r; r.a=r1.a*r2.b; r.b=r1.b*r2.a; cout<<r.b<<"/"<<r.a<<endl;}rationalNumber operator >(rationalNumber r1,rationalNumber r2) if((r1.b*r2.a)>(r1.a*r2.b)) cout<<"第一个分数大于第二个分数."<<endl; } else cout<<"第二个分数大于第一个分数."<<endl; } }rationalNumber operator <(rationalNumber r1,rationalNumber r2) if((r1.b*r2.a)<(r1.a*r2.b)) cout<<"第一个分数小于第二个分数."<<endl; } else cout<<"第二个分数小于第一个分数."<<endl; } }rationalNumber operator ==(rationalNumber r1,rationalNumber r2) if((r1.b*r2.a)==(r1.a*r2.b)) cout<<"第一个分数等于第二个分数."<<endl; }}void main() int a,b; cout<<"输入第一个分数的分母:"; cin>>a; cout<<"输入第一个分数的分子:"; cin>>b; rationalNumber r1(a,b); r1.display(); int aa,bb; cout<<"输入第二个分数的分母:"; cin>>aa; cout<<"输入第二个分数的分子:"; cin>>bb; rationalNumber r2(aa,bb); r2.display(); operator +(r1,r2); operator -(r1,r2); operator *(r1,r2); operator /(r1,r2); operator >(r1,r2); operator <(r1,r2); operator ==(r1,r2);}5,操作符的重载方法我做了几个操作符重载,包括= << >> ()等操作 。其他我觉得都不难class complex{public:double real;double image;public:complex& operator=(const complex& comp){real = comp.real;image = comp.image;return *this;}operator double(){return real;}};ostream& operator<<(ostream& out, complex& c ){return out<<<"+"<<<"i"; } istream& operator>>(istream& in, complex& c) { return in>>c.real>>c.image; } void main() { complex cc; complex c2; cc.real = 1; cc.image = 1; cin>>cc; cout<<//使用成员函数重载运算符#include using namespace std; class example { private: int val; public: example(int v = 0) { val = v; } void setval(int v) { val = v; } int getval() { return val; } example operator +(example); }; //成员函数重载运算符只需要一个参数 , 执行时由表达式左边的对象调用函数 example example::operator +(example o) { example t(val + o.val); return t; } int main() { example ex1(3), ex2, ex3; ex2.setval(5); ex3 = ex1 + ex2; //由ex1调用函数 cout << ex3.getval() << endl; return 0; } //使用友元函数重载运算符,主函数同上 class example { private: int val; public: example(int v = 0) { val = v; } void setval(int v) { val = v; } int getval() { return val; } friend example operator +(example, example); }; //友元函数重载运算符需要两个参数,可以直接访问类的私有数据成员 example operator +(example o1, example o2) { example t(o1.val + o2.val); return t; } //使用普通函数重载运算符,主函数同上 class example { private: int val; public: example(int v = 0) { val = v; } void setval(int v) { val = v; } int getval() { return val; } }; //普通函数重载运算符需要两个参数 , 不能直接访问类的私有数据成员 example operator +(example o1, example o2) { example t(o1.getval() + o2.getval()); return t; }你最好针对一个问题来问 , 其实网络上对于你的问题的回答有很多,你查一查要说得多大一大堆代码呀~~~看看 《C++编程思想》第一卷 操作符重载那一章就有对每个操作符该怎样重载的详细说明~~~ "()"是写仿函数的时候用的 至于 -> * 一般用不到Human类中实现了Human与Int String类型的转型,虽然从这个例子体现不出这种类型转换的好处 , 这只是演示一下实现过程而已,在软件的设计过程中对类型转换处理会带来更精妙的作用的 。using System;using System.Collections.Generic;using System.Text;using System.Runtime.CompilerServices;namespace GraceCode{class 转换操作符以及操作符方法{}internal sealed class Human{private int iSum = 12;private string[] strTmp = new string[3] {"1","2","3" };[IndexerName("Test")]public string this[int iIndex]{get{return strTmp[iIndex];}protected set{strTmp[iIndex] = value;}}public Human(int i){iSum = i;}public int ToInt32(){return this.iSum;}public string ToString(){return iSum.ToString();}public static implicit operator String(Human h){return h.ToString();}public static implicit operator Human(int i){return new Human(i);}public static explicit operator Int32(Human i){return i.ToInt32();}public static Human operator +(Human h1, Human h2){return new Human(h1.iSum + h2.iSum);}}}===========================================================Human使用的部分代码Human h1 = 10;Console.WriteLine(h1);Human h2 = 20;Console.WriteLine(h2);Human hAll = h1 + h2;Console.WriteLine(hAll);Console.Read();本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/lewisshi/archive/2007/08/24/1757836.aspx
推荐阅读
- 苹果电脑可以写代码吗,本人是学习游戏设计的 想买台电脑 用来编程 做图 请问苹果的
- 监控电脑
- 房贷计算器下载,最新准确的房贷计算器下载
- 北斗3d地图高清街景,有没有手机地图可以看到街景的是什么地图呢
- ios开发工程师是什么意思,iOS开发工程师什么性质的岗位
- 蜘蛛牌游戏
- 易风健康科技公司是做什么的?聂风儿子为啥要杀步惊云儿子
- 先锋影音在线播放,手机版先锋影音为什么不能在线播放
- 软件编程和硬件编程,硬件编程与软件编程哪个更赚钱 卖代码