Classes and Object 1. Write the class definition for a class called complex. Com
ID: 3623965 • Letter: C
Question
Classes and Object1. Write the class definition for a class called complex.
Complex numbers have the form
realPart + imaginaryPart * i
where I is the square of (-1)
Use double variable 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 of 0 in case no initializers are provided. Provide public member functions that perform the following tasks:
a). setComplex function to set complex number to set the real part and imaginary part of the complex number
b). displayComplex function to print complex number in the form (a, b), where a is the real part and b is the imaginary part.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{public:
void displayComplex();
void setComplex(double,double);
Complex();
Complex(double,double);
private:
double real,imag;
};
Complex::Complex()
{setComplex(0.,0.);
}
Complex::Complex(double r,double i)
{setComplex(r,i);
}
void Complex::setComplex(double r,double i)
{real=r;
imag=i;
}
void Complex::displayComplex(void)
{cout<<fixed<<setprecision(2)<<"("<<real<<","<<imag<<"i) ";
}
int main()
{
Complex a,b,c(5,3);
b.displayComplex();
c.displayComplex();
system("pause");
return 0;
}