1 //learning about class templates2 //designing a complex class template3 //1. member functions for complex class templates include add and output4 //2. The member function plus can be the addition of two complex class template objects, or a complex number class template object and a template parameter as the real part of the value added5 //3. Design a test main function that requires testing the main function to define both the plural class object with the actual parameter float and the complex number class object with the actual argument double .6 7#include <iostream.h>8 9Template <classT>Ten classcomplex{ One Private: A T Real; - T image; - Public: theComplex (T x=0, T y=0); -~Complex () {} - -Complex Add (ConstComplex x)Const; +Complex Add (ConstT x)Const; - voidShow ()Const; + }; A atTemplate <classT> -Complex<t>:: Complex (T x,t y) { - This->real=x; - This->image=y; - } - in -Template <classT> toComplex<t> Complex<t>::add (ConstComplex x)Const{ + returnComplex<t> ( This->real+x.real, This->image+x.image); - } the *Template <classT> $Complex<t> Complex<t>::add (ConstT x)Const{Panax Notoginseng returnComplex<t> ( This->real+x, This-image); - } the +Template <classT> A voidComplex<t>::show ()Const{ thecout<<"Real is"<< This->real<<Endl; +cout<<"Image is"<< This->image<<Endl; - } $ $ - intMain () { -complex<float>x (1.1,1.1), Y (2.2,2.2), Z; thez=X.add (y); -cout<<"Z is";Wuyi z.show (); the -complex<Double>u (1.111,1.111), V (2.222,2.222), W; Wuw=U.add (v); -cout<<"W is"; About w.show (); $ - return 0; -}
C + + Learning (27)