| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4695 人关注过本帖
标题:C++类模板,重载-函数没有用,怎么修改
只看楼主 加入收藏
星星的光芒
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2014-11-12
结帖率:0
收藏
 问题点数:0 回复次数:0 
C++类模板,重载-函数没有用,怎么修改
#include<iostream>
using namespace std;
template<class T>
class Complex
{
private:
    T real;
    T imag;
public:
    Complex<T>(T r,T i);
    Complex<T>();

    void print();
    Complex<T> operator+(Complex<T>  &c);
    Complex<T>  operator-(Complex<T>  &c);


};
template<class T>
Complex<T>::Complex<T>(T r,T i)
{
    real=r;
    imag=i;
}

template<class T>
Complex<T>::Complex<T>()
{
    real=0;
    imag=0;
}

template<class T>
void Complex<T>::print()
{
    cout<<real;
    if(imag!=0)
    {
        if(imag>0)    cout<<"+";
    cout<<imag<<"i";

    }
    else cout<<imag<<"i";
    cout<<endl;

}

template<class T>
Complex<T>  Complex<T>::operator +(Complex<T>  &c)
{
    real+=c.real;
    imag+=c.imag;
    return *this;
}

template<class T>
Complex<T>  Complex<T>::operator -(Complex <T> &c)
{
    real-=c.real;
    imag-=c.imag;
    return *this;
}
int main()
{  
    Complex<int> c1(1,2),c2(4,5),c5,c6;
    Complex<double> c3(2.4,4.5),c4(3.3,6.6),c7,c8;
    c5=c1+c2;
    c6=c1-c2;
    c7=c3+c4;
    c8=c3-c4;
    c5.print();
    c6.print();
    c7.print ();
    c8.print();
    return 0;
}
搜索更多相关主题的帖子: Complex private include public 
2015-06-14 10:51
快速回复:C++类模板,重载-函数没有用,怎么修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014058 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved