Question
This is a c++ problem, please help me write both Matrix.cpp and main.cpp file Implement a class Matrix( Matrix is of type Complex not
This is a c++ problem, please help me write both Matrix.cpp and main.cpp file
Implement a class Matrix( Matrix is of type "Complex " not int, not double, not float)
The Matrix.h header file is provide with member function and variable
Write the Matrix.cpp file where you'll defne each function clearly with comment next to the code
Everything in Matrix.cpp should be written according to informations inside Matrix.h, please do not attempt to change"void setData(int, int, Complex)" function, it's already written
Finally write a main function to test the above program
Notice: this is an obeject oriented on Matrix implementation in C++ and you should add assignment operator("=")
-------------------------------------------------------------Matrix.h-----------------------------------------------------------------
#ifndef MATRIX_H
#define MATRIX_H
#include
using namespace std;
class Matrix {
private:
Complex ** data; // A two-dimensional double matrix
int row; // save data row
int col; // save data col
void setUp(int, int); // utility function, Set row and col value
public:
// default constructor, No argument passed, set it to 2*2 double data matrix with all values been 0
Matrix();
// constructor, set matrix size
Matrix(int, int);
// constructor, Save the incoming one-dimensional array as a two-dimensional array into data
Matrix(int, int, Complex [], int);
// copy constructor
Matrix(const Matrix &);
// destructor, clean up data
~Matrix();
void setData(int, int, Complex); // set each position (r,c)its with a data value
int getRow() const; // return private member: row
int getCol() const; // return private member: col
Complex getData(int, int) const; // Get value inserted in position (r,c) of matrix
Matrix add(const Matrix &); // Add two matrices, return the sum
Matrix multiply(const Matrix &);//Multiply two matrices, return the product
Matrix transpose(const Matrix &);// Transpose a matrix and return its transpose
void displayData() {// Print matrix data (Dont modify)
for (int i = 0;i
for (int j = 0;j
cout<
}
cout<<""<
}
}
};
#endif
------------------------------------------Complex.h------------------------------------------
#ifndef MY_COMPLEX
#define MY_COMPLEX
#include
#include
#include
#include
using namespace std;
namespace myComplex
{
class Complex
{
double real;
double imaginary;
public:
// default constructor
Complex();
// overloaded constructor
Complex(double real);
// overloaded constructor
Complex(double real, double imaginary);
// copy constructor
Complex(const Complex& ob);
// destructor
~Complex();
// getter methods
double getReal();
double getImaginary();
// setter methods
void setReal(double real);
void setImaginary(double imaginary);
// returns the complex modulus (or complex norm) of the object
double Modulus();
Complex Conjugate();
Complex add(Complex ob);
Complex multiply(Complex ob);
Complex divide(Complex ob);
string toString();
void print();
};
}
#endif
-------------------------------------------------Complex.cpp--------------------------------------------------
#include "Complex.h"
using namespace myComplex;
// default constructor
Complex::Complex()
{
real = 1;
imaginary = 0;
}
// overloaded constructor
Complex::Complex(double real)
{
this->real = real;
this->imaginary = 0;
}
// overloaded constructor
Complex::Complex(double real, double imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
// copy constructor
Complex::Complex(const Complex& ob)
{
this->real = ob.real;//ob.getReal();
this->imaginary = ob.imaginary;//ob.getImaginary();
}
// destructor
Complex::~Complex()
{
}
// getter methods
double Complex::getReal()
{
return this->real;
}
double Complex::getImaginary()
{
return this->imaginary;
}
// setter methods
void Complex::setReal(double real)
{
this->real = real;
}
void Complex::setImaginary(double imaginary)
{
this->imaginary = imaginary;
}
// returns the complex modulus (or complex norm) of the object
double Complex::Modulus()
{
return sqrt(real * real + imaginary * imaginary);
}
Complex Complex::Conjugate()
{
// create a new Complex object
Complex ans;
ans.setReal(this->getReal());
ans.setImaginary(-this->getImaginary());
return ans;
}
Complex Complex::add(Complex ob)
{
// create a new Complex object
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(this->getReal() + ob.getReal());
// compute the imaginary part of the resulting complex number
ans.setImaginary(this->getImaginary() + ob.getImaginary());
return ans;
}
Complex Complex::multiply(Complex ob)
{
// create a new Complex object
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(this->getReal() * ob.getReal() - this->getImaginary() * ob.getImaginary());
// compute the imaginary part of the resulting complex number
ans.setImaginary(this->getReal() * ob.getImaginary() + this->getImaginary() * ob.getReal());
return ans;
}
Complex Complex::divide(Complex ob)
{
// get the modulous of ob
int mod = ob.Modulus();
if(mod == 0)
throw "Error: Cannot divide by zero.";
// create a new Complex object which is complement of ob
Complex comp = ob.Conjugate();
// get a new Complex number which is the product of the current
// object and the complement of ob
Complex temp = this->multiply(comp);
// create a new Complex object which is the required ans
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(temp.getReal() / mod);
// compute the imaginary part of the resulting complex number
ans.setImaginary(temp.getImaginary() / mod);
return ans;
}
string Complex::toString()
{
stringstream x;
x<<this->getReal();
string ans = x.str();
if(this->getImaginary() > 0)
ans += " + ";
else
ans += " - ";
stringstream y;
y<<abs(this->getImaginary());
ans += y.str() + "i";
return ans;
}
void Complex::print()
{
cout<<this->toString();
}
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