Question

Asked Oct 17, 2019

Using C++ 11

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 − 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 it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks:

- a) Adding two complex numbers: The real parts added together and the imaginary parts are added together.
**You should pass two objects to your function.** - b) Subtracting two complex numbers: The real parts subtracted together and the imaginary parts are subtracted together.
**You should pass two objects to your function.** - c) Printing Complex numbers in the form
**(a , b)**, where a is the real part and b is the imaginary part.

(Do not use **operator overloading** for this assignment.)

Step 1

This program demonstrates the complex numbers for performing arithmetic.

Program:

//Header file section

#include<iostream>

#include<stdio.h>

using namespace std;

//Define the class

class complex

{

private:

double real_part;

double img_part;

public:

complex(){ };

//Define teh constructor

complex(double r, double i)

{

real_part = r; img_part = i;

};

char *toString();

void set_comp_No(double r, double i);

complex add(complex);

complex subtract(complex);

complex multiply(complex);

};

//Define the tostring() method to convert the real part and imaginary part

char * complex::toString()

{

char *c = new char[100];

if (img_part >= 0)

sprintf(c, "%ld + %ldi", (long)real_part, (long)img_part);

else

sprintf(c, "%ld %ldi", (long)real_part, (long)img_part);

return c;

}

//Define the method to set the complex number

void complex::set_comp_No(double r, double i)

{

real_part = r; img_part = i;

}

//Define the method to add the complex number

complex complex::add(complex x)

{

complex z;

z.real_part = real_part + x.real_part;

z.img_part = img_part + x.img_part;

return z;

}

//Define the method to subtract the complex number

complex complex::subtract(complex x)

{

complex z;

z.real_part = real_part - x.real_part;

z.img_part = img_part - x.img_part;

return z;

}

//Define the method to multiply the complex number

complex complex::multiply(complex x)

{

complex z;

z.real_part = real_part * x.real_part;

z.img_part = img_part * x.img_part;

return z;

}

//Main function definition

int main()

{

//Pass the arguments

complex c1(2.0, 5.0), c2;

complex a, s, m;

c2.set_comp_No(3.0, 4.0);

//Print the complex numbers

cout << "Complex number-1 : " << c1.toString() << endl;

cout << "Complex number-2 : " << c2.toString() << endl;

//Call the add() function to add the complex number

a = c1.add(c2);

cout << "Addition of complex numbers c1 + c2 = " << a.toString() << endl;

//Call the subtract() function to subtract the complex number

s = c1.subtract(c2);

cout << "Subtraction of complex numbers c1 - c2 = " << s.toString() << endl;

//Call the multiply() function to multiply the complex number

m = c1.multiply(c2);

cout << "Multiplication of complex numbers c1 * c2 = " << m.toString() << endl;

return 0;

}

Step 2

**Screenshot#1:**

Step 3

**Screenshot #2:**

Tagged in

Q: Consider a program that declares global integer variables x, y[10]. Thesevariables are allocated sta...

A: Explanation of the code: lw $s1, 0($gp) #load value in base address to $s1addi $s1, $s1, 25 #increme...

Q: Vectors Write a for loop to print all NUM_VALS elements of vector courseGrades, following each with ...

A: Note: As the user given index value is constant and so the value of index could not be changed whil...

Q: Write a program that begins by reading a number of cents from the users as an integer (this is input...

A: Initialize all required currency named variable with 0 in python3 programming language so that the u...

Q: hello good afternoon, please, I need help with the following problem c++: ASSIGNMENT (CONTROL STRU...

A: A C++ program to display the song time and total time in a table format and a message indicating re...

Q: Create a new file (in Dev C++) In Lab 2, you created a menu for a simple calculator program. In Lab...

A: Program AlgorithmDeclare the required variables and write a welcome message before starting the menu...

Q: Discuss the roles of information in globalisation of business

A: Globalization of business:People, organization and governments are integrated or interacted word wid...

Q: Using the table structures you created (in first attachment called "Table Stucture", remove all tran...

A: Click to see the answer

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: The name of the program is CharCounter.inputEntry is a string type variable and used for storing use...

Q: What type of technique is used when an image is transferred by forcing ink through fine mesh or silk...

A: SerigraphySerigraphy also known as screen printing or silkscreen printing is a printing technique wh...