整理思维导图
课上类实现> 、<、!=、||、!和后自增、前自减、后自减运算符的重载
代码部分:
#include <iostream>
using namespace std;
class complex
{int rel;int vir;
public:complex(int rel,int vir):rel(rel),vir(vir){}complex(){}void show();complex operator+(complex other);complex operator-(complex other);complex operator*(complex other);complex operator/(complex other);bool operator==(const complex other);bool operator>(const complex other);friend bool operator&&(const complex c1,const complex c2);friend bool operator||(const complex c1,const complex c2);complex operator++();complex operator++(int);complex operator--();complex operator--(int);
};
//+
complex complex::operator+(complex other)
{complex temp;temp.rel=this->rel+other.rel;temp.vir=this->vir+other.vir;return temp;
}
//-
complex complex::operator-(complex other)
{complex temp;temp.rel=rel+other.rel;temp.vir=vir+other.vir;return temp;
}
//*
complex complex::operator*(complex other)
{complex temp;temp.rel=rel*other.rel;temp.vir=vir*other.vir;return temp;
}
// /
complex complex::operator/(complex other)
{complex temp;temp.rel=rel/other.rel;temp.vir=vir/other.vir;return temp;
}
// ==
bool complex::operator==(const complex other)
{return this->rel==other.rel&&this->vir==other.vir;
}
// >
bool complex::operator>(const complex other)
{return this->rel>other.rel&&this->vir>other.vir;
}
//&&
bool operator&&(const complex c1,const complex c2)
{return (c1.rel||c1.vir)&&(c2.rel||c2.vir);
}
//||
bool operator||(const complex c1,const complex c2)
{return (c1.rel&&c1.vir)||(c2.rel&&c2.vir);
}
//前++
complex complex::operator++()
{this->rel=this->rel+1;this->vir=this->vir+1;return *this;
}
//后++
complex complex::operator++(int)
{complex temp;temp.rel=this->rel;temp.vir=this->vir;this->rel=this->rel+1;this->vir=this->vir+1;return temp;
}
//前--
complex complex::operator--()
{complex temp;temp.rel=--(this->rel);temp.vir=--(this->vir);return temp;
}
//后--
complex complex::operator--(int)
{complex temp;temp.rel=this->rel;temp.vir=this->vir;this->rel=this->rel-1;this->vir=this->vir-1;return temp;
}
void complex::show()
{cout << rel << "+" << vir << "i" << endl;}
int main()
{int num1=12,num2=28;num2=num1+num2;complex c1(num1,num2);complex c2(8,2);complex c3=c1.operator+(c2);c3=c1-c2;c3=c1*c2;c3=c1/c2;c3.show();cout << (c3==c2) << endl;cout << (c3>c2) << endl;cout << "c1&&c2?" << endl;cout << (c1&&c2) << endl;cout << "c1||c2?" << endl;cout << (c1||c2) << endl;complex c4(1,1);complex c6(1,1);complex c5(2,2);c5=c6+(c4++);c5.show();--c5;c5.show();c5--;c5.show();return 0;
}
效果图:
实现昨天作业中My_string类中的+,==,>运算符重载
代码部分:
#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class My_string
{char *str; //记录C风格的字符串int size; //记录字符串长度
public://无参构造My_string():str(new char('\0')),size(0){cout << "My_string的无参构造" << endl;}//有参构造My_string(const char *p):str(new char[strlen(p)+1]),size(strlen(p)){strcpy(str,p);cout << "My_string的有参构造" << endl;}//拷贝构造My_string(const My_string &other):str(new char[other.size+1]),size(other.size){strcpy(str,other.str); //完成两个类对象字符串内容的拷贝cout << "拷贝构造" << endl;}//拷贝赋值My_string &operator=(const My_string &other){//先把目标类对象指针原来指向的堆地址释放delete this->str;str = new char[other.size+1]; //申请新的能容纳下other类对象字符串的空间strcpy(str,other.str); //拷贝字符串this->size = other.size;//返回自身的引用return *this;}//析构函数~My_string(){delete []str; //释放str指向的多个空间}//at函数char &my_at(int num); //可以判断访问是否合法My_string operator+(My_string &other);bool operator>(My_string &other);bool operator==(My_string &other);
};
char &My_string::my_at(int num)
{if(num<0||num>=this->size){cout << "访问越界" << endl;return c;}return str[num];
}
// +
My_string My_string::operator+(My_string &other)
{My_string temp;strcat(this->str,other.str);temp.size=this->size+other.size;return temp;
}
// ==
bool My_string::operator==(My_string &other)
{return this->size==other.size;
}
// >
bool My_string::operator>(My_string &other)
{return this->size>other.size;
}
int main()
{My_string s1 = "hello";My_string s2 = "world!";My_string s3 = s2;s1.my_at(0) = 'a';cout << s1.my_at(0) << endl;s1 = s2;}