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 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)
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 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)
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....
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...
Write the following complex numbers in polar form, as ??^??. 1/3+4i
Write the following complex numbers in polar form, as ??^??. 1/3+4i
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
Write a java code to find the following. For numbers 501 to 999, how many numbers...
Write a java code to find the following. For numbers 501 to 999, how many numbers will have the sum of the digits equal to 10. 501, sum of digits=6 502, sum of digits=7 503, sum of digits=8 504, sum of digits=9 505, sum of digits=10 506, sum of digits=11
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT