Link to home
Create AccountLog in
Avatar of Or_A
Or_AFlag for Israel

asked on

Implementation of the operator "<" for a class of complex numbers

Hi,

I need to write a program  in c++ that gets parameters of kind :int,string or complex from the user,
(not all of them in one time), and then builds a sorted vector (lets say from the smallest to the largest member) of these paramters and prints it.
i allready made a template which recieves typename T (can be int,string or complex), builds the appropriate vector according to the user's input, and i've used the function "sort" to sort the vector.
now i need to bulid a class complex.h which recieves multiple complex number inputs in the following format:
"(double(real),double(image))"
and sorts the vector of these complex numbers (a size of a number is defind by
sqrt((real)^2+(image)^2))  

how do i  implemant the operator < in the class?

(if it helps, attached are the 3 codes (in txt format because i can't send it zipped) - main.cpp,complex.h,sortedvector.h)

thanks!
Copy-of-complex.h.txt
Copy-of-main.cpp.txt
Copy-of-sortedvector.h.txt
Avatar of Infinity08
Infinity08
Flag of Belgium image

an operator< is implemented as follows :
class Test {
  public :
    bool operator<(const Test &t);
};
 
bool Test::operator<(const Test &t) {
    // code the compare *this and t here, and return true if *this < t
}

Open in new window

SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Or_A

ASKER

thank you very much, but i didn't understand how u compare between the size of *this and the size of t when size=the complex numbers' modulo.
where do i nedd to define that and how?
>> but i didn't understand how u compare between the size of *this and the size of t when size=the complex numbers' modulo.
>> where do i nedd to define that and how?

Take a look at this part of the assignment :

>> (a size of a number is defind by sqrt((real)^2+(image)^2))  

This shows which values to compare in order to find out which of two complex number is the smallest. In other words, calculate that value for both complex numbers, and then compare the two values, and return whether the first is smaller than the second.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>> See my example code below.

I wouldn't call that example code. I'd call it full code, which is not how we do things here when the question is about an assignment. On EE, we are not permitted to solve assignments - we are however permitted to guide the asker in doing the assignment himself by answering specific questions, giving hints, etc.


@Or_A : please ignore isprabu's post, and try to do this yourself instead based on my explanation. If anything is still unclear, then do not hesitate to ask :)