Design a class ‘Complex ‘with data members for real and imaginary part .Operation using Friend function and Member function and operator overloading ,definition of Friend function.

                       FRIEND FUNCTION IN CPP.


            The friend function is not in the scope of the class to which it has been declared as a friend. It cannot access member variables or functions directly, but can only be accessed by the help of the objects of the class. It can be declared with any access specifier and the access specifier does not have any impact on the friend function.


  • Design a class ‘Complex ‘with data members for real and imaginary part. Provide default and parameterized constructors. Write a program to perform arithmetic operations of two complex numbers using operator overloading. 
       i. Addition and subtraction using friend functions.       

       ii. Multiplication and division using member functions.

#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
  int real,img;
  public:
  complex()
   {
     real=0;
     img=0;
   }
  void accept()
   {
     cout<<"Enter complex no";
     cin>>real>>img;
   }
   friend complex operator+(complex p,complex q);
   friend complex operator-(complex p,complex q);
   complex operator*(complex p);
   complex operator/(complex p);
   void display()
    {
      cout<<real<<"+"<<img<<"i"<<"\n";
    }
 };

    complex operator+(complex p,complex q)
 {
  complex temp;
  temp.real=p.real+q.real;
  temp.img=p.img+q.img;
  return temp;
 }
    complex operator-(complex p,complex q)
 {
  complex temp;
  temp.real=p.real-q.real;
  temp.img=p.img-q.img;
  return temp;
 }
    complex complex::operator*(complex c2)
       {
 complex temp;
 temp.real=real*c2.real-img*c2.img;
 temp.img=img*c2.real+real*c2.img;
 return temp;
       }
    complex complex::operator/(complex c2)
       {  float a,b,c;
  complex temp;
  a=(real*c2.real)+(img*c2.img);
  b=(c2.real*c2.real)+(c2.img*c2.img);
  c=(-real*c2.img)+(img*c2.real);
  cout<<a<<"/"<<b;cout<<"+";cout<<c<<"/"<<b<<"i";
       }

int main()
{
   complex c1,c2,c3;
   int n=0;
//clrscr();
c1.accept();
c2.accept();
do
{
  cout<<"Enter\n 1 for add\n2 for sub\n3 for multi\n4 for div\n";
  cout<<"Enter your choice";
  cin>>n;
  
 switch(n)
  {
    case 1:c3=c1+c2;
    c3.display();
    break;
    case 2:c3=c1-c2;
    c3.display();
    break;
    case 3:c3=c1*c2;
    c3.display();
    break;
    case 4:c1/c2;
    break;
 }
}while(n!=5);
  getch();
 return 0;
}
OUTPUT:
Enter complex no3 4
Enter complex no6 3
Enter
 1 for add
2 for sub
3 for multi
4 for div
Enter your choice1
9+7i
Enter
 1 for add
2 for sub
3 for multi
4 for div
Enter your choice2
-3+1i
Enter
 1 for add
2 for sub
3 for multi
4 for div
Enter your choice3
6+33i
Enter
 1 for add
2 for sub
3 for multi
4 for div
Enter your choice4
30/45+15/45i

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages