C++ program – Perform complex arithmetic using operator overloading

by Nideesh C on April 14, 2011 · 0 comments

in C++




#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class complex
{
	int real;
	float image;
	public:
	void getdata()
	{
		cout<<"\n enter the real part of the complex";
		cin>>real;
		cout<<"\n enter the imaginary part of the complex";
		cin>>image;
	}
	void operator + (complex);
	void operator - (complex);
};
 
void complex :: operator +  (complex c1)
{
	complex temp;
	temp.real=real+c1.real;
	temp.image=image+c1.image;
	if (temp.image>=0);
	{
		cout<<"\n complex no. after addition:";
		cout<<temp.real<<"+"<<temp.image<<"i";
	}
	else
	{
		cout<<"\n complex no. after addition ";
		cout<<temp.real<<temp.image<<"i";
	}
}
void complex ::operator-(complex c1)
{
	complex temp;
	temp.real = real-c1.image;
	temp.image= image-c1.image;
	if (temp.image>=0)
	{
		cout<<"\n complex no. after subtraction";
		cout<<"\n temp.real<<"+"<<temp.image<<"i";
	}
	else
	{
		cout<<"\n complex no. after subtraction";
		cout<<temp.real<<temp.image<<"i"
	}
}
void main()
{
	clrscr();
	comp.ex c1, c2;
	int n;
	do
	{
		cout<<"\n 1. Input data for complex no. ";
		cout<<"\n 2. Addition of complex no. ";
		cout<<"\n 3. Subtraction of complex no. ";
		cout<<"\n 4. Quit";
		cout<<"\n Enter your choice";
		cin>>n;
		switch(n)
 
		{
			 case1:
			 cout<<endl<<"\n Enter the data for First Complex No......";
			 cl.getdata();
			 cout<<endl<<"\n Enter the data for seconds Complex No.....";
			 c2.getdata();
			 clrscr();
			 break;
 
			 case 2;
			 cl+c2;
			 getch();
			 clrscr();
			 break;
 
			 case 3:
			 cl-c2;
			 getch();
			 clrscr();
			 brak;
 
			 case 4:
			 exit91);
			 break;
			}
		} while (n!=4);
     getch();
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to simulate a simple calculator to perform arithmetic operations like addition, subtraction,multiplication and division only on integers
  2. C program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace.
  3. C++ program – evaluate an expression entered in postfix form
  4. C++ Program – Array Based Representation of Linear List using templates
  5. C++ program to implement Stack using Formula Based Representation

Leave a Comment

Previous post:

Next post: