算术运算符重载
#include <iostream>
using namespace std;
class Complex
{
public:
int real;
int imag;
Complex(int r, int i):real(r), imag(i){}
Complex operator-(Complex &c) //对减号进行重载
{
return Complex(real-c.real, imag-c.imag);
}
void print()
{
cout<<real<<((imag>0)?"+":"-")<<imag<<"i"<<endl;
}
};
Complex operator+(Complex &a, Complex &b)//对加号进行重载
{
return Complex(a.real+b.real, a.imag+b.imag);
}
int main()
{
Complex a(4,4), b(1,3);
(a+b).print();
(a-b).print();
}
操作符重载为友元函数
在某些情况下,不得不将运算符重载为全局函数,但是又需要在函数中访问类的私有变量,这时,可以将函数设为友元函数.
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(int r, int i):real(r), imag(i){}
Complex operator+(int n)
{
return Complex(real+n, imag);
}
void print()
{
cout<<real<<((imag>0)?"+":"-")<<imag<<"i"<<endl;
}
friend Complex operator+(int n, Complex &a);//友元函数
};
Complex operator+(int n, Complex &a) //
{
return Complex(a.real+n, a.imag);
}
int main()
{
Complex a(4,4);
(a+2).print();
(2+a).print(); // 为了实现这种类型的操作,需要定义一个全局函数.
}
赋值操作符重载
赋值重载函数的返回值必须是对象的引用.
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char * s;
public:
String()
{
s = new char[1];
s[0] = 0;
}
const char * cStr()
{
return s;
}
String & operator=(const char * a)
{
delete [] s;
s = new char[strlen(a) + 1];
strcpy(s, a);
return *this;
}
~ String()
{
delete [] s;
}
};
int main()
{
String s1, s2;
s1 = "hello";
cout<<"s1:"<<s1.cStr()<<endl;
s2 = s1;
cout<<"s2:"<<s2.cStr()<<endl;
s2 = "helloworld";
cout<<"s1:"<<s1.cStr()<<endl;
cout<<"s2:"<<s2.cStr()<<endl;
return 0;
}
输出为:
s1:hello
s2:hello
s1:helloworld
s2:helloworld
可以发现s1的值第二次打印时也发生了改变,这是因为s2 = s1这条语句使s2.s 指向了s1.s的地址,所以所以程序运行结果和期望不符合.为了解决这个问题,再在String类加入一个赋值运算符重载函数如下:
String & operator=(const String & s1)
{
if(&s1 == this)
return *this ;
delete [] s;
s = new char[strlen(s1.s) + 1];
strcpy(s, s1.s);
return *this;
}
发现结果正常:
s1:hello
s2:hello
s1:hello
s2:helloworld
输入输出操作符重载
#include <iostream>
using namespace std;
class Num
{
private:
int x;
public:
Num(int k):x(k){}
friend ostream & operator<<(ostream & os,Num &n);
friend istream & operator>>(istream & is,Num &n);
};
ostream & operator<<(ostream & os,Num &n)
{
os<<n.x;
return os;
}
istream & operator>>(istream & is,Num &n)
{
is>>n.x;
return is;
}
int main()
{
Num n(3);
int a;
cin>>n>>a;
cout<<n<<","<<a<<endl;
return 0;
}
输入为:1,2
运行结果为:
1,2
类型转化操作符重载
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double r, double i):real(r), imag(i){}
operator double()
{return real;}
};
int main()
{
Complex c(1.1, 2.2);
cout<<(double)c<<endl; //显式转化
cout<<c + 2<<endl;//隐式转化
return 0;
}
自增自减运算符重载
为了区分前置运算符和后置运算符,c++规定后置运算符重载函数的参数列表需要加一个int型参数.
++a 返回a的引用
a++ 返回a
#include <iostream>
using namespace std;
class Num
{
private:
int x;
public:
Num(int k):x(k){}
operator int(){return x;}
Num & operator++();
Num operator++(int);
Num & operator--();
Num operator--(int);
};
Num & Num::operator++()
{
++x;
return *this;
}
Num Num::operator++(int k)
{
Num tmp(*this);
++x;
return tmp;
}
Num & Num::operator--()
{
--x;
return *this;
}
Num Num::operator--(int k)
{
Num tmp(*this);
--x;
return tmp;
}
int main()
{
Num n(5);
cout<<n++<<endl;
cout<<++n<<endl;
cout<<n--<<endl;
cout<<--n<<endl;
return 0;
}