Question
USING C++: You have implemented a Complex class in the previous homework. In this problem, you will be implementing an Array class, with some additional
USING C++:
You have implemented a Complex class in the previous homework. In this problem, you will be implementing an Array class, with some additional features, such as dynamic memory allocation and de ning the copy constructor and the assignment operator. Please refer to Stephen Prata, Chapter 12 for details. (a) The class must have the following data members: An int for the array size. A pointer to an int for the array elements. (b) Memory management should be done dynamically using the appropriate new operator in the constructor and the delete operator in the destructor. (c) The class must also de ne the following: A constructor A copy constructor Assignment operator Addition operator A method to sort the array A method to display the array elements Test the code for all of the above functionality.
This is the code that the question is reffering to, just need to add the above:
#include "stdafx.h"
#include
#include
#include
using namespace std;
class complex {
public:
double real;
double imag;
//Declarations:
complex();
complex(double x, double y);
double Addreal(complex c1, complex c2);
double Addimaginary(complex c1, complex c2);
void display();
double MagnitudeSq();
double Magnitude();
double Polar();
complex Conjugate();
//Overloading operators:
complex operator+ (const complex & other);
complex operator- (const complex & other);
complex operator* (const complex & other);
complex operator/ (const complex & other);
bool operator== (const complex & right);
bool operator!= (const complex & right);
};
complex::complex(){
real = 0;
imag = 0;
}
complex::complex(double x, double y) {
real = x;
imag = y;
}
double complex::Addreal(complex c1, complex c2){//adds real part
complex c3;
c3.real = c1.real + c2.real;
return c3.real;
}
double complex::Addimaginary(complex c1, complex c2){//adds imaginary part
complex c3;
c3.imag = c1.imag + c2.imag;
return c3.imag;
}
void complex::display(){// displays result a+bi
cout << real << " + " << imag << "i" < cout << ' '; } double complex::MagnitudeSq(){//gets the magnitude squared[used below] double magnSq = (real*real)+(imag*imag); return magnSq; } double complex::Magnitude(){//get the magnitude double magnSq; magnSq = MagnitudeSq();//[above] double magn = sqrt(magnSq); return magn; } double complex::Polar(){//produces polar form double pol=0, phaseA; double magn, a, b; magn = Magnitude(); phaseA = atan(imag / real); a = magn * (cos(phaseA)); b = magn * (sin(phaseA)); cout << "The polar coordinates of complex: " << "(" << a << " , " << b << ")" << endl; return pol; } complex complex::Conjugate(){//conjugates the expression complex comp; comp.real = real; comp.imag = -1 * (imag); cout << "The conjugate of complex: " << comp.real << " + " << comp.imag << "i" << endl; return comp; } //Overload operator functions: complex complex::operator+ (const complex & other){ return complex(real+other.real , imag+other.imag); } complex complex::operator- (const complex & other){ return complex(real-other.real , imag-other.imag); } complex complex::operator* (const complex & other){ return complex(real*other.real , imag*other.imag); } complex complex::operator/ (const complex & other){ return complex(real/other.real, imag/other.imag); } bool complex::operator== (const complex & right){ if ((real == right.real) && (imag = right.imag)) return true; else return false; } bool complex::operator!= (const complex & right){ if ((real != right.real) && (imag != right.imag)) return true; else return false; } int main(){ using namespace std; complex c1, c2, c3, comp; double magn, pol; int s,p,t,z; //fixed given numbers: c1 = complex(1.9 , 4.2); c2 = complex(0.5 , 2.9); cout << "The real part of c1: " << c1.real << endl; cout << "The imaginary part of c1: " << c1.imag << endl; c1.display(); cout << "The real part of c2: " << c2.real << endl; cout << "The imaginary part of c2: " << c2.imag << endl; c2.display(); //user chooses desired output: while(1){ cout << "(1)Real & Imaginary (2)Magnitude (3)Polar (4)Conjugate (5)Addition (6)Subtraction (7)Muliplication (8)Division (9)Comparison (using ==) (10)Comparison (using !=)" << endl; cin >> s; switch(s){ case 1: // Addreal & Addimaginary c3.real = c3.Addreal(c1, c2); c3.imag = c3.Addimaginary(c1, c2); cout << "The complex number of c3: " << c3.real << " + " << c3.imag << "i" << endl; break; case 2: // Magnitude cout << " (1) c1 (2) c2 (3) c3" << endl; cin >> t; switch(t){ case 1: magn = c1.Magnitude(); cout << "The magnitude of c1: " << magn << endl; break; case 2: magn = c2.Magnitude(); cout << "The magnitude of c2: " << magn << endl; break; case 3: magn = c3.Magnitude(); cout << "The magnitude of c3: " << magn << endl; break; } case 3: // Polar cout << " (1) c1 (2) c2 (3) c3" << endl; cin >> z; switch(z){ case 1: pol = c1.Polar(); break; case 2: pol = c2.Polar(); break; case 3: pol = c3.Polar(); break; } case 4: // Conjugate cout << " (1) c1 (2) c2 (3) c3" << endl; cin >> p; switch(p){ case 1: comp = c1.Conjugate(); break; case 2: comp = c2.Conjugate(); break; case 3: comp = c3.Conjugate(); break; } case 5: // Addition c3=c1+c2; c3.display(); break; case 6: // Subtraction c3=c1-c2; c3.display(); break; case 7: // Multiplication c3=c1*c2; c3.display(); break; case 8: // Division c3=c1/c2; c3.display(); break; case 9: // Comparison (==) if(c1==c2){ cout << "Two complex numbers are the same" << endl; } else{ cout << "Two complex numbers are not the same" << endl; } break; case 10: // Comparison (!=) if(c1!=c2){ cout << "Two complex numbers are not the same" << endl; } else{ cout << "Two complex numbers are the same" << endl; } break; default://incase an unrecognized value is entered cout << "Value out of range, try again (1-10): " << endl; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started