Add to Favorites    Make Home Page 1029 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Beginners / Lab Assignments Home » Complex Numbers Class with lot's of features

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Complex Numbers Class with lot's of features
Author Sanchit Karve
Author Email born2c0de [at] hotmail.com
Description This is a class that can be used for handling complex numbers.It includes overloaded operators and a == overloaded operator as well which can be used in a if or a while loop

Category C++ » Beginners / Lab Assignments
Hits 365353
Code Select and Copy the Code
#include <math.h> #include <iostream.h> #include <iomanip.h> #include <conio.h> class complex { private: float real; // Real Part float imag; // Imaginary Part public: complex(float,float); complex(complex&); complex operator +(complex); complex operator -(complex); complex operator *(complex); complex operator /(complex); complex getconjugate(); complex getreciprocal(); float getmodulus(); void setdata(float,float); void getdata(); float getreal(); float getimaginary(); bool operator ==(complex); void operator =(complex); friend ostream& operator <<(ostream &s,complex &c); }; // CONSTRUCTOR complex::complex(float r=0.0f,float im=0.0f) { real=r; imag=im; } // COPY CONSTRUCTOR complex::complex(complex &c) { this->real=c.real; this->imag=c.imag; } void complex::operator =(complex c) { real=c.real; imag=c.imag; } complex complex::operator +(complex c) { complex tmp; tmp.real=this->real+c.real; tmp.imag=this->imag+c.imag; return tmp; } complex complex::operator -(complex c) { complex tmp; tmp.real=this->real - c.real; tmp.imag=this->imag - c.imag; return tmp; } complex complex::operator *(complex c) { complex tmp; tmp.real=(real*c.real)-(imag*c.imag); tmp.imag=(real*c.imag)-(imag*c.real); return tmp; } complex complex::operator /(complex c) { float div=(c.real*c.real) + (c.imag*c.imag); complex tmp; tmp.real=(real*c.real)+(imag*c.imag); tmp.real/=div; tmp.imag=(imag*c.real)-(real*c.imag); tmp.imag/=div; return tmp; } complex complex::getconjugate() { complex tmp; tmp.real=this->real; tmp.imag=this->imag * -1; return tmp; } complex complex::getreciprocal() { complex t; t.real=real; t.imag=imag * -1; float div; div=(real*real)+(imag*imag); t.real/=div; t.imag/=div; return t; } float complex::getmodulus() { float z; z=(real*real)+(imag*imag); z=sqrt(z); return z; } void complex::setdata(float r,float i) { real=r; imag=i; } void complex::getdata() { cout<<"Enter Real:"; cin>>this->real; cout<<"Enter Imaginary:"; cin>>this->imag; } float complex::getreal() { return real; } float complex::getimaginary() { return imag; } bool complex::operator ==(complex c) { return (real==c.real)&&(imag==c.imag) ? 1 : 0; } ostream& operator <<(ostream &s,complex &c) { s<<"Real Part = "<<c.real<<endl <<"Imaginary Part = "<<c.imag<<endl; s<<"z = "<<c.real<<setiosflags(ios::showpos) <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos); return s; } void main() { complex a(10.0f,-2.f); // Calls Constructor cout<<a<<endl; // Calls the overloaded << operator complex b(-2); // Calls Constructor complex c=b; // Calls Copy Constructor c=a; // calls overloaded = operator b.getdata(); // Calls Getdata() c.getdata(); if(b==c) // calls overloaded == operator cout<<"b == c"; else cout<<"b != c"; cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function() complex d; d=a+b; // Calls overloaded + cout<<d<<endl; d=a-b; // Calls overloaded - cout<<d<<endl; d=a*b; // calls overloaded * cout<<d<<endl; d=a/b; // calls overloaded / cout<<d<<endl; getch(); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=544


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review