Skip to main content

Complex Number Class (C++ with Exception Handling)

This is a Complex Number Class Definition in C++. The basic Operations on Complex Numbers are defined here also, from taking input to Algebra. And it has a tiny example of Exception Handling for new operator

#include <iostream.h>
class complex{
private:
float *x,*y;

public:
complex(float a=0.0, float b=0.0)
{
 try{
  x=new float;
  *x=a;
  y=new float;
  *y=b;
}
catch(...)
 {
  cout<<"Memory is not sufficient";
 }
}

complex operator + (complex c){
 complex z;
 *(z.x)=(*x) + *(c.x);
 *(z.y)=(*y) + *(c.y);
 return z;
}

complex operator - (complex c){ // Binary Minus
 complex z;
 *(z.x)=(*x) - *(c.x);
 *(z.y)=(*y) - *(c.y);
 return z;
}

complex operator - (){ // Unary Minus
complex z;
*(z.x)=-(*x);
*(z.y)=-(*y);
return z;
}

complex operator * (complex c){
 complex z;
 *(z.x)=(*x)*(*(c.x))-(*y)*(*(c.y));
 *(z.y)=(*y)*(*(c.x))+(*x)*(*(c.y));
 return z;
}

complex operator / (complex c){
 complex z;
 float t;
 t=(*(c.x))*(*(c.x)) + (*(c.y))*(*(c.y));
 *(z.x)=((*x)*(*(c.x)) + (*y)*(*(c.y)))/t;
 *(z.y)=((*y)*(*(c.x)) - (*x)*(*(c.y)))/t;
 return z;
}

friend ostream & operator << (ostream &o,complex c);
friend istream & operator >> (istream &i,complex c);
};



ostream & operator << (ostream &o,complex c){
 if (*(c.y)>=0.0)
 o<<*(c.x)<<"+"<<*(c.y)<<"i";
 else
 o<<*(c.x)<<*(c.y)<<"i";

 return o;
}

istream & operator >> (istream &i,complex c){
cout<<"(Real Part):: ";
i>>*(c.x);
cout<<"(Img. Part):: ";
i>>*(c.y);

return i;
}


void main()
{
 complex a,b,c;
 cout<<"Enter complex Number 1:: ";
 cin>>a;
 cout<<"Enter complex Number 2:: ";
 cin>>b;
 cout<<"The Complex Numbers are::\nA="<<a<<endl<<"B="<<b;
 cout<<"\n-B="<<-b;           //Negetive of Complex Number
 cout<<"\nA+B="<<a+b;         //Addition of Complex Numbers
 cout<<"\nA-B="<<a-b;         //Subtraction of Complex Numbers
 cout<<"\nAxB="<<a*b;         //Multiplication of Complex Numbers
 cout<<"\nA/B="<<a/b;         //Division of Complex Numbers

}

This may not run on all Turbo C++ Compiler due to exception handling code.

Comments

Popular posts from this blog

Class Point:: It holds co-ordinates of a point in format (x,y)

A class Named "Point" which stores the Co-ordinates of a point in (x,y) Format. This Class Contains:: O Constructor with default arguments O Copy Constructor O Function to display which Co-ordinate a Point belongs to O Function to get value of x of a point O Function to get value of y of a point O Function to get the Radious Vector (r) of a point O Function to get the Theta of a point O Overloaded Operator - to find out distance between two points O Overloaded Operator >> to take input O Overloaded operator O Destructor #include<iostream.h> #include<math.h> class point{ //Data Members int x,y; //They hold the value of X and Y co-ordinate of a Point. public: //Member Functions point(int t1=0, int t2=0) //Constructor with default arguments. { x=t1; y=t2; } point(point &t) //Copy Constructor { x=t.x; y=t.y; } int coordinate(void); //It returns the point's Co-ordinate, e.g., 1, 2, 3, or 4th. int getx(...

How to set Set-Top Box to Resume automatically to last Channel after switch on?

How to set customised automatic Resume Channel to last viewed in Set-Top Box?  Introduction In the era of Digitally fast growing life WiFi enabled smart devices and also voice controlled smart devices are trying to be companion of every human being. But, all human do not change their habit at the same time. It may be cause of mental attachment or financial limits or unwillingness or though as unnecessary. What ever the reason may be, there exists the old technology, like IR(Infrared), RF(Radio Frequency), etc. So, this article for them who are using "Un-smart" TV like me in this time of Smart World. I was searching for a solution to set my Set-top Box of Videocon DTH (controlled by a RF Remote) to resume the last viewed channel when I power on my TV anytime. Suppose, I was watching NAT-Geo and switch off my (Un-smart) TV, even switch off the plug power at board. Now, after some time / on the next day, when I switch on my TV it will directly tune to the last viewed channel, i....