complex class I wrote

Hi,

complex class I wrote.
Do you think
Is there a place I do wrong?

Which functions should return a reference ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
#ifndef _IMAGINARY_HPP_
#define _IMAGINARY_HPP_

class Imaginary{
   double i;
   Imaginary(double ii = 0.0):i(ii){}
public:
  
   Imaginary(const Imaginary &r);
   Imaginary &operator=(const Imaginary &r);
   Imaginary operator+(const Imaginary &b)const;
   Imaginary operator-(const Imaginary& b)const;
   double operator*(const Imaginary& b);
   double operator/(const Imaginary& b);
   Imaginary &operator+=(const Imaginary& b);
   Imaginary &operator-=(const Imaginary& b);
   Imaginary &operator*=(const Imaginary& b);
   Imaginary &operator/=(const Imaginary& b);

   void setImaginary(double ii);
   friend class Complex;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Imaginary.hpp"
#include <iostream>




	
   Imaginary::Imaginary(const Imaginary &r)
   { 
	
    i = r.i;
	
   }
  
   Imaginary &Imaginary::operator=(const Imaginary &r)
   {
	   i = r.i;
	   return *this;
   }
	
   Imaginary Imaginary::operator+(const Imaginary&b)const 
   {
	      Imaginary result = *this;
	      result.i += b.i;
	      return result;
   }
   Imaginary Imaginary::operator-(const Imaginary& b)const
   {
	      Imaginary result = *this;
	      result.i -= b.i;
	      return result;
   }
   double Imaginary::operator*(const Imaginary& b) 
   { 
		  Imaginary result = *this;
		  result.i *= b.i;
		  return result.i;
   }
   double Imaginary::operator/(const Imaginary& b)
   {
	      Imaginary result = *this;
		  result.i /= b.i;
		  return result.i;
   }
    Imaginary &Imaginary::operator+=(const Imaginary& b)
	{
		this->i += b.i;
		return *this;
	}


	 Imaginary &Imaginary::operator-=(const Imaginary& b)
	{
		this->i -+ b.i;
		return *this;
	}


	  Imaginary &Imaginary::operator*=(const Imaginary& b)
	{
		this->i *+ b.i;
		return *this;
	}


	   Imaginary &Imaginary::operator/=(const Imaginary& b)
	{
		this->i /= b.i;
		return *this;
	}
   void Imaginary::setImaginary(double ii)
   {
	   Imaginary i(ii);
   }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#ifndef _COMPLEX_HPP_
#define _COMPLEX_HPP_

#include "Imaginary.hpp"


class Complex{
   double a;
   Imaginary b;
public:
   Complex(double aa, double bb);
   Complex(const Complex &r);
   Complex &operator=(const Complex &r);
   Complex operator +(const Complex& c)const;
   Complex operator -(const Complex& c)const;
   Complex operator *(const Complex& c)const;
   Complex operator /(const Complex& c)const;



   void set(double ii);
   void display()const;
  
   
   //and so on.
};


   bool operator==(const Complex &r1, const Complex  &r2);
   bool operator!=(const Complex &r1, const Complex  &r2);
   bool operator>=(const Complex &r1, const Complex  &r2);
   bool operator<=(const Complex &r1, const Complex  &r2);
   bool operator<(const Complex &r1, const Complex  &r2);
   bool operator>(const Complex &r1, const Complex  &r2);


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Complex.hpp"
#include "Imaginary.hpp"
#include <iostream>





  Complex::Complex(double aa, double bb)
  {
	  a = aa;
	  b.i = bb;
  }

  Complex::Complex(const Complex &r)
  {
	  a = r.a;
	  b = r.b;

  }
  Complex &Complex::operator=(const Complex &r)
  {
	    a = r.a;
		b = r.b;
		return *this;

  }

  Complex Complex::operator+(const Complex& c)const
  {
		Complex result = *this;
		result.a += c.a; 	
		result.b += c.b;
		return result;
  }


   Complex Complex::operator*(const Complex& c)const
  {
		Complex result = *this;
		result.a *= c.a, result.b *= c.b;
		return result;
  } 
   
   Complex Complex::operator/(const Complex& c)const
  {
		Complex result = *this;
		result.a /= c.a, result.b /= c.b;
		return result;
  }

    Complex Complex::operator-(const Complex& c)const
  {
		Complex result = *this;
		result.a -= c.a, result.b -= c.b;
		return result;
  }


  void Complex::set(double ii)
  {
	  b.setImaginary(ii);
  }
  
  void Complex::display()const
  {
	  std::cout << "(" << a  << "," << b.i << ")" << std::endl;
  }

   //and so on.

Last edited on
setImaginary is wrong.

It is very strange that, given two Imaginary instance a and b,
the type of the expression a * b is double, but the type of
the expression a *= b is Imaginary. operator* and operator/
should return Imaginary instances.

Both copy constructors are superfluous. The compiler
gives you that implementation for free.


Do you think now is a mistake?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
#ifndef _IMAGINARY_HPP_
#define _IMAGINARY_HPP_

class Imaginary{
   double i;
   
public:
   Imaginary(double ii = 0.0):i(ii){}
   Imaginary(const Imaginary &r);  // do not have a copy constructor.
   Imaginary &operator=(const Imaginary &r);
   Imaginary operator+(const Imaginary &b)const;
   Imaginary operator-(const Imaginary& b)const;
   Imaginary operator*(const Imaginary& b);
   Imaginary operator/(const Imaginary& b);
   Imaginary &operator+=(const Imaginary& b);
   Imaginary &operator-=(const Imaginary& b);
   Imaginary &operator*=(const Imaginary& b);
   Imaginary &operator/=(const Imaginary& b);

   void setImaginary(double ii);
   friend class Complex;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Imaginary.hpp"
#include <iostream>




   Imaginary &Imaginary::operator=(const Imaginary &r)
   {
	   i = r.i;
	   return *this;
   }
	
   Imaginary Imaginary::operator+(const Imaginary&b)const 
   {
	      Imaginary result = *this;
	      result.i += b.i;
	      return result;
   }
   Imaginary Imaginary::operator-(const Imaginary& b)const
   {
	      Imaginary result = *this;
	      result.i -= b.i;
	      return result;
   }
   Imaginary Imaginary::operator*(const Imaginary& b) 
   { 
		  Imaginary result = *this;
		  result.i *= b.i;
		  return result.i;
   }
   Imaginary Imaginary::operator/(const Imaginary& b)
   {
	      Imaginary result = *this;
		  result.i /= b.i;
		  return result.i;
   }
    Imaginary &Imaginary::operator+=(const Imaginary& b)
	{
		this->i += b.i;
		return *this;
	}


	 Imaginary &Imaginary::operator-=(const Imaginary& b)
	{
		this->i -+ b.i;
		return *this;
	}


	  Imaginary &Imaginary::operator*=(const Imaginary& b)
	{
		this->i *= b.i;
		return *this;
	}


	   Imaginary &Imaginary::operator/=(const Imaginary& b)
	{
		this->i /= b.i;
		return *this;
	}

	void Imaginary::setImaginary(double ii)
	{
	   this->i = ii;
   }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#ifndef _COMPLEX_HPP_
#define _COMPLEX_HPP_

#include "Imaginary.hpp"


class Complex{
   double a;
   Imaginary b;
public:
   Complex(double aa, double bb);
   Complex(const Complex &r);// do not have a copy constructor.
   Complex &operator=(const Complex &r);
   Complex operator +(const Complex& c)const;
   Complex operator -(const Complex& c)const;
   Complex operator *(const Complex& c)const;
   Complex operator /(const Complex& c)const;



   void set(double ii);
   void display()const;
  
   
   //and so on.
};


   bool operator==(const Complex &r1, const Complex  &r2);
   bool operator!=(const Complex &r1, const Complex  &r2);
   bool operator>=(const Complex &r1, const Complex  &r2);
   bool operator<=(const Complex &r1, const Complex  &r2);
   bool operator<(const Complex &r1, const Complex  &r2);
   bool operator>(const Complex &r1, const Complex  &r2);


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Complex.hpp"
#include "Imaginary.hpp"
#include <iostream>





  Complex::Complex(double aa, double bb)
  {
	  a = aa;
	  b.i = bb;
  }

  Complex &Complex::operator=(const Complex &r)
  {
	    a = r.a;
		b = r.b;
		return *this;

  }

  Complex Complex::operator+(const Complex& c)const
  {
		Complex result = *this;
		result.a += c.a; 	
		result.b += c.b;
		return result;
  }


   Complex Complex::operator*(const Complex& c)const
  {
		Complex result = *this;
		result.a *= c.a, result.b *= c.b;
		return result;
  } 
   
   Complex Complex::operator/(const Complex& c)const
  {
		Complex result = *this;
		result.a /= c.a, result.b /= c.b;
		return result;
  }

    Complex Complex::operator-(const Complex& c)const
  {
		Complex result = *this;
		result.a -= c.a, result.b -= c.b;
		return result;
  }


  void Complex::set(double ii)
  {
	  b.setImaginary(ii);
  }
  
  void Complex::display()const
  {
	  std::cout << "(" << a  << "," << b.i << ")" << std::endl;
  }

   //and so on.

Why you need the Imaginary class at all? it seems like a typedef double Imaginary will be better.
the Complex::operator* is wrong, that is not how you multiply two complex numbers
@ne555
"Operator *" where the wrong ??


Complex Complex::operator*(const Complex& c)const
{
Complex result = *this;
result.a *= c.a, result.b *= c.b;
return result;
}
 (1 +j 3 ) * (2 -j 5 ) = 1*2 - (3*(-5)) +j ( 3*2+1*(-5) ) = 17 +j 1

But you are doing
(1 +j 3 ) * (2 -j 5 ) = 1*2 + j(3*(-5)) = 2 - j 15

I am using
result of the operation is correct


it is incorrect.the following must be wrong


Complex Complex::operator/(const Complex& c)const
{
Complex result = *this;
result.a /= c.a, result.b /= c.b;
return result;
}
Last edited on
Topic archived. No new replies allowed.