/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:score.cpp
*作 者:惠睿
*完成日期:2015年4月10日
*版 本 号:v1.0
*
*问题描述:利用模板类,实现各种类型的复数的加法。
*程序输入:无输入。
*程序输出:输出运算后的结果。
*/
#include <iostream>
using namespace std;
template <class numtype>
class Complex
{
public:
Complex()
{
real=0;
imag=0;
}
Complex(numtype r,numtype i)
{
real=r;
imag=i;
}
Complex complex_add(Complex &c1);
template <class numtype1> friend Complex<numtype1> add_complex(const Complex<numtype1> &c1,const Complex<numtype1> &c2);
void display();
private:
numtype real;
numtype imag;
};
template <class numtype>
Complex<numtype> Complex<numtype>::complex_add(Complex<numtype> &c1)
{
Complex<numtype> c;
c.real=real+c1
第六周项目6.3 友元函数实现复数的加法
最新推荐文章于 2022-04-27 13:10:21 发布
本文介绍如何使用C++的友元函数来实现复数类的加法操作,通过友元函数打破封装,实现复数对象间的相加功能。
摘要由CSDN通过智能技术生成