C Assignment Programming Define a class for complex numbers

C++ Assignment Programming

Define a class for complex numbers with two double member variables, real and imaginary. Your class definition must include accessor, mutators, input, output, and three constructors (one default, one which receives one argument, and one receives two arguments.) overload +, - (binary and unary), *, = =. Name the program file complexnum.cpp

Solution

solution

#include<iostream>
using namespace std;

class ComplexNumbers
{
   public:
   int real;
   int imaginary;
   //default constructor
   ComplexNumbers()
   {
       real=0;
       imaginary=0;
      
   }
  
   void setReal(int x)
   {
       real=x;
   }
  
       void setImaginary(int y)
   {
       imaginary=y;
   }

int getReal()
{
    return real;
   }  
   int getImaginary()
{
    return imaginary;
   }
     
     
   //single parameterized constructor
   ComplexNumbers(int z)
   {
      real=z;
       }
         
       //two parameterized constructor
       ComplexNumbers(int r,int i)
   {
      int real=r;
       imaginary=i;
       }
         
         
  
};
int main()
{
   int real,imaginary;
   cout<<\"enter real no\"<<endl;
   cin>>real;
  
   cout<<\"enter imaginary\"<<endl;
   cin>>imaginary;
   ComplexNumbers c1(real,imaginary);
   c1.setReal(real);
   c1.setImaginary(imaginary);
  
   cout<<\"the complex no is\"<<c1.getReal()<<\"+\"<<c1.getImaginary()<<\"i\"<<endl;
}

output

enter real no
25
enter imaginary
36
the complex no is25+36i

C++ Assignment Programming Define a class for complex numbers with two double member variables, real and imaginary. Your class definition must include accessor,
C++ Assignment Programming Define a class for complex numbers with two double member variables, real and imaginary. Your class definition must include accessor,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site