c++常见运算符重载程序样例
运算符重载为成员函数:
先来看看《c++语言程序设计》的定义:
样例代码:
#include <iostream>
using namespace std;
class calculate {
public:
calculate() = default;
calculate(int a1, int b1) : a(a1), b(b1) { }
calculate(const calculate &item) { a = item.a; b = item.b;}
~calculate() { }
//重载+号
calculate operator+(calculate &item) {
return calculate(a + item.a, b + item.b);
}
//重载减号
calculate operator-(calculate &item) {
return calculate(a - item.a, b - item.b);
}
//重载负号
calculate operator-() {
return calculate(-a, -b);
}
//重载后置++
calculate operator++(int) {
return calculate(a++, b++);
}
//重载后置--
calculate operator--(int) {
return calculate(a--, b--);
}
//重载前置++
calculate operator++() {
return calculate(++a, ++b);
}
//重载前置--
calculate operator--() {
return calculate(--a, --b);
}
void display() {
cout << a << ' ' << b << endl;
}
private:
int a = 1, b = 1;
};
int main() {
calculate x1;
calculate x2(3, 3);
cout << "重载+号:" << endl;
calculate x3 = x1 + x2;
x3.display();
cout << "重载-号:" << endl;
calculate x4 = x1 - x2;
x4.display();
cout << "重载负号:" << endl;
calculate x5;
x5 = -x5;
x5.display();
cout << "重载后置++:" << endl;
calculate x6;
x6++;
x6.display();
cout << "重载后置--:" << endl;
calculate x7;
x7--;
x7.display();
cout << "重载前置++:" << endl;
calculate x8;
++x8;
x8.display();
cout << "重载前置--:" << endl;
calculate x9;
--x9;
x9.display();
return 0;
}
运行结果:
重载+号:
4 4
重载-号:
-2 -2
重载负号:
-1 -1
重载后置++:
2 2
重载后置--:
0 0
重载前置++:
2 2
重载前置--:
0 0
运算符重载为成员函数:
看看《c++语言程序设计》的定义:
样例代码:
#include <iostream>
using namespace std;
class calculate {
friend ostream &operator<<(ostream &, calculate &); //将重载<<的函数声明为友元
friend istream &operator>>(istream &, calculate &); //将重载>>的函数声明为友元
friend calculate operator+(calculate &, calculate &);
friend calculate operator-(calculate &, calculate&);
friend calculate operator-(calculate &);
friend calculate operator++(calculate &, int);
friend calculate operator--(calculate &, int);
friend calculate operator++(calculate &);
friend calculate operator--(calculate &);
public:
calculate() = default;
calculate(int a1, int b1) : a(a1), b(b1) { }
calculate(const calculate &item) {a = item.a; b = item.b;}
private:
int a = 1, b = 1;
};
//重载 <<
ostream &operator<<(ostream &os, calculate &item) {
os << item.a << ' ' << item.b << endl;
return os;
}
//重载 >>
istream &operator>>(istream &is, calculate &item) {
is >> item.a >> item.b;
return is;
}
calculate operator+(calculate &item1, calculate &item2) {
return calculate(item1.a + item2.a, item1.b + item2.b);
}
calculate operator-(calculate &item1, calculate &item2) {
return calculate(item1.a - item2.a, item1.b - item2.b);
}
calculate operator-(calculate &item) {
return calculate(-item.a, -item.b);
}
calculate operator++(calculate &item, int) {
return calculate(item.a++, item.b++);
}
calculate operator--(calculate &item, int) {
return calculate(item.a--, item.b--);
}
calculate operator++(calculate &item) {
return calculate(++item.a, ++item.b);
}
calculate operator--(calculate &item) {
return calculate(--item.a, --item.b);
}
int main() {
calculate x1;
calculate x2(3, 3);
cout << "重载+号:" << endl;
calculate x3 = x1 + x2;
cout << x3;
cout << "重载-号:" << endl;
calculate x4 = x1 - x2;
cout << x4;
cout << "重载负号:" << endl;
calculate x5;
x5 = -x5;
cout << x5;
cout << "重载后置++:" << endl;
calculate x6;
x6++;
cout << x6;
cout << "重载后置--:" << endl;
calculate x7;
x7--;
cout << x7;
cout << "重载前置++:" << endl;
calculate x8;
++x8;
cout << x8;
cout << "重载前置--:" << endl;
calculate x9;
--x9;
cout << x9;
cout << "重载>>和<<:" << endl;
calculate x10;
cin >> x10;
cout << x10;
return 0;
}
运行结果:
重载+号:
4 4
重载-号:
-2 -2
重载负号:
-1 -1
重载后置++:
2 2
重载后置--:
0 0
重载前置++:
2 2
重载前置--:
0 0
重载>>和<<:
10 10
10 10
需要注意的一些地方: