In: Computer Science
PLEASE DO IN C++
(Math: The Complex class)
The description of this project is given in Programming Exercise
14.7 in the Chapter 14 Programming Exercise from the Book section.
If you get a logical or runtime error, please refer
https://liangcpp.pearsoncmg.com/faq.html.
Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as a string. If b is 0, it simply returns a.
Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. Also provide the getRealPart() and getImaginaryPart() functions for returning the real and imaginary part of the complex number, respectively.
Overload the operators +, -, *, /, +=, -=, *=, /=, [], unary + and -, prefix ++ and --, postfix ++ and --, <<, >>.
Overload [] so that [0] returns a and [1] returns b.
Overload the relational operators <, <=, ==, !=, >, >= by comparing the absolute values of two complex numbers.
Overload the operators +, -, *, /, <<, >>, <, <=, ==, !=, >, >= as nonmember functions.
PLEASE DO IN C++
program plan:
Define the class complex
1.Declare the global variable to hold real and imaginary values.
2.Define the constructor with zero parameter to set the value 0.
3.Define the constructor set the imaginary value to 0 and real value with parameter value
4.Define the constructor with two parameter set the real and imaginary value's with real parameter values
5.Define two getter method getImaginarypart() and getRealpart() which return the imaginary ang real global variable values.
6.define the all methods addition(),substraction() division() and multiplication() that returns the complex object with single parameter which accept the complex object
7.Display the result in the form of a+bi;
8.call the add(),sub(),mul(),divide() method using first complex class object by passing second complex object.
9. Display the values.
#include<iostream>
using namespace std;
class complex{
int real, img;
public:
complex(){
//default constructor to initialize complex number to 0+0i
real = 0; img = 0;
}
complex(int r, int i){
//parameterized constructor to initialize complex number.
real = r; img = i;
}
void setRealPart();
void setImgPart();
void getRealPart();
void getImgPart();
void display();
friend complex add(complex, complex);
friend complex sub(complex, complex);
friend complex mul(complex, complex);
friend complex division(complex, complex);
};
void complex::setRealPart(){
cout << "Enter Real part: ";
cin >> real;
}
void complex::setImgPart(){
cout << "Enter Imaginary Part: ";
cin >> img;
}
void complex::getRealPart(){
cout << "The complex number is: "<< real << "+"
<< img << "i" << endl;
}
void complex::getImgPart(){
cout << "The complex number is: "<< real << "+"
<< img << "i" << endl;
}
void complex::display(){
if(img < 0)
if(img == -1)
cout << "The complex number is: "<< real << "-i"
<< endl;
else
cout << "The complex number is: "<< real << img
<< "i" << endl;
else
if(img == 1)
cout << "The complex number is: "<< real << " +
i"<< endl;
else
cout << "The complex number is: "<< real << " + "
<< img << "i" << endl;
}
complex add(complex c1, complex c2){
complex res;
res.real = c1.real + c2.real;//addition for real part
res.img = c1.img + c2.img;//addition for imaginary part
return res;//the result after addition
}
complex sub(complex c1, complex c2){
complex res;
res.real = c1.real - c2.real;//subtraction for real part
res.img = c1.img - c2.img;//subtraction for imaginary part
return res;//the result after subtraction
}
complex mul(complex c1, complex c2){
complex res;
res.real = c1.real * c2.real;//multiplication for real part
res.img = c1.img *c2.img;//multiplication for imaginary part
return res;//the result after multiplication
}
complex division(complex c1, complex c2){
complex res;
res.real = c1.real / c2.real;//division for real part
res.img = c1.img /c2.img;//division for imaginary part
return res;//the result after division
}
main(){
complex n1(3, 2), n2(4, -3);
complex result;
result = add(n1,n2);
result.display();
result = sub(n1,n2);
result.display();
result = mul(n1,n2);
result.display();
result=division(n1,n2);
result.display();
}
Output is :
$g++ -o main *.cpp $main The complex number is: 7-i :addition The complex number is: -1 + 5i :substraction The complex number is: 12-6i :multiplication The complex number is: 0 + 0i :division
The complex number is: 48 + 1073741824i : extraction