Question

Consider the following C struct that represents a complex number. struct complex {    double real;    double...

Consider the following C struct that represents a complex number.

struct complex {

   double real;

   double imaginary;

};

(a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class:

  • A default constructor that initializes the real and imaginary parts to 0.
  • A constructor that allows initialization of both real and imaginary parts to any double value.
  • A public member function that returns the magnitude of the complex number.
  • A public member function that will accumulate another complex number into the object, for example, in pseudocode its equivalent to obj += other. (A note for those of you reading ahead, I’m not looking for operator overloading here.)

(b) [10 points] Implement a main function that demonstrates how to use your class. Your code must exercise all parts of the definition from part (a).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program to implement a class to represent Complex number
//and perform operations
#include <iostream>
#include <math.h>

using namespace std;

//(a) Class to encapsulate Complex number features
class complex
{
//Private member variables
double real;
double imaginary;
  
//Public member features
public:
//Default Constructor
complex()
{
//Initialize real and imaginary parts to 0
real = 0;
imaginary = 0;
}
  
//Constructor that allows initialization to any double value,
//Parameterized Constructor
complex(double rVal, double iVal)
{
//Initialize real and imaginary part to passed values
real = rVal;
imaginary = iVal;
}
  
//member function that returns the magnitude of complex number
double getMagnitude()
{
/*
Magnitude of a complex number (a+bi) is represented by the
distace from the origin in an Argand diagram
that is using Pythagoras theorem, square root of (a^2 + b^2)
*/
//Calculate magnitude
double magnitude = sqrt((real*real) + (imaginary*imaginary));
  
//Return the calculated magnitude
return magnitude;
}
  
//member function that will accumulate another complex number into the object
complex accObject(const complex& other)
{
//Add each part of the obeject in a temporary object
complex temp;
temp.real = this->real + other.real;
temp.imaginary = this->imaginary + other.imaginary;
  
//return the temporary object
return temp;
}
};

//(b) Driver function main()
int main()
{
//Create an object using default constructor
complex cNum1;
  
//Create an object by passing some values ot the constructor
complex cNum2(5, -5);
  
//Calculate and print magnitude of each object
cout<<"Magnitude of first complex number: "<<cNum1.getMagnitude()<<endl;
cout<<"Magnitude of second complex number: "<<cNum2.getMagnitude()<<endl;

//Accumulate one number into another
complex accNum = cNum1.accObject(cNum2);
  
//Calculate and print the magnitude of newly formed number
cout<<"Magnitude of accumulated complex number: "<<accNum.getMagnitude()<<endl;
return 0;
}

Screenshot of Output:

Add a comment
Know the answer?
Add Answer to:
Consider the following C struct that represents a complex number. struct complex {    double real;    double...
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
  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • 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...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • 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...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. 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’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

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

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart 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: Adding two Complex...

  • 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...

  • 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...

  • need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re,...

    need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re, im; // the real and imaginary parts of a complex number }; typedef struct _cplx Complex; // Initializes a complex number from two real numbers Complex CmplxInit(double re, double im) Complex z = { re, im }; return z; // Prints a complex number to the screen void CmplxPrint(Complex z) // Not printing a newline allows this to be printed in the middle of...

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