Week six (class template)

Source: Internet
Author: User

/*
*copyright (c) 2015, College of Computer Science, Yantai University
*all rights reserved.
* File name: Week sixth (class template)

* Wangzhong
* Completion Date: 2015.4.14

* Version Number: v1.0
*
* Problem Description: You can design the complex by technical means of the template class, so that the type of the real and imaginary parts is 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, the subtraction, multiplication and division are realized again.

* Input Description:

* Program output:

#include <iostream>using namespace Std;template<class numtype>class Complex{public:complex () {R        eal=0;    imag=0;        } Complex (Numtype r,numtype i) {real=r;    Imag=i;    } Complex Complex_add (Complex &c2);    Complex complex_str (Complex &c2);    Complex Complex_mul (Complex &c2);    Complex Complex_div (Complex &c2); void display ();p rivate:numtype Real,imag;}; Template<class numtype>complex<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<numtype>::complex_str (Complex &c2) {Complex C    ;    C.real=real-c2.real;    C.imag=imag-c2.imag; return c;} Template<class numtype>complex<numtype> Complex<numtype>::complex_mul (Complex &c2) {Complex C    ;    C.real=real*c2.real-imag*c2.imag;    C.imag=imag*c2.real+real*c2.imag; Return C;} Template<class numtype>complex<numtype> complex<numtype>::complex_div (Complex &c2) {Complex C    ;    Numtype A;    A=c2.real*c2.real+c2.imag*c2.imag;    C.real= (REAL*C2.REAL+IMAG*C2.IMAG)/A;    c.imag= (IMAG*C2.REAL-REAL*C2.IMAG)/A; return c;} Template<class numtype>void complex<numtype>::d isplay () {cout<< "(" <<real<< "," < <imag<< "i)" &LT;&LT;ENDL;}   int main () {complex<int> C1 (3,4), C2 (5,-10), C3;    The real and imaginary parts are of type int c3=c1.complex_add (C2);    cout<< "c1+c2=";    C3.display (); complex<double> C4 (3.1,4.4), C5 (5.34,-10.21), C6;    The real part and the imaginary part are double type c6=c4.complex_add (C5);    cout<< "c4+c5=";    C6.display ();    The following tests subtract, multiply and divide c3=c1.complex_str (C2);    cout<< "c1-c2=";    C3.display ();    C3=c1.complex_mul (C2);    cout<< "c1*c2=";    C3.display ();    C3=c1.complex_div (C2);    cout<< "c1/c2=";    C3.display (); return 0;}

When I imitate the multiplication, run the results, I found that the plural of multiplication and plus minus is not the same thing, I give depressed, tangled ah

Week six (class template)

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.