C + + Learning (27)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.