第6周项目6复数模板类(友元函数)
来源:互联网 发布:加载寄售物品数据库 编辑:程序博客网 时间:2024/06/06 09:04
#include <iostream>using namespace std;template <class T1>class Complex{public: Complex( ) { real=0; imag=0; } Complex(T1 a,T1 b) { real=a; imag=b; } Complex complex_add(Complex &c); template <class T2> friend Complex<T2> add_complex(Complex<T2> &c1,Complex<T2> &c2);//利用了模板的外部函数要作为友元函数,注意声明方式:类声明中也必须给出模板声明 void display();private: T1 real; T1 imag;};template<class T1>Complex<T1> Complex<T1>::complex_add(Complex<T1> &c){ Complex<T1> a; a.real=real+c.real; a.imag=imag+c.imag; return a;}template<class T1>Complex<T1> add_complex(Complex<T1> &c1,Complex<T1> &c2){ Complex<T1> c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c;}template<class T1>void Complex<T1>::display(){ cout<<'('<<real<<','<<imag<<')'<<endl;}int main( ){ Complex<int> c1(3,4),c2(5,-10),c3; c3=c1.complex_add(c2); //调用成员函数支持加法运算,有一个形参 cout<<"c1+c2="; c3.display( ); Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6; c6=c4.complex_add(c5); //调用成员函数支持加法运算,有一个形参 cout<<"c4+c5="; c6.display( ); Complex<int> c7; c7=add_complex(c1,c2); //调用友员函数支持加法运算,有两个形参 cout<<"c1+c2="; c7.display( ); Complex<double> c8; c8=add_complex(c4,c5); //调用友员函数支持加法运算,有两个形参 cout<<"c4+c5="; c8.display( ); return 0;}
0 0
- 第6周项目6-复数模板类(2)(友元函数版)
- 第6周项目6复数模板类(友元函数)
- 第6周项目6--复数模板类-友元
- 第5周-项目6-复数模板类-友元函数
- 第六周项目六--复数模板类(友元函数)
- C++第7周(春)项目5 复数模板类(加使用友元函数拓展)
- C++第7周(春)项目5 复数模板类(加使用友元函数拓展) .
- 第六周上机实践项目6-复数模板类(3、友元的实现)
- 第6周项目6-复数模板类(1)
- 第6周项目6-复数模板类(2)
- 第12周项目-复数类中的运算符重载(2)(友元函数)
- 第8周项目1-实现复数类中的运算符重载(友元函数)
- 第6周项目6-2,3复数模板类
- 第6周项目6-3复数模板类
- 第6周项目6-复数模板类
- 第5周-项目6-复数模板类
- 第5周项目6-复数类模板
- 第6周项目5复数模板类
- Integer与int比较
- TEXT() 宏,以宏UNCODE为其开关,代表windows 编程风格;_T() 宏,以宏_UNCODE为其开关,代表C/C++编程风格
- 稳、准、狠:京东开放平台的“速度与激情”
- 程序员为什么喜欢夜间工作?
- 马云鼓动年轻人2015创业是存私心的?
- 第6周项目6复数模板类(友元函数)
- 如何做seo优化
- 通过重写ViewGroup学习onMeasure()和onLayout()方法
- java解惑之陷阱和缺陷的目录总结
- linux adb无法连接的问题
- 阿里2015年4月实习生招聘研发岗笔试题——RPC题解
- 基于Lockset的数据竞争检测方法汇总(二)
- 记事本在读取一个文本时如何判断是该用啥字符集来读取解析文本字符
- 多选AlertDialog至多选择3项