第六周项目6.3 友元函数实现复数的加法

本文介绍如何使用C++的友元函数来实现复数类的加法操作,通过友元函数打破封装,实现复数对象间的相加功能。
摘要由CSDN通过智能技术生成
/*
*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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值