正常情况下,+ -只支持基础数据类型。但是对象类型不支持的,但可以通过运算重载赋予它新的意义
运算符重载有两种实现方式:
成员运算符重载
友元函数重载
1.+的友元函数运算符重载
#include <iostream>
using namespace std;
class Rice{
private:
int production;//产量
int defense; //抗性
public:
Rice(int p,int d){
production=p;
defense=d;
}
void show(){
cout<<"产量是"<<production<<" "<<"抗性是"<<defense<<endl;
}
friend Rice operator +(Rice & r1,Rice & r2);
};
Rice operator +(Rice & r1,Rice & r2){
int p=(r1.production+r2.production)/2;
int d=(r1.defense+r2.defense)/2;
Rice r3(p,d);
return r3;
}
int main()
{
Rice r1(1000,50);
Rice r2(500,90);
Rice r3=r1+r2; //相当于r3=operator+(r1,r2);
r3.show();
}
2.+的成员函数重载
#include <iostream>
using namespace std;
class Rice{
private:
int production;//产量
int defense; //抗性
public:
Rice(int p,int d){
production=p;
defense=d;
}
void show(){
cout<<"产量是"<<production<<" "<<"抗性是"<<defense<<endl;
}
//成员运算符重载是类中的成员。所以隐含一个this指针,指代当前对象
//哪个对象调用的这个函数,this指针就指向谁
Rice operator +(Rice& r2){
int p=(this->production+r2.production)/2;
int d=(this->defense+r2.defense)/2;
Rice r3(p,d);
return r3;
}
};
int main()
{
Rice r1(1000,50);
Rice r2(500,90);
Rice r3=r1+r2; //相当于 r3=r1.operator+(r2)
r3.show();
}
可以被重载的运算符:
算术运算符:+、-、*、/、%、++、--
位操作运算符:&、|、~、^(位异或)、<<(左移)、>>(右移)
逻辑运算符:!、&&、||
比较运算符:<、>、>=、<=、==、!=
赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=
其他运算符:[]、()、->、,、new、delete、new[]、delete[]
不被重载的运算符:
成员运算符 .、指针运算符 *、三目运算符 ? :、sizeof、作用域 ::
3.前置++和后置++成员函数重载
#include <iostream>
using namespace std;
class Integer{
private:
int value;
public:
Integer(int v):value(v){};
void getValue(){
cout<<value<<endl;
}
//成员函数重载 前置++
Integer operator ++(){
//先加后返回
return ++this->value; //隐式调用构造函数 //return ++(整数变量)
}
//成员函数重载 后置++ 需要int占位
Integer operator ++(int){
return this->value++; //ruturn (整数类型变量)++
}
};
int main()
{
Integer i(10);
i.getValue();
//对象类型前置++
(++i).getValue(); //11
(i++).getValue(); //11
i.getValue(); //12
}
4.前置++和后置++友元函数重载实现
#include <iostream>
using namespace std;
class Integer{
private:
int value;
public:
Integer(int v):value(v){};
void getValue(){
cout<<value<<endl;
}
//友元函数重载 前置++
friend Integer operator ++(Integer & i);
//友元函数重载 后置++ 需要int占位
friend Integer operator ++(Integer & i,int);
};
Integer operator ++(Integer & i){
return ++i.value;
}
Integer operator ++(Integer & i,int){
return i.value++;
}
int main()
{
Integer i(10);
i.getValue();
//对象类型前置++
(++i).getValue(); //11
(i++).getValue(); //11
i.getValue(); //12
}
5.赋值运算符重载
作用:是用一个对象的值改变另一个对象的值
格式要求:T & operator=(const T & other) //T是代表任意类型
特点:赋值运算符属于类内函数,它只支持成员函数重载
如果不给出赋值运算符重载。编译器会有个默认的赋值函数,但是当类的属性有指针的时候,是需要给出赋值运算符重载,因为会产生浅拷贝问题
#include <iostream>
using namespace std;
class Integer{
private:
int value;
public:
Integer(int v):value(v){};
void getValue(){
cout<<value<<endl;
}
//T & operator=(const T & other)
Integer & operator=(const Integer & other){ //类内成员函数
cout<<"赋值运算符重载函数调用"<<endl;
this->value=other.value;
return *this;
}
};
int main()
{
Integer i(10);
Integer i2(20);
i2=i; //调用了写的赋值运算符重载
i2.getValue();
}
6.类型转换运算符重载
自定义的对象类型自动的转成任意类型,此函数也只支持成员函数重载。
#include <iostream>
#include <string.h>
using namespace std;
class Student{
private:
string name;
int age;
public:
Student(string n,int a):name(n),age(a){}
operator string(){
return name;
}
operator int(){
return age;
}
};
int main()
{
Student s1("张三",18);
string s=s1;
cout<<s<<endl;
int age=s1;
cout<<age<<endl;
}
3713

被折叠的 条评论
为什么被折叠?



