尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

类和对象-C++运算符重载project7

类和对象-C++运算符重载project7
📅 发布时间:2026/6/19 6:00:33

运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

加号运算符重载

作用:实现两个自定义数据类型相加的运算
成员函数重载或全局函数重载,不要同时使用

include

using namespace std;
//加号运算符重载
class Person {
//成员函数重载+号
public:
/Person operator+(Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
/
public:
int m_A;
int m_B;
};
//void test01() {
// Person p1;
// p1.m_A = 10;
// p1.m_B = 10;
// Person p2;
// p2.m_A = 10;
// p2.m_B = 10;
// //Person p3 = p1.operator+ (p2);//成员函数本质调用
// Person p3 = p1 + p2;
// cout << p3.m_A<<endl;
// cout << p3.m_B << endl;
//}
//全局函数重载+号
Person operator+(Person & p1, Person & p2) {
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//运算符重载 也可以发生函数重载
Person operator+(Person& p1,int num) {
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test02() {
Person p4;
p4.m_A = 20;
p4.m_B = 20;
Person p5;
p5.m_A = 20;
p5.m_B = 20;
//全局函数本质调用 Person p6 = operate+(p1,p2)
Person p6 = p5 + p4;
cout << p6.m_A << endl;
cout << p6.m_B << endl;
Person p7 = p4 + 10;
cout << p7.m_A << endl;
}
int main() {
//test01();
test02();
system("pause");
return 0;
}

左移运算符重载project7 filename02

作用 可以输出自定义类型
只能够在全局函数实现重载

include

using namespace std;
//左移运算符重载
class Person {
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a,int b){
m_A = a;
m_B = b;
}
private:
int m_A;
int m_B;
};
ostream & operator<<(ostream& cout, Person& p) {//本质operator<<(cout,p) cout<<p
cout << p.m_A << p.m_B << endl;
return cout;
}
void test03() {
Person p(10, 10);
cout << p << endl;
}
int main() {
test03();
system("pause");
return 0;
}

递增运算符重载project7 filename03

前置递增返回的引用 后置递增返回值

include

using namespace std;

//自定义整型
class MyInteger {
// 修改为const引用,可以接受临时对象
friend ostream& operator<<(ostream& cout, const MyInteger& myint);
public:
MyInteger() {
m_Num = 0;
}

//重载前置递增
MyInteger& operator++() {m_Num++;return *this;
}//重载后置递增
MyInteger operator++(int) {MyInteger temp = *this; // 记录当前值m_Num++;                // 自身递增return temp;            // 返回旧值
}

private:
int m_Num;
};

// 修改为const引用
ostream& operator<<(ostream& cout, const MyInteger& myint) {
cout << myint.m_Num;
return cout;
}

void test04() {
MyInteger myint;
cout << "初始值: " << myint << endl;

// 测试前置递增
cout << "前置递增: " << ++(++myint) << endl;
cout << "递增后值: " << myint << endl;// 测试后置递增
cout << "后置递增: " << myint++ << endl;
cout << "递增后值: " << myint << endl;

}

int main() {
test04();
system("pause");
return 0;
}

赋值运算符重载project7 filename04

C++编译器至少给一个类添加四个函数
1,默认构造函数(无参,函数体为空)
2,默认析构函数(无参,函数体为空)
3,默认拷贝构造函数,对属性进行值拷贝
4,赋值运算符operator=,对属性进行值拷贝

include

using namespace std;
class Person {
public:
Person(int age) {
m_Age = new int(age);
}
//赋值运算符重载
Person& operator=(Person& p) {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age
//提供深拷贝,解决浅拷贝的问题
m_Age = new int(*p.m_Age);
return this;
}
~Person()
{
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
public:
int
m_Age;
};
void test05() {
Person p1(18);
Person p2(20);
Person p3(25);
p3=p2 = p1;
cout << *p1.m_Age << endl;
cout << *p2.m_Age << endl;
cout << *p3.m_Age << endl;
}

int main04() {
test05();
system("pause");
return 0;
}

关系运算符重载project7 filename05

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

include

using namespace std;
class Person {
public:
Person(string name,int age) {
this->m_Age = age;
this->m_Name = name;
}
bool operator==(Person& p) {
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
return true;
}
else {
return false;
}
}
public:
string m_Name;
int m_Age;
};
void test06() {
Person p1("james", 18);
Person p2("james", 18);
if (p1 == p2) {
cout << "相等" << endl;
}
else {
cout << "不相等" << endl;
}
}

int main() {
test06();
system("pause");
return 0;
}

函数调用运算符重载project7 filename06

函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法 非常灵活

include

using namespace std;
//函数调用运算符重载
//打印输出类
class MyPrint {
public:
void operator()(string text) {
cout << text << endl;
}
int operator()(int num1, int num2) {
return num1 + num2;
}
};
void test07() {
MyPrint myFunc;
myFunc("hello");
int ret = myFunc(12, 12);
cout << ret << endl;
//匿名函数对象
cout << MyPrint()(12,12) << endl;
}

int main() {
test07();
system("pause");
return 0;
}

相关新闻

  • 2025 年 11 月酒店加盟公司最新推荐,品牌资质与运营韧性深度解析!
  • faust-一个简单的单选下拉菜单代码,用于切换波形。
  • [GESP202306 二级] 找素数

最新新闻

  • Onekey完整教程:一键解锁Steam游戏DLC的终极方案
  • 2026年南京知名3D效果图制作公司大盘点,你知道几家?
  • S12 MSCAN与SCI模块深度解析:低功耗、中断与安全初始化实战
  • MPV播放器懒人包:3分钟打造专业级视频播放体验
  • 2026年6月经验丰富的升降货梯生产公司哪家便宜,导轨式货梯升降机/厂房升降货梯/四柱液压货梯,升降货梯工厂平价推荐 - 品牌推荐师
  • 4.19周总结

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号