C++ Problem 1. Define a class, named ComplexNumber, that represents complex numb
ID: 673313 • Letter: C
Question
C++
Problem 1. Define a class, named ComplexNumber, that represents complex numbers. A complex number is any number a + bi, where a is the real part, and b is the imaginary part. In ComplexNumber class, define (not declare) the following inline functions (defined within the class, no separation of .h and .cpp files):
a. A default constructor that initializes the complex number as 0 + 0i.
b. An alternative constructor that takes in two double values (a and b) as arguments and initializes the complex number to a + bi.
c. A function addComplex that takes in two complex numbers (m and n) as arguments, performs complex number addition with m and n, and updates the real and imaginary parts of the complex number. Use the fact that (a + bi) + (c + di) = (a + c) + (b + d)i. Be aware the type of m and n when you define the functions, they are not double!
class ComplexNumber {
private: double realPart, imaginaryPart;
public:
//define your default constructor here
//define your alternative constructor here
//define your addComplex here
};
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
struct complex
{
float rel;
float img;
}s1,s2;
void main()
{
clrscr();
float a,b;
cout<<“Enter real and imaginary part of 1st complex number:”;
cin>>s1.rel>>s1.img;
cout<<“Enter real and imaginary part of 2nd complex number:”;
cin>>s2.rel>>s2.img;
//Addition
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<“nAddition: “<<“(“<<a<<“)”<<“+”<<“(“<<b<<“)”<<“i”;
//Subtraction
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<“nSubtraction: “<<“(“<<a<<“)”<<“+”<<“(“<<b<<“)”<<“i”;
//Multiplication
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
cout<<“nMultiplication: “<<“(“<<a<<“)”<<“+”<<“(“<<b<<“)”<<“i”;
//Division
a=(((s1.rel)*(s2.rel))+((s1.img)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
b=(((s2.rel)*(s1.img))-((s1.rel)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
cout<<“nDivision: “<<“(“<<a<<“)”<<“+”<<“(“<<b<<“)”<<“i”;
getch();
}
or
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.