在某些特殊的运算情况下,以正常的运算方法不匹配时,就可以使用运算符重载;
注意:当重载运算符一样的时候,类内作为友元的只放一个就可以了!!!
头文件:
#include <string>
class COMPUTER{
public:
COMPUTER(int val=0);
std::string description();
private:
int val;
};
实现cpp文件:
#include "COMPUTER.h"
#include <sstream>
COMPUTER::COMPUTER(int val){
this->val = val;
}
std::string COMPUTER::description() {
std::stringstream ret;
ret << val << "台电脑";
return ret.str();
}
头文件:
class COMPUTER;
class GAMES{
public:
GAMES(int val=0);
//注意operator这个数据后面接的是什么运算符,在调用时就是什么运算符
//如:operator*;那么在主函数中调用肯定是T1*T2
//operator本身就是一个对象,在那个类里,就是那个类的对象;
COMPUTER operator*();//使用成员函数实现运算符重载,这是单目运算,也可以加参数
friend COMPUTER operator+(GAMES& T1, GAMES& T3);//使用友元实现运算符重载
private:
int val;
};
实现cpp文件:
#include "GAMES.h"
#include "COMPUTER.h"
GAMES::GAMES(int val) {
this->val = val;
}
COMPUTER GAMES::operator*() {
// 为了区分运算符, operator后面是什么运算符实现的时候最好一致;
int tmp = this->val / 3;
return COMPUTER(tmp);
}
测试函数:
/*
运算符重载
假设,一台电脑可以换3台游戏机
*/
#include "COMPUTER.h"
#include "GAMES.h"
#include <iostream>
using namespace std;
//使用友元,实现运算符重载;
COMPUTER operator+(GAMES& T1,GAMES &T3) {
int tmp = (T1.val+T3.val) / 3;
return COMPUTER(tmp);
}
int main(void) {
GAMES T1(10);
GAMES T2(5);
//友元实现运算符重载:
COMPUTER K1 = T1 + T2;
cout << K1.description() << endl;
//成员函数实现运算符重载:
COMPUTER K = T2.operator*();
cout << K.description() << endl;
system("pause");
return 0;
}
运行结果:
多种运算符的重载应用:
/*
实现赋值运算符 =
比较运算符 < > ==
输入,输出运算符 << >>
下标运算符 []
*/
#include "BOY.h"
//使用全局函数可以指定格式;使程序代码美观
ostream& operator<<(ostream& os,BOY&boy) {
os << "名字:" << boy.name << "\t年龄:" << boy.age << "\t薪资:" << boy.salary << endl;
return os;
}
istream& operator>>(istream& is,BOY&boy) {
cout << "输入名字:";
is >> boy.name;
cout << "输入年龄:";
is >> boy.age;
cout << "输入薪资:";
is >> boy.salary;
return is;
}
int main(void) {
BOY boy1("小龙", 28, 12000);
BOY boy2("老王", 35, 16000);
BOY boy3, boy4;
//测试输出/输入运算符功能
cout << boy1 << endl<< boy2 << endl;
cin >> boy3 >> boy4;
cout << boy3 << endl << boy4 << endl;
//测试比较运算符功能
if (boy1 > boy2) {
cout << "选择boy1" << endl;
}
else if (boy1 == boy2) {
cout << "不分伯仲,难以选择" << endl;
}
else {
cout << "选择boy2" << endl;
}
//测试下标运算符功能:枚举类型
cout <<"名字:"<<boy1.getName()<< ",年龄:" << boy1[AGE] << ",薪资:" << boy1[SALARY]
<< ",系数:" << boy1[POWER] << endl;
//测试下标法运算符功能:宏定义类型
cout << "名字:"<<boy2.getName()<<",年龄:" << boy2[BOY_AGE] << ",薪资:" << boy2[BOY_SALARY]
<< ",系数:" << boy2[BOY_POWER] << endl;
system("pause");
return 0;
}
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
#define BOY_AGE "age"
#define BOY_SALARY "salary"
#define BOY_POWER "power"
typedef enum{
AGE,
SALARY,
POWER
}BOY_KEY;
class BOY{
public:
BOY();//默认构造函数
BOY(string, int, int);//构造函数
~BOY(){}//析构函数
string getName() { return name; }
BOY& operator=(BOY& boy);//赋值函数
bool operator>(BOY& boy);//大于,
bool operator<(BOY& boy);//小于,
bool operator==(BOY& boy);//等于,
friend ostream& operator<<(ostream& os, BOY& boy);//输出:使用友元可以让格式更加美观
friend istream& operator>>(istream& is, BOY& boy);//输入:解释同上
//运算符下标法可以用enum(枚举) 和#define(宏定义)来实现
int operator[](int number);//枚举实现
int operator[](string index);//宏定义实现
private:
string name;
int age;
int salary;
int power();
};
#include "BOY.h"
BOY::BOY(){
this->name = "无名";
this->age = 0;
this->salary = 0;
}
BOY::BOY(string name, int age, int salary){
this->name = name;
this->age = age;
this->salary = salary;
}
BOY& BOY::operator=(BOY& boy){
this->name = boy.name;
this->age = boy.age;
this->salary = boy.salary;
return boy;
}
bool BOY::operator>(BOY& boy){
if (power() > boy.power()) {
return true;
}
else {
return false;
}
}
bool BOY::operator<(BOY& boy){
if (power() < boy.power()) {
return true;
}
else {
return false;
}
}
bool BOY::operator==(BOY& boy){
if (power() == boy.power()) {
return true;
}
else {
return false;
}
}
int BOY::operator[](int number){
switch (number) {
case AGE:
return age;
case SALARY:
return salary;
case POWER:
return power();
default:
return -1;
}
}
int BOY::operator[](string index){
if (index == BOY_AGE) {
return age;
}
else if (index == BOY_SALARY) {
return salary;
}
else if (index == BOY_POWER) {
return power();
}
else {
return 1;
}
}
int BOY::power(){
int tmp = salary - (age * 100);
if (tmp < 0) {
cout << "返回值错误!" << endl;
return -1;
}
return tmp;
}
运行结果: