Question

C++ // Need to create a class which contains and operates on complex numbers. A complex...

C++ // Need to create a class which contains and operates on complex numbers. A complex number is defined as “a number that can be expressed in the form a + bi, where a and b are real numbers, and i is imaginary” a is called the real part and b is called the imaginary part. The class MyComplexClass will store the complex number as separate real part and imaginary part values.

MyComplexClass Private Member Variables

*One double for the real part.

*One double for the imaginary part.

*bool isWhole() Function that returns true if both the real and imaginary part are whole numbers (ie no decimal component).

MyComplexClass Public Member Variables

*int findGCD() - Returns the greatest common divisor (GCD) of the real part and imaginary part only when the real part and imaginary part are whole numbers. If the real part or imaginary part are not whole it returns 0. The GCD can be determined for whole numbers n1 and n2 with this logic:

IF n1 is negative
Convert n1 to positive
IF n2 is negative
Convert n2 to positive
FOR  i = 1; i <= n1 && i <= n2; ++i
IF n1 % i == 0 && n2 % i == 0
gcd = i
END IF
END FOR

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
using namespace std;

class MyComplexClass
{
   double real;
   double img;
   bool isWhole()
   {
       if(abs(real-int(real))==0 and abs(img-int(img))==0) /// check for absence of decimal part
           return(true); // in case of absence of decimal , its actual and int value will be equal
       else // and difference will be zero and it should be applicable for both return(false);   //data  members
   }
   public:
   MyComplexClass() // constructor ... you can skip this part as you wish
   {
       real=0.0;
       img=0.0;
   }
   void setValue(double a,double b) // since both member variables are private , we must have a public function
   { // to set its values .
       real=a;
       img=b;
   }
  
   int findGCD()
   {
       if(isWhole()==false) // isWhole() is a private member function but a public member function can call
           return(0); // any private member function
       else
       {   
           int a=real; // we don't want to change the actual complex number . so taking two separate
           int b=img; // variables to find GCD
           if(a<0) // Rest part is according to the given algorithm
               a=-a;
           if(b<0)
               b=-b;
           int gcd;
           for(int i=1;i<=a and i<=b;++i)
           {
               if(a%i==0 and b%i==0)
                   gcd=i;
           }
           return(gcd);
       }
   }
};
int main()
{
   MyComplexClass temp;
   temp.setValue(10,25.0);
   cout<<temp.findGCD()<<endl;
   return(0);
}
// the Output in this case is : 5
  
          

Add a comment
Know the answer?
Add Answer to:
C++ // Need to create a class which contains and operates on complex numbers. A complex...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex...

    C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √ −1. You should represent a complex number here as two values of type double. You should name the variables real and imaginary. You can call the...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Design a class Complex for handling Complex numbers and include the following: _ real: a double...

    Design a class Complex for handling Complex numbers and include the following: _ real: a double _ imaginary: a double The class has the following member functions. a. A constructor initializing the number with default parameters. b. Getters and Setters of the class data members as given below _ void setReal(double r) _ double getReal()const _ void setImaginary(double i) _ double getImaginary() const d. Overload unary ! operator which returns true if the real and the imaginary parts are zero,...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write...

    USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √−1. You should represent a complex number here as two values of type double. You should name the variables real...

  • Create a class for working with complex numbers. Only 2 private float members are needed, the...

    Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT