Pages

Monday, March 24, 2014

Add two complex numbers using friend function in C++

//adding two complex number using friend function
#include<iostream>
#include<cmath>
using namespace std;
class complex
{
    private:
        float real,imag;
        friend complex add (complex c1, complex c2);
    public:
        void getdata()
        {
            cout<<"enter the real part: ";
            cin>>real;
            cout<<"enter the imag part: ";
            cin>>imag;
        }
        void showdata()
        {
            cout<<real<<"+"<<imag"i"<<endl;
        }
};
        complex add(complex c1,complex c2)
        {
            complex c3;
            c3.real=c1.real+c2.real;
            c3.imag=c1.imag+c2.imag;
            return c3;
        }
    int main()
    {
        complex c1,c2,c3;
        cout<<"enter first complex number: "<<endl;
        c1.getdata();
        cout<<"enter second complex number: "<<endl;
        c2.getdata();
        c3=add(c1,c2);
        cout<<"sum of two complex number: ";
        c3.showdata();       
        system("pause");
        return 0;
    }

No comments:

Post a Comment