01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: complx. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, April 12, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# 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 & C2); complex complex_cut (complex & C2); complex complex_mul (complex & C2 ); complex complex_div (complex & C2); void display (); Private: numtype real; numtype imag ;}; template <class numtype> complex <numtype> :: complex_add (complex & C2) {Complex C; C. real = real + c2.real; C. imag = imag + c2.imag; return C;} template <class numtype> complex <numtype>: complex_cut (complex & C2) {Complex C; C. real = real-c2.real; C. imag = imag-c2.imag; return C;} template <class numtype> complex <numtype>: complex_mul (complex & C2) {Complex C; C. real = real * c2.real-imag * c2.imag; C. imag = real * c2.imag + imag * c2.real; return C;} template <class numtype> complex <numtype>: complex_div (complex & C2) {Complex C; c. real = (Real * c2.real + imag * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag); C. imag = (-Real * c2.imag + imag * c2.real)/(c2.real * c2.real + c2.imag * c2.imag); Return C;} template <class numtype> void complex <numtype> :: display () {cout <"(" <real <"," <imag <"I)" <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 (); // The following test subtraction, multiplication, and Division complex <int> C7 (3, 3), C8 (6, -1), C9; C9 = c7.complex _ Cut (C8); cout <"c7-c8 ="; c9.display (); complex <int> C10 (3, 4 ), c11 (5, 5), C12; C12 = c10.complex _ MUL (C11); cout <"C10 * C11 ="; c12.display (); complex <double> C13 (5, 3 ), c14 (4, 6), C15; C15 = c13.complex _ MUL (C14); cout <"C13/C14 ="; c15.display (); Return 0 ;}
Running result: