construct "the type Complex"?

Hi guys,

I do not want the creation of the Imaginary object type in main

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

class Imaginary{
   double i;
   Imaginary(double ii = 0.0):i(ii){}
public:
   Imaginary operator+(const Imaginary&b)const 
   {
	   Imaginary result = *this;
	   result.i += b.i;
	   return result;
   }
   Imaginary operator-(const Imaginary& b)const
   {
	   Imaginary result = *this;
	   result.i -= b.i;
	   return result;
   }
   double operator*(const Imaginary& b) { return -1*i*b.i; }
   double operator/(const Imaginary& b)
   {
	      Imaginary result = *this;
		  result.i /= b.i;
		  return result.i;
   }

   void setImaginary(double ii)
   {
	   Imaginary i(ii);
   }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Complex{
   double a;
   Imaginary b;
public:
	Complex(double aa, Imaginary bb):a(aa), b(bb){}
   Complex operator +(const Complex& c)
   { 
	   return Complex(a+c.a, b + c.b); 
   }

  void set(double ii)
  {
	  b.setImaginary(ii);
  }
  
   //and so on.
};


1
2
3
4
5
6
7
8
int main()
{

Complex c(2.2);


return 0;
}


How can I construct "the type Complex"?
imaginary.hpp
1
2
3
4
5
6
7
8
9
#pragma once
#ifndef FIRIX_IMAGINARY_HPP
#define FIRIX_IMAGINARY_HPP

class Imaginary {
  ...
};

#endif 

complex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#ifndef FIRIX_COMPLEX_HPP
#define FIRIX_COMPLEX_HPP

#include "imaginary.hpp"

class Complex {
  double a;
  Imaginary b;
...
};

#endif 

main.cpp
1
2
3
4
5
6
7
8
9
10
#include "complex.hpp"

int main()
{

Complex c(2.2);


return 0;
}

Since the Complex class requires the Imaginary class, there is no way around it. But you don't have to have main explicitly require it.

Hope this helps.
Duoas sorry but I do not understand anything.

What exactly should I do?

Obligatory link: http://cplusplus.com/forum/articles/10627/#msg49679


Basically, complex.hpp needs to include imaginary.hpp
closed account (D80DSL3A)
Not sure why a complex class must be based on an imaginary class. I built the properties of complex numbers into the class methods and it seems to work fine.

Complex.h
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
#include<iostream>// for the Print() targeting the console
using std::cout;
using std::endl;

class Complex
{
public:
	double real;
	double imag;

	Complex(double Real, double Imaginary );
	Complex(void);
	~Complex(void);

	double magSquared(void) const;// z * z.conj()	
	void Print(void);// for convenience in printimg z

	Complex conj(void) const;// returns complex conjugate
	Complex inv(void) const;// returns 1/z
	Complex operator+(const Complex& v) const;// Complex + Complex	
	Complex operator-(const Complex& v) const;// Complex - Complex

	Complex operator*(const Complex& v) const;// Complex*Complex
	Complex operator*(const double& a) const;// Complex*double
	friend Complex operator*(const double& a, const Complex& u);// double * Complex

	Complex operator/(const Complex& v) const;// Complex / Complex
	Complex operator/(const double& a) const;// Complex / double
	friend Complex operator/(const double& a, const Complex& u);// double / Complex
};


Complex.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Complex.h"

Complex::Complex(double Real, double Imaginary)
{
	real = Real;
	imag = Imaginary;
}

Complex::Complex(void)
{
}

Complex::~Complex(void)
{
}

double Complex::magSquared(void) const
{
	return( real*real + imag*imag );
}

void Complex::Print(void)
{
	if( this->imag >= 0.0 )
		std::cout << this->real << " + " << this->imag << "i";
	else
		std::cout << this->real << " - " << -1.0*this->imag << "i";

	return;
}

Complex Complex::conj(void) const
{
	// z = conjugate of u this
	Complex z;

	z.real = this->real;
	z.imag = -1.0*this->imag;

	return z;
}

Complex Complex::inv(void) const
{
	// z = 1/this
	Complex z;
	double magSq = this->magSquared();

	z.real = this->real/magSq;
	z.imag = -1.0*this->imag/magSq;

	return z;
}

Complex Complex:: operator+(const Complex& v) const
{
	// z = u + v where u = this
	Complex z;

	z.real = this->real + v.real;
	z.imag = this->imag + v.imag;

	return z;
}// end of +

Complex Complex:: operator-(const Complex& v) const
{
	// z = u - v where u = this
	Complex z;

	z.real = this->real - v.real;
	z.imag = this->imag - v.imag;

	return z;
}// end of -

Complex Complex:: operator*(const Complex& v) const// Complex * Complex
{
	// z = u*v where u = this
	Complex z;	

	z.real = ( this->real*v.real - this->imag*v.imag );
	z.imag = ( this->imag*v.real + this->real*v.imag );

	return z;
}// end of *

Complex Complex:: operator*(const double& a) const// Complex * double
{
	// z = u*a where u = this and a = double value
	Complex z;	

	z.real = a*this->real;
	z.imag = a*this->imag;

	return z;
}// end of *

Complex operator*(const double& a, const Complex& u)// double * Complex
{
	// z = a*u
	Complex z;	

	z.real = a*u.real;
	z.imag = a*u.imag;

	return z;
}// end of *

Complex Complex:: operator/(const Complex& v) const// Complex / Complex
{
	// z = u/v where u = this
	Complex z;
	double magSq = v.magSquared();

	z.real = ( this->real*v.real + this->imag*v.imag )/magSq;
	z.imag = ( this->imag*v.real - this->real*v.imag )/magSq;

	return z;
}// end of /

Complex Complex:: operator/(const double& a) const// Complex / double
{
	// z = u/a where u = this and a = double value
	Complex z;	

	z.real = this->real/a;
	z.imag = this->imag/a;

	return z;
}// end of /

Complex operator/(const double& a, const Complex& u)// double / Complex
{
	// z = a/u
	return a*u.inv();
}// end of / 


And a little something to test it all with:
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
#include "Complex.h"
using std::cout;
using std::endl;

int main()
{
	Complex u(4.0,5.0), v(3.0,2.0);
	Complex z;

	u.Print(); cout << endl;
	v.Print(); cout << endl;

	// test the + and - operators
	z = u + v; z.Print(); cout << endl;
	z = u - v; z.Print(); cout << endl << endl;

	// test the 3 * operators
	z = u*v; z.Print(); cout << endl;
	z = v*3.0; z.Print(); cout << endl;
	z = 3.0*u; z.Print(); cout << endl << endl;

	// test the 3 / operators
	z = u/v; z.Print(); cout << endl;	
	z = 2.0/v; z.Print(); cout << endl;
	z = u/2.0; z.Print(); cout << endl << endl;

	// test other functions
	z = u.conj(); z.Print(); cout << endl;
	z = z.inv(); z.Print(); cout << endl;
	
	cout << endl;
	return 0;
}


I realize that other operators such as =, ==, <, <=, >, >= are not yet defined here. I'll add these in the future. Have I missed any others? What faults are there in my methods? (operator overloading is new to me so this was a good exercise).
thank you
Topic archived. No new replies allowed.