In: Computer Science
C++ PLEASE
This will be a multi-part assignment. Later homework will expand upon what is developed in this assignment.
1. Create a class called MyComplex to represent complex numbers. Each part (Real and Imaginary parts) should be stored as a double. Data members should be private and you need to supply appropaiate accessor and mutator methods.
2. Supply a default constructor (both parts zero), a constructor that allows the real part to be set and a third constructor that allows both parts to be set.
3. Supply an insertion operator that will allow the value of a complex number to be printed. The output should look like: 2+3i, 1-4i, -2+0i, 0+5i for example.
4. Supply a complete program that exercises your class. (be sure you also exercise the accessor and mutator functions). You must supply a listing of your program and sample output.
//Programm is as below-
#include<iostream>
using namespace std;
class MyComplex{
private:
//private data members
double realPart;
double imaginaryPart;
public:
//defaukt constructor
MyComplex(){
realPart=0;
imaginaryPart=0;
}
//constructor which is setting only real part and hence
imaginary part is zero
MyComplex(double real){
this->realPart=real;
this->imaginaryPart= 0;
}
//constructor which is setting both the real and imaginary
part
MyComplex(double real, double
imaginary){
this->realPart =real;
this->imaginaryPart = imaginary;
}
//Accesor
double getReal(){
return
realPart;
}
double getImaginary(){
return
imaginaryPart;
}
//Mutator
void setRealPart(double
real){
this->realPart = real;
}
void setImaginaryPart(double
imaginary){
this->imaginaryPart = imaginary;
}
//display function which will display the complext number
void display(){
if(imaginaryPart
>= 0){
cout << realPart << "+"
<<imaginaryPart<< "i"<<"\n";
}else{
cout << realPart
<<imaginaryPart<< "i"<<"\n";
}
}
};
int main(){
//creating object of MyComplex class and displaying
MyComplex complexNumber(2,3);
complexNumber.display();
MyComplex complexNumber2(1,-4);
complexNumber2.display();
MyComplex complexNumber3(-2);
complexNumber3.display();
MyComplex complexNumber4(0,5);
complexNumber4.display();
}
// executable screen shot is as belowscreen shot is as below