运算符重载
方式一, 使用成员函数重载运算符
- 需求:把牛肉换猪肉, 羊肉换猪肉
规则: 一斤牛肉:2斤猪肉 一斤羊肉:3斤猪肉
实现: 牛 + 牛 = ?猪肉 牛 + 羊 = ?猪肉
? Cow类
> Cow.h
#pragma once
class Pork;
class Sheep;
class Cow{
public:
Cow(int weight = 0);
Pork operator+(const Cow& cow);
Pork operator+(const Sheep& sheep);
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Cow.cpp
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
Cow::Cow(int weight){
this->weight = weight;
}
Pork Cow::operator+(const Cow& cow){
return Pork((this->weight + cow.weight) * 2);
}
Pork Cow::operator+(const Sheep& sheep){
int tmp = (this->weight * 2) + (sheep.getWeight() * 3);
return Pork(tmp);
}
? Sheep类
> Sheep.h
#pragma once
class Sheep{
public:
Sheep(int weight = 0);
int getWeight() const;
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Sheep.cpp
#include "Sheep.h"
Sheep::Sheep(int weight){
this->weight = weight;
}
int Sheep::getWeight() const{
return weight;
}
?
Pork类
> Pork.h
#pragma once
#include <string>
using namespace std;
class Pork{
public:
Pork(int weight = 0);
string description() const;
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Pork.cpp
#include <sstream>
#include "Pork.h"
Pork::Pork(int weight){
this->weight = weight;
}
string Pork::description() const{
stringstream ret;
ret << this->weight << "斤";
return ret.str();
}
?
main.cpp
#include <iostream>
#include <Windows.h>
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
using namespace std;
int main(void) {
Pork p1;
Cow c1(100);
Cow c2(200);
Sheep s1(100);
p1 = c1 + c2;
cout << "牛 + 牛 = 猪肉:" << p1.description() << endl;
p1 = c1 + s1;
cout << "牛 + 羊 = 猪肉:" << p1.description() << endl;
system("pause");
return 0;
}
? ? ?
方式二, 使用非成员函数【友元函数】重载运算符
实现: 牛 + 牛 = ?猪肉 牛 + 羊 = ?猪肉
? Cow类
> Cow.h
#pragma once
class Pork;
class Sheep;
class Cow{
public:
Cow(int weight = 0);
friend Pork operator+(const Cow& c1, const Cow& c2);
friend Pork operator+(const Cow& c1, const Sheep& s1);
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Cow.cpp
#include "Cow.h"
Cow::Cow(int weight){
this->weight = weight;
}
? Sheep类
> Sheep.h
#pragma once
class Sheep{
public:
Sheep(int weight = 0);
int getWeight() const;
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Sheep.cpp
#include "Sheep.h"
Sheep::Sheep(int weight){
this->weight = weight;
}
int Sheep::getWeight() const{
return weight;
}
?
Pork类
> Pork.h
#pragma once
#include <string>
using namespace std;
class Pork{
public:
Pork(int weight = 0);
string description() const;
private:
int weight;
};
_________________________________________________________________________________________________________________________________
> Pork.cpp
#include <sstream>
#include "Pork.h"
Pork::Pork(int weight){
this->weight = weight;
}
string Pork::description() const{
stringstream ret;
ret << this->weight << "斤";
return ret.str();
}
?
main.cpp
#include <iostream>
#include <Windows.h>
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
using namespace std;
Pork operator+(const Cow& c1, const Cow& c2) {
return ((c1.weight + c2.weight) * 2);
}
Pork operator+(const Cow& c1, const Sheep& s1) {
return((c1.weight * 2) + (s1.getWeight() * 3));
}
int main(void) {
Pork p1;
Cow c1(100);
Cow c2(200);
Sheep s1(100);
p1 = c1 + c2;
cout << "使用友元 牛 + 牛 = 猪肉:" << p1.description() << endl;
p1 = c1 + s1;
cout << "使用友元 牛 + 羊 = 猪肉:" << p1.description() << endl;
system("pause");
return 0;
}
? ? ?
两种方式的区别
区别:
- 使用成员函数来实现运算符重载时,少写一个参数,因为第一个参数就是this指针。
两种方式的选择:
- 一般情况下,单目运算符重载,使用成员函数进行重载更方便(不用写参数)
- 一般情况下,双目运算符重载,使用友元函数更直观
方便实现a+b和b+a相同的效果,成员函数方式无法实现。 例如: 100 + cow; 只能通过友元函数来实现 cow +100; 友元函数和成员函数都可以实现 - 特殊情况:
(1)= () [ ] -> 不能重载为类的友元函数!!!(否则可能和C++的其他规则矛盾),只能使用成员函数形式进行重载。 (2)如果运算符的第一个操作数要求使用隐式类型转换,则必须为友元函数(成员函数方式的第一个参数是this指针)
注意: 同一个运算符重载, 不能同时使用两种方式来重载,会导致编译器不知道选择哪一个(二义性)
|