运算符重载
来源:互联网 发布:石头纸 知乎 编辑:程序博客网 时间:2024/04/30 05:37
#include <iostream>
using namespace std;
{
public:
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
};
Complex ComAdd(Complex &c1, Complex &c2)
{
Complex tmp;
tmp.a = c1.a + c2.a;
tmp.b = c1.b + c2.b;
return tmp;
}
Complex operator+(Complex &c1, Complex &c2)
{
Complex tmp;
tmp.a = c1.a + c2.a;
tmp.b = c1.b + c2.b;
return tmp;
}
void main()
{
Complex c1(1, 2), c2(3, 4), c4;
c4 = ComAdd(c1, c2);
c4.printCom();
Complex c3 = c1 + c2;
c3.printCom();
system("pause");
using namespace std;
//a+bi
{
public:
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
};
Complex ComAdd(Complex &c1, Complex &c2)
{
Complex tmp;
tmp.a = c1.a + c2.a;
tmp.b = c1.b + c2.b;
return tmp;
}
Complex operator+(Complex &c1, Complex &c2)
{
Complex tmp;
tmp.a = c1.a + c2.a;
tmp.b = c1.b + c2.b;
return tmp;
}
void main()
{
Complex c1(1, 2), c2(3, 4), c4;
c4 = ComAdd(c1, c2);
c4.printCom();
Complex c3 = c1 + c2;
c3.printCom();
system("pause");
}
运行结果:
0 0
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- IntelliJ IDEA 设置代码提示或自动补全的快捷键
- Find Leaves of Binary Tree
- 字符串 KMP HDU 1686
- 需要认识的c math 函数
- PHP假期复习(四)---web技术
- 运算符重载
- hdu 5818 Joint Stacks 栈
- Error22error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning
- POJ2002Squares(哈希)
- Effective Objective-C 2.0 第15条:用前缀避免命名空间冲突
- 数据结构上机实验之顺序查找
- PHP假期复习(五)---AJAX
- ssh2框架程序运行流程
- 数据结构实验之求二叉树后序遍历和层次遍历