010_linuxC++之_运算符重载
2024-09-05 07:30:15
(一)运算符重载:运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
(二)实现类不同对象里中变量的相加
(三)程序
#include <iostream>
#include <string.h>
#include <unistd.h> using namespace std; class Point {
private:
int x;
int y; public:
Point() {}
Point(int x, int y) : x(x), y(y) {} void printInfo()
{
cout<<"("<<x<<", "<<y<<")"<<endl;
}
friend Point operator+(Point &p1, Point &p2);
}; Point operator+(Point &p1, Point &p2)
{
cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
} int main(int argc, char **argv)
{
Point p1(, );
Point p2(, ); Point sum = p1+p2;
sum.printInfo(); return ;
}
point.cpp
(四)运行结果
最新文章
- gradle学习笔记(1)
- SSH实战 &#183; SSH项目开发环境搭建
- Docker化运维方式讲解
- Linux服务器之间的目录共享
- Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role
- java中如何实现类似goto的作法
- linux命令(11)下查看和添加环境变量
- Nodejs文件服务器
- Android 调用系统的分享[完美实现同一时候分享图片和文字]
- (十二)boost库之多线程高级特性
- LeetCode总结 -- 高精度篇
- iOS状态变更
- HTML学习笔记3:文字和段落
- NVIDIA GRID 和 NICE DCV 技术用于实现 Linux 和 Windows&#174; 图形加速虚拟桌面
- 设计模式-原型(prototype)
- 百道Python入门级练习题(新手友好)第一回合——矩阵乘法
- 使用vuex保存singer每个歌星的基本信息
- Cannot find entry file index.android.js in any of the roots:[ Android ]
- 沉淀再出发:ElasticSearch的中文分词器ik
- ISP (互联网服务提供商)