网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
#include <iostream>
using namespace std;
class Complex //复数类
{
//私有数据
private:
double real;//实数
double imag;//虚数
public:
Complex(double real=0,double imag=0)
{
this->real=real;
this->imag=imag;
}
//友元函数重载双目运算符
friend Complex operator+(Complex com1,Complex com2);
void showSum();
};
Complex operator+(Complex com1,Complex com2)//运算符重载函数
{
return Complex(com1.real+com2.real,com1.imag+com2.imag);
}
void Complex::showSum()
{
if(imag>=0)
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
else
{
cout<<real<<imag<<"i"<<endl;
}
}
int main()
{
Complex com1(10,10),com2(20,20),sum;
sum=operator+(com1,com2);//或 sum=com1+com2
sum.showSum();
return 0;
}
2) 友元函数重载单目运算符(++):
#include <iostream>
using namespace std;
class Point//坐标类
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
friend void operator++(Point& point);//友元函数重载单目运算符++
void showPoint();
};
void operator++(Point& point)//友元运算符重载函数
{
++point.x;
++point.y;
}
void Point::showPoint()
{
std::cout<<"("<<x<<","<<y<<")"<<std::endl;
}
int main()
{
Point point(10,10);
++point;//或operator++(point)
point.showPoint();//输出坐标值
return 0;
}
输出
(11,11)
2.2 运算符重载函数作为类的成员函数的形式:
对于成员函数重载运算符而言,双目运算符的参数表中仅有一个参数,而单目则无参数。同样的是重载,为什么和友元函数在参数的个数上会有所区别的。原因在于友元函数,没有this指针。
class 类名
{
返回类型 operator 运算符(形参表);
}
类外定义格式:
返回类型 类名:: operator 运算符(形参表)
{
函数体;
}
1) 成员函数重载双目运算符(+):
#include <iostream>
using namespace std;
class Complex //复数类
{
//私有数据
private:
double real;//实数
double imag;//虚数
public:
Complex(double real=0,double imag=0)
{
this->real=real;
this->imag=imag;
}
//成员函数重载双目运算符
Complex operator+(Complex com1);
void showSum();
};
Complex Complex::operator+(Complex com1)
{
return Complex(real+com1.real,imag+com1.imag);
}
void Complex::showSum()
{
if(imag>=0)
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
else
{
cout<<real<<imag<<"i"<<endl;
}
}
int main()
{
Complex com1(10,10),com2(200,200),sum;
sum=com1+com2;//或 sum=com1.operator+(com2);
sum.showSum();
return 0;
}
2)成员函数重载单目运算符(++)
#include <iostream>
using namespace std;
class Point//坐标类
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
void operator++();//成员函数重载单目运算符++
void showPoint();
};
void Point::operator++()//成员运算符重载函数
{
++x;
++y;
}
void Point::showPoint()
{
std::cout<<"["<<x<<","<<y<<"]"<<std::endl;
}
int main()
{
Point point(10,10);
++point;//或operator++(point)
point.showPoint();//输出坐标值
return 0;
}
输出
[11,11]
三、重载输出运算符<<
#include<iostream>
using namespace std;
class People{
public:
People(int id,int pwd):m\_id(id),m\_pwd(pwd){}
~People(){}
friend ostream& operator<< (ostream &os,People p);
private:
int m_id;
int m_pwd;
};
ostream& operator << (ostream &os, const People p){
os<<"your id is:"<<p.m_id<<" and password is:"<<p.m_pwd<<endl;
return os;
}
int main() {
People a(1,123);


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.youkuaiyun.com/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
63)]
[外链图片转存中...(img-XoR7l7ne-1715811417164)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.youkuaiyun.com/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**