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++ 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...
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...
Implement a VotingMachine class that can be used for a simple election. Include the methods to...
Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results. Write the driver code to simulate the voting. in java
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
2. Implement a decoder for the encoding scheme used in the first problem. The decoder will...
2. Implement a decoder for the encoding scheme used in the first problem. The decoder will reverse whatever operation was performed on the string, meaning each character will be rotated 3 to the left. For example, the input string def will decode to abc. Create a program which implements this decoding scheme and allows the user to input a string to be decoded. • Only use ASCII characters in range [32, 127]. • Read an entire string of input from...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold the following information about a book: title, authors, publisher, ISBN Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT