Question: I need to allocate memory dynamically here , but it gives an error . Please fix this Header file #include using namespace std; class Complex{
I need to allocate memory dynamically here , but it gives an error . Please fix this
Header file
#include
using namespace std;
class Complex{ private: int real; int img;
public: Complex(){ }
Complex(int r, int i){ this->real = new int(r); this->img = new int(i); }
~Complex(){ cout << "Object is being destroyed!!" << endl; }
void setReal(int real); int getReal(); void setImg(int img); int getImg();
Complex operator + (Complex c1); Complex operator - (Complex c1); Complex operator * (Complex c1); Complex operator / (Complex c1);
Complex getConjugate(); void print(); };
main file
#include
using namespace std;
//int Complex::objects = 0;
/*Complex::Complex(int r, int i){ this->real = r; this->img = i; //objects++; }*/
void Complex::setReal(int real){ this->real = real; }
int Complex::getReal(){ return this->real; }
void Complex::setImg(int img){ this->img = img; }
int Complex::getImg(){ return this->img; }
void Complex::print(){ if (this->img > 0){ if (this->img == 1){ cout << "The complex number is: " << this->real << " + i" << endl; }
else{ cout << "The complex number is: " << this->real << " + " << this->img << "i" << endl; } }
else{ if (this->img == -1){ cout << "The complex number is: " << this->real << " - i" << endl; }
else{ cout << "The complex number is: " << this->real << " - " << -(this->img) << "i" << endl; } } }
Complex Complex::operator + (Complex c1){ int real = this->real + c1.getReal(); int img = this->img + c1.getImg();
return (Complex(real, img)); }
Complex Complex::operator - (Complex c1){ int real = this->real - c1.getReal(); int img = this->img - c1.getImg();
return (Complex(real, img)); }
Complex Complex::operator * (Complex c1){ int product1 = this->real * c1.getReal(); int product2 = this->img * c1.getImg(); int product3 = (this->real + this->img) * (c1.getImg() + c1.getReal());
int real = product1 - product2; int img = product3 - (product1 + product2);
return (Complex(real, img)); }
Complex Complex::operator / (Complex c1) { int division=(c1.real*c1.real) + (c1.img*c1.img);
Complex temp;
temp.real=(this->real*c1.real)+(this->img*c1.img); temp.real/=division; temp.img=(this->img*c1.real)-(this->real*c1.img); temp.img/=division;
return temp; }
Complex Complex::getConjugate(){ int real = this->real; int img = this->img * -1;
return (Complex(real, img)); }
using namespace std;
int main(){ int real1, real2, img1, img2;
cout << "Enter the real and imaginary part: " << endl; cin >> real1; cin >> img1;
cout << "Enter the real and imaginary part: " << endl; cin >> real2; cin >> img2;
Complex c1(real1, img1); Complex c2(real2, img2);
Complex c3(0, 0);
c1.print(); c2.print();
c3 = (c1 + c2); cout << "Addition: " << endl; c3.print(); c3 = (c1 - c2); cout << "Subtraction: " << endl; c3.print(); c3 = (c1 * c2); cout << "Multiplication: " << endl; c3.print(); c3 = (c1 / c2); cout << "Division: " << endl; c3.print();
cout << "Conjugate: " << endl; c3 = c1.getConjugate(); c3.print(); c3 = c2.getConjugate(); c3.print();
//cout << "Number of objects created: " << Complex::objects << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
