Write a program to perform complex arithmetic using operator overloading
#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(); } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:[email protected]
Good.keep it up
very thank to u…