Question

In: Computer Science

Write minimal code to declare a class Complex that enables operations on complex numbers, and also...

Write minimal code to declare a class Complex that enables operations on complex numbers, and also to overload the multiplication * operator.

Solutions

Expert Solution

#include<iostream>
#include<stdlib.h>
#include<string>

using namespace std;


//complex class
class complex
{
   float real;
   float img;
  
   public :

   // constructor
   complex(float r=0,float i=0)
   {
       real = r;
       img = i;
   }

   void display()
   {
       cout<<real<<"+i"<<img;
   }

   // fucntion to return the real part
   float getReal()
   {
       return real;
   }

   // function to return the imaginary part
   float getImg()
   {
       return img;
   }


   // function to over load the + operator
   complex operator + (const complex &c2)
   {
       complex dummy;
       dummy.real=real+c2.real;
       dummy.img=img+c2.img;
       return dummy;
   }
  
   // function to overload the - operator
   complex operator - (const complex &c2)
   {
       complex dummy;
       dummy.real=real-c2.real;
       dummy.img=img-c2.img;
       return dummy;
   }

   friend complex operator * (float,complex);  
   friend complex operator * (complex,float);


   //fucntion to overload the * operator for complex multiplication
   complex operator * (const complex &c)
   {
       complex dummy;
       dummy.real = real*c.real - img*c.img;
       dummy.img = c.img*real + c.real*img;
       return dummy;
   }
  
};


// complex to overload multiplication by scalar of the form
// x * (a + ib)
complex operator * (float f, complex c)
{
   complex dummy;
   dummy.real = c.real * f;
   dummy.img *= c.img * f;
   return dummy;
}


// complex to overload multiplication by scalar of the form
// (a + ib) * x
complex operator * (complex c, float f)
{
   complex dummy;
   dummy.real = c.real * f;
   dummy.img = c.img * f;
   return dummy;
}

int main()
{
   complex t1(1,2),t2(2,3);
   complex t3 = t1*2;
   t3.display();
   cout<<endl;

   t3 = t1*t2;
   t3.display();
   cout<<endl;
  
   t3=t1+t2;
   t3.display();
   cout<<endl;

   return 0;
}


Related Solutions

Write the class Complex that supports the basic complex number operations. Such operations are addition (+),...
Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-) and multiplication (*) of complex numbers, and multiplication (*) of a complex by a scalar (float or int). All methods must return (not print) the result. Class also supports the rich comparison for equality (= =) - Check the doctest for object behavior examples. - You must use the special methods for those 4 operators in order to override their behavior -...
n Java, Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have...
n Java, Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form realPart + imaginaryPart * i where i is square root of -1 Use floating-point 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. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations: a)...
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 the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Lab Objectives Be able to declare a new class Be able to write a constructor Be...
Lab Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that take arguments Be able to instantiate an object Be able to use calls to instance methods to access and change the state of an object Introduction Everyone is familiar with a television. It is the object we are going to create in this lab. First we need a...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Write the following complex numbers in polar form, as ??^??. 1/3+4i
Write the following complex numbers in polar form, as ??^??. 1/3+4i
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT