a.Seperate this into 3 parts- class specification, class member functions defini
ID: 3553508 • Letter: A
Question
a.Seperate this into 3 parts- class specification, class member functions definition and test file.
b.create a member function- multiplication for complex are defined as (a+bi) * (c+di) = (a*c-b*d) + (a*d+b*c)i
c.change the main function to ask user to enter 2 complex number and display the results for addition, subtraction, and multiplication.
d.Build and run the program
#include <iostream>
using namespace std;
class Complex {
public:
Complex( double = 0.0, double = 0.0 ); // default constructor
void addition( const Complex & );
void subtraction( const Complex & );
void printComplex( void );
void setComplexNumber( double, double );
private:
double realPart;
double imaginaryPart;
};
Complex::Complex( double real, double imaginary )
{ setComplexNumber( real, imaginary ); }
void Complex::addition( const Complex &a )
{
realPart += a.realPart;
imaginaryPart += a.imaginaryPart;
}
void Complex::subtraction( const Complex &s )
{
realPart -= s.realPart;
imaginaryPart -= s.imaginaryPart;
}
void Complex::printComplex( void )
{ cout << '(' << realPart << ", " << imaginaryPart << ')'; }
void Complex::setComplexNumber( double rp, double ip )
{
realPart = rp;
imaginaryPart = ip;
}
int main()
{
Complex b( 1, 7 ), c( 9, 2 );
b.printComplex();
cout << " + ";
c.printComplex();
cout << " = ";
b.addition( c );
b.printComplex();
cout << ' ';
b.setComplexNumber( 10, 1 ); // reset realPart and imaginaryPart
c.setComplexNumber( 11, 5 );
b.printComplex();
cout << " - ";
c.printComplex();
cout << " = ";
b.subtraction( c );
b.printComplex();
cout << endl;
return 0;
}
Explanation / Answer
File Complex.h
#ifndef __COMPLEX_H // Control inclusion of header files
#define __COMPLEX_H
#include <iostream>
using namespace std;
class Complex {
public:
Complex( double = 0.0, double = 0.0 ); // default constructor
void addition( const Complex & );
void subtraction( const Complex & );
void printComplex( void );
void multiplication(const Complex &);
void setComplexNumber( double, double );
private:
double realPart;
double imaginaryPart;
};
#endif
Complex.cpp
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::Complex( double real, double imaginary )
{
setComplexNumber( real, imaginary );
}
void Complex::addition( const Complex &a )
{
realPart += a.realPart;
imaginaryPart += a.imaginaryPart;
}
void Complex::subtraction( const Complex &s )
{
realPart -= s.realPart;
imaginaryPart -= s.imaginaryPart;
}
void Complex::printComplex( void )
{ cout << '(' << realPart << ", " << imaginaryPart << ')'; }
void Complex::multiplication(const Complex &m)
{
this->realPart=realPart*m.realPart-imaginaryPart*m.imaginaryPart;
this->imaginaryPart=realPart*m.imaginaryPart+imaginaryPart*m.realPart;
}
void Complex::setComplexNumber( double rp, double ip )
{
realPart = rp;
imaginaryPart = ip;
}
Main File
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex b( 1, 7 ), c( 9, 2 );
b.printComplex();
cout << " + ";
c.printComplex();
cout << " = ";
b.addition( c );
b.printComplex();
cout << ' ';
b.setComplexNumber( 10, 1 ); // reset realPart and imaginaryPart
c.setComplexNumber( 11, 5 );
b.printComplex();
cout << " - ";
c.printComplex();
cout << " = ";
b.subtraction( c );
b.printComplex();
cout << endl;
b.setComplexNumber( 7, 2 ); // reset realPart and imaginaryPart
c.setComplexNumber( 21, 10 );
b.printComplex();
cout << " * ";
c.printComplex();
cout << " = ";
b.multiplication( c );
b.printComplex();
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.