1在Complex类的基础上,完成^,>>,<<,~运算符的重载
#include <iostream>
using namespace std;
class Complex
{int rel; //实部int vir; //虚部
public:Complex(){}Complex(int rel,int vir):rel(rel),vir(vir){}void show(){cout << this->rel << "+" << this->vir << "i" << endl;}friend Complex operator-(const Complex c1,const Complex c2);friend Complex operator/(const Complex c1,const Complex c2);friend bool operator==(const Complex c1,const Complex c2);friend Complex operator++(Complex &c1,int);friend ostream &operator<<(ostream &out,Complex c1);friend istream &operator>>(istream &in,Complex &c1);Complex operator~() const { return Complex(-rel, -vir); } Complex operator^(const Complex& other) const { // 实部相减,虚部相加 return Complex(rel - other.rel, vir + other.vir); } //成员函数版实现+运算符重载Complex operator+(const Complex c1);//成员函数版实现*运算符重载Complex operator*(const Complex c1);//成员函数版%运算符重载Complex operator%(const Complex c1);// <<friend ostream &operator<<(ostream &out,Complex c1);// >>friend istream &operator>>(istream &in,Complex c2);//成员函数版>运算符重载bool operator>(const Complex c1);bool operator!();//成员函数版的前自增运算重载,单目运算符,成员函数版运算符重载无需传参Complex operator++(){++this->rel;++this->vir;return *this;}//成员函数版()运算符的重载operator int(){return this->rel;}
// operator double()
// {
// return 0.9;
// }
// operator char()
// {
// return 'a';
// }//使用operator()实现仿函数的功能int operator()(int a,int b){return a>b?a:b;}void operator()(){cout << "这是一个仿函数" << endl;}
};
//成员函数版的!运算符重载
bool Complex::operator!()
{return !(this->rel||this->vir);
}
bool Complex::operator>(const Complex c1)
{return this->rel>c1.rel;
}
ostream &operator<<(ostream &out,Complex c1){out<<c1.rel<<"+"<<c1.vir<<"i"<<endl;return out;
}
istream &operator>>(istream &in,Complex c2){in>>c2.rel>>c2.vir;return in;
}
Complex Complex::operator%(const Complex c1)
{Complex temp;temp.rel = this->rel%c1.rel;temp.vir = this->vir%c1.vir;return temp;
}
Complex Complex::operator*(const Complex c1)
{Complex temp;temp.rel = this->rel*c1.rel;temp.vir = this->vir*c1.vir;return temp;
}
Complex Complex::operator+(const Complex c1)
{//实例化了一个temp类对象,并调用了有参构造//Complex temp(this->rel+c1.rel,this->vir+c1.vir);Complex temp;temp.rel = this->rel+c1.rel;temp.vir = this->vir+c1.vir;return temp;
}
//全局函数版本的-运算符重载
Complex operator-(const Complex c1,const Complex c2)
{//实例化一个类对象,用于返回运算后的结果Complex temp;temp.rel = c1.rel-c2.rel;temp.vir = c1.vir-c2.vir;return temp;
}
//全局函数版/运算符重载
Complex operator/(const Complex c1,const Complex c2)
{Complex temp;temp.rel = c1.rel/c2.rel;temp.vir = c1.vir/c2.vir;return temp;
}
//全局函数版本==运算符重载
bool operator==(const Complex c1,const Complex c2)
{return c1.rel==c2.rel&&c1.vir==c2.vir;
}
//全局函数版后自增运算符重载
Complex operator++(Complex &c1,int)
{Complex temp(c1.rel++,c1.vir++);return temp;
}
//全局函数版<<运算符重载,
//参数是ostream类对象的引用,返回值也是ostream类对象的引用
ostream &operator<<(ostream &out,Complex c1)
{out << c1.rel << "+" << c1.vir << "i" << endl;return out;
}
//全局函数版>>运算符重载,传类对象的引用,因为需要把输入保存在类对象中
istream &operator>>(istream &in,Complex &c1)
{in >> c1.rel >>c1.vir;return in;
}
-
在昨天作业myString类的基础上,完成+、关系运算符、逻辑运算符、输入输出运算符的重载
#include <iostream>
#include <cstring>
using namespace std;
class myString
{private:char *str; //记录c风格的字符串int size; //记录字符串的实际长度public://运算符重载friend myString operator+(const myString s1,const myString s2);//+friend ostream &operator<<(ostream &out,myString s1);//左移friend istream &operator>>(istream &in,myString &s1);//右移//>、<、==、!=关系运算符重载friend bool operator>(const myString s1,const myString s2);friend bool operator<(const myString s1,const myString s2);friend bool operator==(const myString s1,const myString s2);friend bool operator!=(const myString s1,const myString s2);//无参构造myString() : str(new char[1]), size(0){str[0] = '\0';//cout<<"无参构造"<<endl;}//有参构造myString(const char* s){size = strlen(s);str = new char[size + 1];strcpy(str, s);//cout<<"有参构造"<<endl;}myString(string s1){size = s1.size();str = new char[size + 1];strcpy(str, s1.c_str());// cout<<"有参构造"<<endl;}//拷贝构造myString(const myString &other){size = other.size;str = new char[size + 1];strcpy(str, other.str);//cout<<"拷贝构造"<<endl;}//拷贝赋值函数myString& operator=(const myString &other){if (this != &other) {delete[] str;size = other.size;str = new char[size + 1];strcpy(str, other.str);//cout<<"拷贝赋值"<<endl;}return *this;}//析构函数~myString(){delete[] str;//cout<<"析构"<<endl;}//判空函数bool empty(){return size==0;}//size函数void mysize(){cout<<"size = "<<size<<endl;}//c_str函数char* c_str(){return str;}//at函数char &at(int pos){if (pos < 0 || pos >= size) {cout<<"位置不合法"<<endl;}return str[pos];}};
//+
myString operator+(const myString s1,const myString s2)
{myString temp;temp.size = s1.size+s2.size;temp.str=strcpy(temp.str,strcat(s1.str,s2.str));return temp;
}//左移运算符重载
ostream &operator<<(ostream &out,myString s1)
{out<< s1.str <<" "<<s1.size;return cout;
}
//右移运算符重载
istream &operator>>(istream &in,myString &s1)
{string s;in>>s;s1.size = s.size();strcpy(s1.str, s.c_str());return cin;
}
bool operator>(const myString s1,const myString s2)
{string s3 =s1.str;string s4 =s2.str;return s3>s4;
}
bool operator<(const myString s1,const myString s2)
{string s3 =s1.str;string s4 =s2.str;return s3<s4;
}
bool operator==(const myString s1,const myString s2)
{string s3 =s1.str;string s4 =s2.str;return s3==s4;
}
bool operator!=(const myString s1,const myString s2)
{string s3 =s1.str;string s4 =s2.str;return s3!=s4;
}