You must Sign In to post a response.
  • Category: Programming

    Program to find sum of two complex numbers using friend function


    Are find difficulty in Object Oriented Programming using C++ subject preparation? Want to know the solution and program code for finding sum of two complex numbers using friend function? Scroll through this page and go through the program solution and explanation provided by our ISC Experts here.

    Following is a question that was asked in the question paper of BCA(May 2017) for subject Object Oriented Programming using C++(US-649).

    This question is asked for 10 marks and hence requires some explanation along with the program code. Please let me know the solution -
    Q) Write a C++ program to find sum of two complex numbers using friend function. [10 Marks]
  • Answers

    2 Answers found.
  • In the page mentioned below, a programme has been given for C++ Program to add two complex no using a friend function. You can visit this page so that you can use the programme for your understanding.
    https://www.technicalsymposium.com/CPP_Program_for_add_two_complex_no_using_friend_function.html#.XI47HCgzbIU

    drrao
    always confident

  • Friend function is a very useful feature provided by Object Oriented Programming. As class contains different functions and through object of that class it is easy to get access to these class functions. But when there is requirement to access private data from two or more classes then it become necessary to have a common function to all classes and that function has ability to access private data of different classes. Such a function is nothing but the friend function. In below program friend function is used on a single class but normally friend function is used to access private data of different classes.
    //PROGRAM TO ADD TWO COMPLEX NUMBERS USING FRIEND FUNCTION
    #include
    #include
    class complex
    {
    private:
    int real,imag;
    public:
    complex(){}
    complex(int x, int y)
    {
    real=x;
    imag=y;
    }
    friend complex add(complex c1, complex c2)
    {
    complex temp;
    temp.real=c1.real+c2.real;
    temp.imag=c1.imag+c2.imag;
    return(temp);
    }
    void display(void)
    {
    cout< }
    };
    void main(void)
    {
    class complex m1(12,22);
    class complex m2(33,22);
    class complex re;
    clrscr();
    cout<<"\nFirst Complex number is \n";
    m1.display();
    cout<<"\nSecond Complex number is \n";
    m2.display();
    cout<<"\nAfter addition of above two complex numbers\n";
    cout<<"\nResultant complex number is \n";
    re=add(m1,m2);
    re.display();
    getch();
    }


  • Sign In to post your comments