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 -...
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.
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....
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
Write the following complex numbers in polar form, as ??^??. 1/3+4i
Write the following complex numbers in polar form, as ??^??. 1/3+4i
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT