程序頭部註釋開始
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱: 複數類的改進
* 作 者: 郭廣建
* 完成日期: 2012年 4 月 03 日
* 版 本 號: 1.0
題目介紹:
【任務3】閱讀P314的例10.1(電子版的在平臺上見txt文件)。該例實現了一個複數類,但是美中不足的是,複數類的實部和虛部都固定是double型的。可以通過模板類的技術手段,設計Complex,使實部和虛部的類型爲定義對象時用的實際類型。
(1)要求類成員函數在類外定義。
(2)在此基礎上,再實現減法、乘法和除法
你可以使用的main()函數如下。
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( );
system("pause");
return 0;
}
附:例10.1的代碼
#include <iostream>
using namespace std;
class Complex
{
public:
Complex( ){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex complex_add(Complex &c2);
void display( );
private:
double real;
double imag;
};
Complex Complex::complex_add(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void Complex::display( )
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main( )
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1="; c1.display( );
cout<<"c2="; c2.display( );
cout<<"c1+c2="; c3.display( );
return 0;
}
源程序:
#include<iostream>
using namespace std;
template<class comstyle>
class Complex
{
public:
Complex( ){real = 0; imag = 0;}
Complex(comstyle r,comstyle i){real = r; imag = i;}
Complex complex_add(Complex &c2);
Complex complex_subtract(Complex &c2);
Complex complex_multiply(Complex &c2);
Complex complex_divide(Complex &c2);
void display( );
private:
comstyle real;
comstyle imag;
};
template<class comstyle>
Complex<comstyle> Complex<comstyle>::complex_add(Complex<comstyle> &c2) //加法定義
{
Complex<comstyle> c;
c.real = real + c2. real;
c.imag = imag + c2. imag;
return c;
}
template<class comstyle>
Complex<comstyle> Complex<comstyle>::complex_subtract(Complex<comstyle> &c2) //減法定義
{
Complex<comstyle> c;
c.real = real - c2. real;
c.imag = imag - c2. imag;
return c;
}
template<class comstyle>
Complex<comstyle> Complex<comstyle>::complex_multiply(Complex<comstyle> &c2) //乘法定義
{
Complex<comstyle> c;
c.real = real * c2.real - imag * c2.imag ;
c.imag = real * c2.imag + imag * c2.real ;
return c;
}
template<class comstyle>
Complex<comstyle> Complex<comstyle>::complex_divide(Complex<comstyle> &c2) //除法定義
{
Complex<comstyle> c;
c.real = (real * c2.real + imag * c2.imag) / (c2 .real * c2.real + c2.imag * c2.imag);
c.imag = (imag * c2.real - real * c2.imag) / (c2 .real * c2.real + c2.imag * c2.imag);
return c;
}
template<class comstyle>
void Complex<comstyle>::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( );
c3 = c1.complex_subtract(c2);
cout << "c1-c2 =";
c3.display( );
c3 = c1.complex_multiply(c2);
cout << "c1*c2 =";
c3.display( );
c3 = c1.complex_divide(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( );
c6 = c4.complex_subtract(c5);
cout << "c4-c5 =";
c6.display( );
c6 = c4.complex_multiply(c5);
cout << "c4*c5 =";
c6.display( );
c6 = c4.complex_divide(c5);
cout << "c4/c5 =";
c6.display( );
system("pause");
return 0;
}
運行結果:
經驗積累:
1).類模板可以避免函數的重載的定義和運用的麻煩
2).但是在類中成員函數定義之前應該注意不能忘掉 “template<class comstyle>”
3).並沒有一勞永逸的解決方法,一方面簡單的同時,在某些方面肯定有很多注意點,所以在應用時要慎重!