C++运算符重载
来源:互联网 发布:唐嫣香港知乎 编辑:程序博客网 时间:2024/06/10 01:15
#include "stdafx.h"#include<iostream>class Complex{public:Complex();Complex(double r, double i);Complex C_add(Complex &d);void print();private:double real;double imag;};Complex::Complex(){real = 0;imag = 0;}Complex::Complex(double r, double i){real = r;imag = i;}Complex Complex::C_add(Complex &d){Complex c;c.real = real + d.real;c.imag = imag + d.imag;return c;}void Complex::print(){std::cout<< "(" << real << "," << imag << "i)\n";}int main(){Complex c1(3, 4), c2(5, -10), c3;c3 = c1.C_add(c2);c1.print();c2.print();c3.print();return 0;}
重载
#include "stdafx.h"#include<iostream>class Complex{public:Complex();Complex(double r, double i);Complex operator + (Complex &d);void print();private:double real;double imag;};Complex::Complex(){real = 0;imag = 0;}Complex::Complex(double r, double i){real = r;imag = i;}Complex Complex::operator+(Complex &d){Complex c;c.real = real + d.real;c.imag = imag + d.imag;return c;}void Complex::print(){std::cout<< "(" << real << "," << imag << "i)\n";}int main(){Complex c1(3, 4), c2(5, -10), c3(5,100),c4;c4 = c1+c2+c3;c1.print();c2.print();c4.print();return 0;}
阅读全文
1 0
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- C++:运算符重载
- ButterKnife 使用方法
- react native 调用android activity
- windows 80端口占用问题
- 人工智能(七)逻辑Agent
- ZooKeeper基础与集群搭建资料汇总
- C++运算符重载
- Tensorflow深入mnist程序整理
- XRecyclerView多布局2
- 虚拟机连不上网,ping时报错,connect network is unreachable
- OkHttpUtils的封装
- 关于安装版JDK1.8 1.7更改JDK环境变量不生效的解决方法
- 云服务器和VPS以及虚拟机有什么区别?个人站长和中小企业如何选择才适合自己?
- 升级到ionic3后建立低版本项目
- 关于Base64编码