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, otherwise return false.
bool opeator!()const
rite main function to test all the implemented functionality
class Complex {
double real;
double imaginary;
public:
Complex();
Complex(double,double);
void setReal(double r);
double getReal()const;
void setImaginary(double i);
double getImaginary() const;
bool operator!()const;
virtual ~Complex();
};
I have implemente the class and the code is below.
=====================================================================
#include<iostream> using namespace std; // class to implement the complex number // It will contain the real value and imaginary value. class Complex { // variables to store the values double real; double imaginary; public: // Default Constructor to initialis the complex number with 0. Complex(){ this->real = 0; this->imaginary = 0; } //Constructor to initialise the complex number with the given value. Complex(double real,double imaginary){ this->real = real; this->imaginary = imaginary; } // Setter function which sets the real value void setReal(double r){ this->real = r; } // getter function which gets the real value. double getReal()const{ return this->real; } // setter function to set the Imaginary value. void setImaginary(double i){ this->imaginary = i; } // Getter function which gets the Imaginary value. double getImaginary() const{ return this->imaginary; } // ! operator which return true if real and imaginary part is zero otherwise false bool operator!()const{ if(this->real==0 && this->imaginary == 0){ return true; } else{ return false; } } // it sets the real and imginary part to zero after the execution of the program. virtual ~Complex(){ this->real = 0; this->imaginary = 0; } }; // main function to test the complex class. int main(){ Complex c1,c2; // declaring the object of class complex. double r,i; cout<<"Enter the Real value : "; cin>>r; cout<<"Enter the Imaginary value : "; cin>>i; c1.setReal(r); // setting the real value c1.setImaginary(i); // setting the imaginary value. cout<<"The complex number you enterd is "; // getting the value of real and imaginary part. cout<<c1.getReal()<<" + "<<c1.getImaginary()<<"i"<<endl; cout<<"If c1 has value in real and imaginary part it will print 0 otherwise 1: "; cout<<!c1<<endl; cout<<"The deafult complex number is "; cout<<c2.getReal()<<" + "<<c2.getImaginary()<<"i"<<endl; cout<<"If c2 has value in real and imaginary part it will print 0 otherwise 1: "; cout<<!c2<<endl; return 0; }
=====================================================================================
And the screenshot of my code editor is below.




And output of the above code is:

I hope this will help.
Leave a comment if you have any query or want to tell any
suggestion
If you found it helpful Up Vote it
Thank you
Design a class Complex for handling Complex numbers and include the following: _ real: a double...
C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers. is the imaginary unit with the property of . is called the real part of the complex number and is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...
Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....
Consider the following C struct that represents a complex number. struct complex { double real; double imaginary; }; (a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class: A default constructor that initializes the real and imaginary parts to 0. A constructor that allows initialization of both real and imaginary parts to any double value. A public member function that returns the magnitude of the complex number....
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...
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++ 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: 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++
//add as many comments as possible
5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...
A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...
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...