To run the code:
<span style= "FONT-SIZE:18PX;" >/**copyright (c) 2014, College of Computer and Control engineering, Yantai University *all rights reserved.*dood luck* file name: d.cpp* Author: Zhang Wanhua * Completion Date: April 15, 2015 * version number: v1.0* *///Item 6-Plural template class "//This example implements a plural class, but in the ointment, the real and imaginary parts of the plural class are fixed only as double type. The complex can be designed by the technical means of the template class to make the type of the real and imaginary parts the actual type specified when the object is defined. (1) A class member function is required to be defined outside the class. (2) On this basis, then the subtraction, multiplication and division//You can use the main () function as follows. #include <iostream>using namespace Std;template <class t>class Complex{public:complex () {real=0 ; imag=0; } Complex (T m,t N) {real=m; Imag=n; } Complex Complex_add (Complex &); Complex Complex_minus (Complex &); Complex complex_multiply (Complex &); Complex complex_divide (Complex &); void display ();p rivate:t Real; Each T in the class declaration will be replaced by the actual type provided when the object is defined instead of T imag;}; int main () {complex<int> C1 (3,4), C2 (5,-10), C3;//When defining an object, use the "class template name < actual type name >" Form cout<< "c1="; C1.display (); cout<< "c2="; C2.display (); C3=c1.complex_add (C2); cout<< "c1+c2="; C3.disPlay (); C3=c1.complex_minus (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 (); cout<<endl; complex<double> C4 (3.1,4.4), C5 (5.34,-10.21), C6; When defining an object, use the "class template name < actual type name >" Form cout<< "c4="; C4.display (); cout<< "c5="; C5.display (); C6=c4.complex_add (C5); cout<< "c4+c5="; C6.display (); C6=c4.complex_minus (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 (); return 0;} Template <class t>void complex<t>::d isplay () {cout<< ' (' <<real<< ', "<<imag< < "i)" <<ENDL;} Template <class t>complex<t> complex<t>:: Complex_add (Complex &m) {Complex C; C.real=real+m.real; C.imag=imag+m.imag; return c;} Template <class t>complex<t> Complex<t>::complex_minus (Complex &m) {Complex C; C.real=real-m.real; C.imag=imag-m.imag; return c;} Template <class t>complex<t> complex<t>::complex_multiply (Complex &m) {Complex C; C.real=real*m.real+imag*m.imag; C.imag=real*m.imag+imag*m.real; return c;} Template <class t>complex<t> complex<t>::complex_divide (Complex &m) {Complex C; T N=m.real*m.real-m.imag*m.imag; C.real= (Real*m.real+imag*m.imag)/n; c.imag= (real*m.imag+imag*m.real)/n; return c;} </span>
Operation Result:
Knowledge Point application:
The use of templates,
Learning experience: The beginning of the complex number of subtraction think very simple, but look back to find yourself too 2, review high school knowledge exchange is quite simple.
"Item 6-Complex template class" In week Six