感谢内容提供者:金牛区吴迪软件开发工作室
接上一篇:C++程序设计【三】之 类和对象进阶
第四章:运算符重载的概念
一、重载运算符的概念
1.可重载的运算符
2.不可重载的运算符和符号
二、重载运算符为类的成员函数
#include<iostream>
using namespace std;
class myComplex {
// 复数类
private:
double real, imag;
public:
myComplex(); // 构造函数
myComplex(double r, double i); // 构造函数
void outCom();
myComplex operator-(const myComplex &c); // 成员函数
friend myComplex operator+(const myComplex &c1, const myComplex &c2);
};
myComplex::myComplex() {
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i) {
real = r;
imag = i;
}
void myComplex::outCom() {
cout << "(" << real << "," << imag << ")";
}
myComplex myComplex::operator-(const myComplex &c) {
return myComplex(this -> real - c.real, this -> imag - c.imag);
}
myComplex operator+(const myComplex &c1, const myComplex &c2) {
return myComplex(c1.real + c2.real, c1.imag + c2.imag);
}
int main() {
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom();
cout << "operator+";
c2.outCom();
cout << "=";
res = c1 + c2;
res.outCom();
cout << endl;
c1.outCom();
cout << "operator-";
c2.outCom();
cout << "=";
res = c1 - c2;
res.outCom();
cout << endl;
return 0;
}
// (1, 2)operator+(3, 4) = (4, 6)
// (1, 2)operator-(3, 4) = (-2, -2)
重载赋值运算符
#include<iostream>
using namespace std;
class myComplex {
// 复数类
private:
double real, imag;
public:
myComplex(); // 构造函数
myComplex(double r, double i); // 构造函数
void outCom(const char string1[22]);
myComplex operator-(const myComplex &c); // 成员函数
myComplex operator=(const myComplex &c); // 成员函数
myComplex operator=(const double r); // 成员函数
friend myComplex operator+(const myComplex &c1, const myComplex &c2);
friend myComplex operator+(const myComplex &c1, double r);
friend myComplex operator+(double r, const myComplex &c1);
};
myComplex::myComplex() {
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i) {
real = r;
imag = i;
}
void myComplex::outCom(const char string1[22]) {
cout << string1 << "(" << real << "," << imag << ")\n";
}
myComplex myComplex::operator-(const myComplex &c) {
return myComplex(this -> real - c.real, this -> imag - c.imag);
}
myComplex operator+(const myComplex &c1, const myComplex &c2) {
// c1 + c2
return myComplex(c1.real + c2.real, c1.imag + c2.imag); // 返回一个临时对象
}
myComplex operator+(const myComplex &c1, double r) {
// c1 + double
return myComplex(c1.real + r, c1.imag); // 返回一个临时对象
}
myComplex operator+(double r, const myComplex &c1) {
// double + c1
return myComplex(r + c1.real, c1.imag); // 返回一个临时对象
}
myComplex myComplex::operator=(const myComplex& c1) {
this -> real = c1.real;
this -> imag = c1.imag;
return *this;
}
myComplex myComplex::operator=(double r) {
this -> real = r;
this -> imag = 0;
return *this;
}
int main() {
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom("\t\t\tC1");
c2.outCom("\t\t\tC2");
res = c1 + c2;
res.outCom("执行res = c1 + c2 ->\tres");
res = c1 + 5;
res.outCom("执行res = c1 + 5 ->\tres");
res = 5 + c1;
res.outCom("执行res = 5 + c1 ->\tres");
res = c1;
c1.outCom("\t\t\tC1");
res.outCom("执行res = c1 ->\tres");
// c1.changeReal(-3);
// c1.outCom("执行c1.changeReal(-3) -> \tc1");
res.outCom("\t\t\tres");
res = c1;
res.outCom("执行res = c1 -> \tres");
res = 7;
res.outCom("执行res = 7 -> \tres");
res = 7 + 8;
res.outCom("执行res = (7+8) -> \tres");
res = c1 = c2;
c1.outCom("\t\t\tc1");
c2.outCom("\t\t\tc2");
res.outCom("执行res = c1 = c2 -> \tres");
return 0;
}
浅拷贝和深拷贝
// 浅拷贝例子
#include<iostream>
using namespace std;
class pointer {
public:
int a;
int *p;
pointer() {
a = 100;
p = new int(10);
}
pointer(const pointer &tempp) {
if (this != &tempp) {
a = tempp.a;
p = tempp.p;
}
}
};
int main() {
pointer p1;
pointer p2(p1);
pointer p3 = p1;
cout << "\n初始化后,各对象的值及内存地址" << endl;
cout << "对象名\t对象地址 a的值 p中的值 p指向的值 p的地址" << endl;
cout << "p1:\t" << &p1 << ", " << p1.a << ", " << p1.p << ", " << *p1.p << ", " << &p1.p << endl;
cout << "p2:\t" << &p2 << ", " << p2.a << ", " << p2.p << ", " << *p2.p << ", " << &p2.p << endl;
cout << "p3:\t" << &p3 << ", " << p3.a << ", " << p3.p << ", " << *p3.p << ", " << &p3.p << endl;
*p1.p = 20;
p2.a = 300;
cout << "\n修改后,各对象的值及内存地址" << endl;
cout << "对象名\t对象地址 a的值 p中的值 p指向的值 p的地址" << endl;
cout << "p1:\t" << &p1 << ", " << p1.a << ", " << p1.p << ", " << *p1.p << ", " << &p1.p << endl;
cout << "p2:\t" << &p2 << ", " << p2.a << ", " << p2.p << ", " << *p2.p << ", " << &p2.p << endl;
cout << "p3:\t" << &p3 << ", " << p3.a << ", " << p3.p << ", " << *p3.p << ", " << &p3.p << endl;
return 0;
}
三、重载运算符为友元函数
#include<iostream>
using namespace std;
class myComplex {
// 复数类
private:
double real, imag;
public:
myComplex(); // 构造函数
myComplex(double r, double i); // 构造函数
void outCom();
friend myComplex operator+(const myComplex &c1, const myComplex &c2);
friend myComplex operator-(const myComplex &c1, const myComplex &c2);
friend myComplex operator-(double r, const myComplex &c2);
friend myComplex operator-(const myComplex &c1, double r);
};
myComplex::myComplex() {
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i) {
real = r;
imag = i;
}
void myComplex::outCom() {
cout << "(" << real << "," << imag << ")";
}
myComplex operator+(const myComplex &c1, const myComplex &c2) {
// c1 + c2
return myComplex(c1.real + c2.real, c1.imag + c2.imag); // 返回一个临时对象
}
myComplex operator-(const myComplex &c1, const myComplex &c2) {
// c1 - c2 新增
return myComplex(c1.real - c2.real, c1.imag - c2.imag); // 返回一个临时对象
}
myComplex operator-(const myComplex &c1, double r) {
//c1 - r 新增
return myComplex(c1.real - r, c1.imag); // 返回一个临时对象
}
myComplex operator-(double r, const myComplex &c1) {
// r - c1新增
return myComplex(r - c1.real, r -c1.imag); // 返回一个临时对象
}
int main() {
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom();
cout << "operator+";
c2.outCom();
cout << "=";
res = c1 + c2;
res.outCom();
cout << endl;
res = c1 - 5;
res.outCom();
res = 5 - c1;
res.outCom();
return 0;
}
// (1,2)operator+(3,4)=(4,6)
// (-4,2)(4,3)
重载流插入运算符和提取运算符
使用友元函数重载运算符 << 和 >>
作用:直接输出class的话会报错,所以我们对其进行重载可以解决这种问题。
#include <iostream>
using namespace std;
class test {
private:
int i;
float f;
char ch;
public:
test(int a = 0, float b = 0, char c = '\0') {
i = a; f = b; ch = c;}
friend ostream &operator<<(ostream &, test);
friend istream &operator>>(istream &, test &);
};
ostream &operator<<(ostream & stream, test obj) {
stream << obj.i << ","; // stream是cout的别名
stream << obj.f << ",";
stream << obj.ch << endl;
return stream;
};
istream &operator>>(istream & t_stream, test&obj) {
t_stream >> obj.i; // t_stream是cin的别名
t_stream >> obj.f;
t_stream >> obj.ch;
return t_stream;
}
int main() {
test A(45, 8.5, 'W');
operator<<(cout, A);
test B;
cout << "Input as i f ch:";
operator>>(cin, B);
operator<<(cout, B);
return 0;
}
重载强制类型转换运算符
#include <iostream>
using namespace std;
class myComplex {
private:
double real, imag;
public:
myComplex(double r = 0, double i = 0) : real(r), imag(i) {
};
operator double () {
// 重载强制类型转换运算符double
return real;
}
};
int main() {
myComplex c(1.2, -3.4);
cout << double(c) << endl; // 输出1.2
double n = 12 + c; // 相当于double n = 12 + double(c)
cout << n;
return 0;
}
重载自增、自减运算符
#include<iostream>
using namespace std;
class CDemo {
private:
int n;
public:
CDemo(int i = 0):n(i){
}
CDemo & operator++(); // 用于前置形式
CDemo operator++(int); // 用于后置形式
operator int() {
return n;}
friend CDemo & operator--(CDemo &);
friend CDemo operator--(CDemo &, int);
};
CDemo & CDemo::operator++() {
// 前置++
n++;
return *this;
}
CDemo CDemo::operator++(int k) {
// 后置++,多一个参数
CDemo tmp(*this); // 记录修改前的对象
n++;
return tmp; // 返回修改前的对象
}
CDemo & operator--(CDemo & d) {
// 前置--
d.n--;
return d;
}
CDemo operator--(CDemo & d, int) {
// 后置--
CDemo tmp(d);
d.n--;
return tmp;
}
int main() {
CDemo d(10);
cout << (d++) << ","; // 等价于d.operator++(0)
cout << d << ",";
cout << (++d) << ",";// 等价于d.operator++()
cout << d << ",";
cout << (d--) << ",";// 等价于d.operator--(0)
cout << d << ",";
cout << (--d) << ",";// 等价于d.operator--()
cout << d << endl;
return 0;
}
// 10,11,12,12,12,11,10,10
四、重载运算符的规则
转载:https://blog.csdn.net/weixin_43606158/article/details/111997160
查看评论