Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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.

#include<iostream>
using namespace std;
class Complex
{
 private:
  float real,img;
 public:
  Complex()
  {
   real=0;
   img=0;
  }
  void accept()
  {
   cout<<"Enter the complex number:"<<"
";
   cout<<"Real:";
   cin>>real;
   cout<<"Imaginary:";
   cin>>img;
  }
  void display()
  {
   cout<<"complex number is:";
   cout<<real<<"+"<<img<<"i"<<"
";
  }
  Complex(float a,float b)
  {
   real=a;
   img=b;
  }
  friend Complex operator +(Complex c1,Complex c2)
  {
   c1.real=c1.real+c2.real;
   c1.img=c1.img+c2.img;
   return c1;
  }

  friend Complex operator -(Complex c1,Complex c2)
  {
   c1.real=c1.real-c2.real;
   c1.img=c1.img-c2.img;
   return c1;
  }
  Complex operator *(Complex c2);
  Complex operator /(Complex c2);
};
Complex Complex::operator *(Complex c2)
{
 Complex c3;
 c3.real=(real*c2.real)-(img*c2.img);
 c3.img=(real*c2.img)+(img*c2.real);
 return c3;
}
Complex Complex::operator /(Complex c2)
{
 Complex c3;
 c3.real=((real*c2.real)+(img*c2.img))/((c2.real*c2.real)+(c2.img*c2.img));
 c3.img=((real*c2.img)+(img*c2.real))/((c2.real*c2.real)+(c2.img*c2.img));
 return c3;
}
int main()
{
 int ch;
 Complex c3;		//default constructor
 Complex c4(4,5);		//parameterized constructor
 
 Complex c5;
 c5.accept();
 cout<<"1st";
 c4.display();
 cout<<"
";
 cout<<"2nd";
 c5.display();
 cout<<"
";

 
 do
 {
 cout<<"
"<<"Enter your choice:";
 cout<<"1.Addition"<<"
"<<"2.Substraction"<<"
"<<"3.Multiplication"<<"
"<<"4.Division"<<"
";
 cin>>ch;
 switch(ch)
 {
  case 1: 	cout<<"Addition:";
 		//c3=c4+c5;
 		c3=operator+(c4,c5);
 		c3.display();
 		cout<<"
";
 		break;
  case 2:   cout<<"Substraction:";
		// c3=c4-c5;
 		c3=operator-(c4,c5);
 		c3.display();
		cout<<"
";
		break;
  case 3:   cout<<"Multication:";
 		c3=c4*c5;
 		//c3=c4.operator*(c5);
 		c3.display();
 		cout<<"
";
		break;
  case 4:	cout<<"Division:";
		// c3=c4/c5;
 		c3=c4.operator/(c5);
 		c3.display();
 		cout<<"
";
		break;
  default:
 		cout<<"EXIT";
 }
 }while(ch<=4);
 return 0;
}
Comment

PREVIOUS NEXT
Code Example
Java ::  
::  
::  
::  
Java ::  
Java ::  
Java ::  
Java :: javafx edit list 
::  
::  
Java ::  
:: get free player inventory slots spigot 
:: osmdroid get current zoom level 
:: windows Jmeter java.net.BindException: Address already in use: connect 
:: Java Add Elements to an ArrayList 
:: java stream sort Collator 
Java ::  
Java :: java array merge 
::  
::  
::  
::  
::  
:: android studio find all views in layout 
::  
Java :: search 
:: multiple spinner android 
::  
::  
::  
ADD CONTENT
Topic
Content
Source link
Name
6+2 =