Question

Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form a + ib. Let x = a +ib and y = c +id
0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: PLEASE LIKE THE ANSWER, IT WILL ENCOURAGE ME. IF YOU HAVE ANY DOUBTS AND QUERIES ON THIS ANSWER PLEASE FEEL FREE TO COMMENT BELOW. I WILL REPLY "ASAP" AND PLEASE DONT DISLIKE THIS ANSWER.
HOPE I ANSWERED YOUR QUESTION.
THANK YOU.........

Program:

//Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

//Complex class

class Complex
{

//private data members
private:
double realPart = 0;
double imaginaryPart = 0;
public:
Complex();
Complex(double realP);
Complex(double realP, double imagP);
Complex(const Complex&);
Complex add(const Complex&);
Complex sub(const Complex&);
Complex mult(const Complex&);
Complex div(const Complex&);
double getReal();
double getImag();
void setReal(double real);
void setImag(double imag);
void print();
};
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Complex.cpp

#include <iostream>
#include "Complex.h"
using namespace std;

//constructors

Complex::Complex(): realPart(0), imaginaryPart(0){}
Complex::Complex(double r, double i) : realPart(r), imaginaryPart(i) {}
Complex::Complex(double r) : realPart(r), imaginaryPart(0) {}

//copy constructor

Complex::Complex(const Complex& c)
{
this->realPart = c.realPart;
this->imaginaryPart = c.imaginaryPart;
}

//function to add two complex numbers

Complex Complex::add(const Complex& c2) {
Complex temp = Complex();
temp.realPart = this->realPart + c2.realPart;
temp.imaginaryPart = this->imaginaryPart + c2.imaginaryPart;
return temp;
}

//function to subtrac two complex numbers
Complex Complex::sub(const Complex& c2) {
Complex temp = Complex();
temp.realPart = this->realPart - c2.realPart;
temp.imaginaryPart = this->imaginaryPart - c2.imaginaryPart;
return temp;
}

//function to multiply two complex numbers

Complex Complex::mult (const Complex& c2) {

Complex temp = Complex();
temp.realPart = (this->realPart * c2.realPart) - (this->imaginaryPart * c2.imaginaryPart);
temp.imaginaryPart = (this->realPart * c2.imaginaryPart) + (this->imaginaryPart * c2.realPart);
return temp;
}

//function to division two complex numbers
Complex Complex::div (const Complex& c2) {

Complex temp = Complex();
temp.realPart = (this->realPart*c2.realPart + this->imaginaryPart*c2.imaginaryPart)/(c2.realPart*c2.realPart +c2.imaginaryPart*c2.imaginaryPart);
temp.imaginaryPart = (this->imaginaryPart*c2.realPart - this->realPart*c2.imaginaryPart)/(c2.realPart*c2.realPart +c2.imaginaryPart*c2.imaginaryPart);
return temp;
}

//getter functions

double Complex::getReal() {
return this->realPart;
}


double Complex::getImag() {
return this->imaginaryPart;
}

//setter functions

void Complex::setReal(double real) {
realPart = real;
}


void Complex::setImag(double imag) {
imaginaryPart = imag;
}

//print function to print complex number

void Complex::print() {
if(realPart!=0)
cout << realPart;
if(imaginaryPart!=0)
{
if(imaginaryPart==1)
cout << "+i";
else if(imaginaryPart==-1)
cout << "-i";
else if(imaginaryPart>0 && realPart!=0)
cout <<"+"<< imaginaryPart<< "i";
else
cout << imaginaryPart<< "i";
}
cout<<endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Application.cpp

#include <iostream>
#include "Complex.h"

using namespace std;

//main function
int main()
{
//create Complex objects
Complex a(1, 5);
Complex b(3,-1);
Complex c;

//testing
cout << "a = "; a.print();
cout << "b = "; b.print();
cout << "a * b = "; c = a.mult(b); c.print();
cout << "a / b = "; c = a.div(b); c.print();
cout << "a + b = "; c = a.add(b); c.print();
cout << "a - b = "; c = a.sub(b); c.print();

return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Output:

a = 1+5i
b = 3-i
a * b = 8+14i
a / b = -0.2+1.6i
a + b = 4+4i
a - b = -2+6i

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

Add a comment
Know the answer?
Add Answer to:
Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this...

  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT