Wednesday 13 July 2011

Program Class Tempate 3

#include <iostream.h>
#include <conio.h>

template <class T>
class Complex {
public:
    Complex (T re=0, T im=0);
    Complex<T> add(Complex<T> a, Complex<T> b);
    void cetak();
private:
    T real, imaginary;
};
   
template <class T>
Complex<T>::Complex (T re, T im)
{
    real = re; imaginary = im;
}
   
template <class T>
Complex<T> Complex<T>:: add(Complex<T> a, Complex<T> b)
{
    Complex temp;
    temp.real = a.real + b.real;
    temp.imaginary = a.imaginary + b.imaginary;
    return temp;
}
   
template <class T>
void Complex<T>::cetak()
{
    cout << "[" << real << ", " << imaginary << "]" << endl;
}
   
int main()
{
    Complex <int> a(2,3), b(1,1), c;
    c = c.add(a,b);
    c.cetak();

    getche();
    return 0;
}

No comments: