复数四则运算--类模板实现
来源:互联网 发布:网络贷款还不起怎么办 编辑:程序博客网 时间:2024/04/30 05:31
#include<iostream>using namespace std;template<class numtype>class Complex {public:Complex(numtype a=0, numtype b=0) :x(a), y(b) {}Complex complex_add(Complex &);Complex complex_sub(Complex &);Complex complex_mul(Complex &);Complex complex_div(Complex &);void display();private:numtype x, y;};//每一个成员函数的定义前,必须要声明类模板//使用了模板的类,将不再独立使用,其类名的完整表示为“类模板名<虚拟类型参数>” //凡用到类名处也用“类模板名<虚拟类型参数>”形式;template<class numtype> Complex<numtype> Complex<numtype>::complex_add(Complex<numtype> &c) {Complex<numtype> nc;nc.x = x + c.x;nc.y = y + c.y;return nc;}template<class numtype>Complex<numtype> Complex<numtype>::complex_sub(Complex<numtype> &c) {Complex<numtype> nc;nc.x = x - c.x;nc.y = y - c.y;return nc;}template<class numtype>Complex<numtype> Complex<numtype>::complex_mul(Complex<numtype> &c) {Complex<numtype> nc;nc.x = x*c.x-y*c.y;nc.y = x*c.y+y*c.x;return nc;}template<class numtype>Complex<numtype> Complex<numtype>::complex_div(Complex<numtype> &c) {Complex<numtype> nc;nc.x = (x*c.x+y*c.y)/(c.x*c.x+c.y*c.y);nc.y = (y*c.x - x*c.y) / (c.x*c.x + c.y*c.y);return nc;}template<class numtype>void Complex<numtype>::display() {cout <<"("<< x <<","<<y<<"i)"<< endl;}int main(){Complex<int> c1(1, 2), c2(2, 4), c3; //实部和虚部是int型 c3 = c1.complex_add(c2);cout << "c1+c2=";c3.display();c3 = c1.complex_sub(c2);cout << "c1-c2=";c3.display();c3 = c1.complex_mul(c2);cout << "c1*c2=";c3.display();c3 = c1.complex_div(c1);cout << "c1/c2=";c3.display();return 0;}
0 0
- 复数四则运算--类模板实现
- 使用模板类实现复数的四则运算
- 复数的四则运算(模板类)
- 第7周任务3(使用类模板,实现复数的四则运算)
- 复数类完整实现 + 四则运算符重载
- 复数类的四则运算
- 给出复数,实现复数的四则运算
- 复数四则运算-c++代码实现
- 复数四则运算
- 复数模板类的简单实现
- 实现复数的加减乘除四则运算的C#类(已测成功)
- 复数模板类、
- 复数模板类
- 复数类模板
- 复数模板类
- 复数模板类
- 复数模板类
- 复数模板类1
- django中的一个数据库错误 django.db.utils.InternalError: 1366
- emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is
- Unity文件操作——调用减压文件解压
- TimesTen 数据库复制学习:9. 更改Active Standby Pair
- JSP实现验证码(利用tooltip进行消息提示)
- 复数四则运算--类模板实现
- java中各种术语
- HDU 5038 Grade
- os.time 和 os.date
- 【git】 安装与使用
- day_03文件操作模式
- 添物不花钱学计算机及编程(预备篇) - 计算机网络
- 使用Spring处理一个RESTful请求
- Android中解析JSON总结