Question

In: Computer Science

Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...

Problem 2 (C++):

Design and implement a class complexType, that can be used to process complex numbers.

A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib.

Complex numbers can also be represented as ordered pairs (a, b).

The class you will design should have the following features.

Constructor

Your class should have a default constructor as well as a parametrized constructor (which requires 2 double arguments).

Accessor functions required

Your class should implement the following member functions:

setComplex – set the real and imaginary parts of the complex number

getComplex – get the real and imaginary parts complex number

Operator Overloading

The class must overload and implement the following operators:

Stream I/O

  • Extend the definition of the class complexType so that it overloads stream insertion and extraction operators

To output a complex number in the form (a, b)

in which a is the real part and b is the imaginary part, the algorithm is as follows:

  1. Output the left parenthesis, (.
  2. Output the real part.
  3. Output the comma and a space.
  4. Output the imaginary part.
  5. Output the right parenthesis, ).

The algorithm to read this complex number is as follows:

  1. Read and discard the left parenthesis.
  2. Read and store the real part.
  3. Read and discard the comma.
  4. Read and store the imaginary part.
  5. Read and discard the right parenthesis.
  • + and -: Extend the class to perform addition addition and subtraction operations. Overload the operators addition and subtraction for this class as member functions and define the functions.

If (a, b) and (c, d) are complex numbers:

(a, b) + (c, d) = (a + c, b + d)

(a, b) - (c, d) = (a - c, b - d)

  • * and /: Extend the class to perform the multiplication and division operations. Overload the operators multiplication and division for this class as member functions and define the functions.

If (a, b) and (c, d) are complex numbers:

((a, b) * (c, d) = ac +ad + bc +bd

If (c, d) is nonzero:

(a, b)/(c, d)=((ac +bd)/(c2 +d2 ), (-ad +bc)/( c2 +d2))

  • =: The class must implement = assignment statement as well.

Solutions

Expert Solution

C++ code

#include<iostream>
using namespace std;

class complexType{
   private:
       double real,imag;
      
   public:
       complexType(){
           real = 0;
           imag = 0;
       }
       complexType(double r , double i){
           real = r;
           imag = i;
       }
      
       void setReal(double r){
           real = r;
       }
       void setImag(double i){
           imag = i;
       }
       double getReal(){
           return real;
       }
       double getImag(){
           return imag;
       }
      
       complexType& operator+(const complexType& other){
           double r = real + other.real;
           double i = imag + other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator-(const complexType& other){
           double r = real - other.real;
           double i = imag - other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator*(const complexType& other){
           double r = real * other.real - imag*other.imag;
           double i = imag*other.real + real*other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator/(const complexType& other){
           double val = (other.real* other.real + other.imag*other.imag);
           double r = (real * other.real + imag*other.imag)/val;
           double i = (imag*other.real - real*other.imag)/val;
          
           return *(new complexType(r , i));
       }
      
       friend ostream& operator << (ostream& out , const complexType& other){
           out<<"("<<other.real<<", "<<other.imag<<")";
           return out;
       }
      
       friend istream& operator >> (istream& in , complexType& other){
           char ch;
           in>>ch>>other.real>>ch>>other.imag>>ch;
           return in;
       }
};

int main(){
   complexType c1(2,3);
   complexType c2,c3;
   cin>>c2;
   c3 = c1 + c2;
   cout<<"c1 + c2 = "<<c3<<endl;
  
   c3 = c1 - c2;
   cout<<"c1 - c2 = "<<c3<<endl;
  
   c3 = c1 * c2;
   cout<<"c1 * c2 = "<<c3<<endl;
  
   c3 = c1 / c2;
   cout<<"c1 / c2 = "<<c3<<endl;
  
   return 0;
  
}


Related Solutions

Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function void sort(int arr[ ], int size) which, when overridden, will sort the array by calling...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Problem: Design and write a C language program that can be used as a unit converter...
Problem: Design and write a C language program that can be used as a unit converter application. Your unit converter should contain at least four unit categories, for example: length, mass, temperature, and time. The program should display the main menu that contains unit categories that are available, and the user will be prompted to select a unit category first. After the unit category has been selected the program should then display another menu (i.e., a submenu) that contains at...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final fields of type double to store the real and imaginary parts. Try to use constructor chaining to implement 2 of the 3 required constructors. If you cannot complete one or more of the methods, at least make sure that it returns some value of the correct type; this will allow the tester to run, and it will make it much easier to evaluate your...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes Tree inherits from Plant and adds a height attribute Flower inherits from Plant and adds a color attribute A driver file to test the 3 classes above. The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set...
Use C++ write a "Design and implement a class of infix calculators" ,simply write a function...
Use C++ write a "Design and implement a class of infix calculators" ,simply write a function named "evaluateInfix()" that evaluates infix expressions. It should have one string parameter and should return an int result. It should call a separate function named "infixToPostfix()" to convert the infix expression into a postfix expression, and then it should do the work of evaluating the resulting postfix expression. Then write a main() function to thoroughly test the function. Use the pseudocode algorithm that evaluates...
In this problem you will design and implement C++ code that identifies overlap in strings. Specifically,...
In this problem you will design and implement C++ code that identifies overlap in strings. Specifically, design and implement a C++ program that does the following: 1. Asks a user to input a filename and then opens that file. If the file open fails, then print the message “Unable to open file” and terminate the program using exit(1). 2. Reads the file contents, in order, into an array of strings. (See the file format explanation below.) 3. Computes the string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT