Template ---- addition, subtraction, multiplication, division

Source: Internet
Author: User

/** Copyright (c) 2013, School of Computer Science, Yantai University * All rights reserved. * Author: Ma Guangming * Completion Date: July 15, April 9, 2014 * Problem description: Template Class-addition, subtraction, multiplication, division * version number: v1.0 */# include
 
  
Using namespace std; template
  
   
Class Complex {public: Complex () {real = 0; imag = 0;} Complex (T r, T I) {real = r; imag = I ;} complex complex_add (Complex & c2); Complex complex_minus (Complex & c2); Complex complex_multiply (Complex & c2); Complex complex_divide (Complex & c2); void display (); private: T real; T imag;}; template
   
    
Complex
    
     
Complex
     
      
: Complex_add (Complex
      
        & C2) {Complex
       
         C; c. real = real + c2.real; c. imag = imag + c2.imag; return c;} template
        
          Complex
         
           Complex
          
            : Complex_minus (Complex
           
             & C2) {Complex
            
              C; c. real = real-c2.real; c. imag = imag-c2.imag; return c;} template
             
               Complex
              
                Complex
               
                 : Complex_multiply (Complex
                
                  & C2) {Complex
                 
                   C; c. real = real * c2.real-imag * c2.imag; c. imag = imag * c2.real + real * c2.imag; return c;} template
                  
                    Complex
                   
                     Complex
                    
                      : Complex_divide (Complex
                     
                       & C2) {Complex
                      
                        C; T d = c2.real * c2.real + c2.imag * c2.imag; c. real = (real * c2.real + imag * c2.imag)/d; c. imag = (imag * c2.real-real * c2.imag)/d; return c;} template
                       
                         Void Complex
                        
                          : Display () {cout <"(" <
                         
                           C1 (3, 4), c2 (5,-10), c3; c3 = c1.complex _ add (c2); cout <"c1 + c2 ="; c3.display (); Complex
                          
                            C4 (3.1, 4.4), c5 (5.34,-10.21), c6; 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 ;}
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Related Article

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.