-->





c++ program for complex numbers using class

C++ class for addition, subtraction, multiplication and division for complex numbers

Class has four functions to perform arithmetic operations. It takes two complex numbers input from user real and imaginary parts separately.

The double data type is used to perform all operations. Code tested using c++ CodeBlocks IDE.


#include <iostream>
using namespace std;

//**********COMPLEX CLASS************************
class Complex{

private:
 double real,imag;

public:
 Complex(){
  real=imag=0;
 }
 ///////////////////////////////////////////////////
 Complex(double r){
  real=r;
  imag=0;
 }
    ///////////////////////////////////////////////////
 Complex(double r, double i){
  real=r;
  imag=i;
 }
    ///////////////////////////////////////////////////
 Complex(Complex &obj){
  real=obj.real;
  imag=obj.imag;
 }
    ///////////////////////////////////////////////////
 Complex add(Complex c){
        Complex Add;
  Add.real = real + c.real;
  Add.imag = imag + c.imag;
        return Add;
 }
    ///////////////////////////////////////////////////
 Complex sub(Complex c){
  Complex Sub;
  Sub.real = real - c.real;
  Sub.imag = imag - c.imag;
  return Sub;
 }
    ///////////////////////////////////////////////////
 Complex mult(Complex c){
        Complex Mult;
  Mult.real = real*c.real - imag*c.imag;
  Mult.imag = real*c.imag - c.real*imag;
  return Mult;
 }
    ///////////////////////////////////////////////////
 Complex div(Complex c){
  Complex Div;
  Div.real = (real*c.real + imag*c.imag)/(c.real*c.real + c.imag*c.imag);
  Div.imag = (imag*c.real + real*c.imag)/(c.real*c.real + c.imag*c.imag);
  return Div;
 }
    ///////////////////////////////////////////////////
 void print(){
        cout<<real<<"+"<<imag<<"i"<<endl<<endl;
 }
    ///////////////////////////////////////////////////
 double getReal() const{
  return real;
 }
    ///////////////////////////////////////////////////
 double getImag() const{
  return imag;
 }
    ///////////////////////////////////////////////////
 void setReal(double re){
  real = re;

 }
    ///////////////////////////////////////////////////
 void setImag(double im){
        imag = im;
 }
 ///////////////////////////////////////////////////

};

//***************MAIN***************************
int main()
{
 double real1,imag1,real2,imag2;

 cout<<"Enter the Real  part of First Number: ";
    cin>>real1;
 cout<<"Enter the imaginary  part of First Number: ";
 cin>>imag1;
    Complex obj1(real1,imag1);
 obj1.print();

 cout<<"Enter the Real part of Second Number: ";
 cin>>real2;
 cout<<"Enter the Imaginary part of second number: ";
    cin>>imag2;
    Complex obj2(real2,imag2);
 obj2.print();

 Complex c;
 c = obj1.add(obj2);
 cout<<"Addition is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl;
 c= obj1.sub(obj2);
 cout<<endl<<"Subtraction is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl;

 c= obj1.mult(obj2);
 cout<<endl<<"Multiplication is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl;

 c= obj1.div(obj2);
 cout<<endl<<"Division result  is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl;
 return 0;
}



program input-output
complex number class example c++ code
program output




Program images
complex number c++ code
complex numbers
complex number c++ program
c++ class complex number code


also, find more examples here C++ Examples

3 comments:

  1. #include
    using namespace std;
    struct complexn{
    int real,imaginary;

    }num1,num2,num3;
    void add(complexn,complexn);
    void Subtratction(complexn,complexn);
    void Multitplication(complexn,complexn);
    void Division(complexn,complexn);
    int main()
    {int choice;
    char choice2;
    cout<<"-----------Complex Number 1 Data-----------"<>num1.real;
    cout<<"Enter Imaginary Part\t";
    cin>>num1.imaginary;
    cout<<"\n-----------Complex Number 2 Data-----------"<>num2.real;
    cout<<"Enter Imaginary Part\t";
    cin>>num2.imaginary;
    do{
    cout<<"---------------------MENU---------------------"<>choice;
    switch(choice)
    {
    case 1:add(num1,num2);
    break;
    case 2:Subtratction(num1,num2);
    break;
    case 3:Multitplication(num1,num2);
    break;
    case 4:Division(num1,num2);
    break;
    case 5:exit(0);
    break;
    default:cout<<"INVALID INPUT"<>choice2;
    }while(choice2=='y'||choice2=='Y');
    return 0;
    }



    void add(complexn a,complexn b){
    complexn num3;
    num3.real=a.real+b.real;
    num3.imaginary=a.imaginary+b.imaginary;
    if(num3.imaginary>0)
    {
    cout<0)
    {
    cout<0)
    {
    cout<0)
    {
    cout<0)
    {
    cout<0)
    {
    cout<<num4.real<<endl;
    }
    else{cout<<num4.real<<endl;}
    }
    }

    ReplyDelete