How do I define a class Imaginary???

Hi friends,

Define a class Imaginary to represent imaginary numbers. Define class Complex based on that.Implement the fundamental arithmetic operators.

This class Implements Complex Numbers and it's Functions.
I define a class of complex, but I not define a class Imaginary.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class Complex{
	double    r, i;
public:
    Complex(double r_val = 0.0, double i_val = 0.0):r(r_val), i(i_val){}

	Complex(const Complex &g)
	{
		r = g.r;
		i = g.i;
	}

	Complex &operator=(const Complex &g)
	{
		r = g.r;
		i = g.i;
		return *this;
	}

	Complex &operator+=(const Complex &g)
	{
		this->i += g.i;
		this->r += g.r;

		return *this;
	}
	Complex &operator-=(const Complex &g)
	{
		this->i -= g.i;
		this->r -= g.r;

		return *this;

	}
	Complex &operator*=(const Complex &g)
	{
		this->i *= g.i;
		this->r *= g.r;

		return *this;

	}
	
	Complex &operator/=(const Complex &g)
	{
		this->i /= g.i;
		this->r /= g.r;

		return *this;

	}




    void set(double r_val, double i_val)
    {
        r = r_val; i = i_val;
    }

    void get(double &r_val, double &i_val) const
    {
        r_val = r; i_val = i;
    }

	double get_real() const 
	{ 
		return r; 
	}
    double get_imaginary() const 
	{ 
		return i; 
	}

	Complex operator+(const Complex &a)
	{
		return Complex(this->get_real() + a.get_real(),this->get_imaginary() + this->get_imaginary());
	}

	Complex operator-(const Complex &a)
	{
		return Complex(this->get_real() - a.get_real(),this->get_imaginary() - this->get_imaginary());
	}

		Complex operator*(const Complex &a)
	{
		return Complex(this->get_real() * a.get_real(),this->get_imaginary() * this->get_imaginary());
	}

	Complex operator/(const Complex &a)
	{
		return Complex(this->get_real() / a.get_real(),this->get_imaginary() / this->get_imaginary());
	}


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

 friend istream &operator>>(istream &is, Complex &value);



};




bool operator==(const Complex &r1, const Complex  &r2)
{
	if (r1.i ==  r2.i &&  r1.r == r2.r)
		return true;
	else 
		return false;

}
bool operator!=(const Complex &r1, const Complex  &r2)
{
	if (r1.i !=  r2.i &&  r1.r != r2.r)
		return true;
	else 
		return false;


}
bool operator<(const Complex &r1, const Complex  &r2)
{
	
	if (r1.i <  r2.i && r1.r < r2.r)
		return true;
	else 
		return false;

}
bool operator>(const Complex &r1, const Complex  &r2)
{
	
	if (r1.i >  r2.i &&  r1.r > r2.r)
		return true;
	else 
		return false;


}
bool operator<=(const Complex &r1, const Complex  &r2)
{
	
	if (r1.i <=  r2.i &&  r1.r <= r2.r)
		return true;
	else 
		return false;


}
bool operator>=(const Complex &r1, const Complex  &r2)
{
	
	if (r1.i >=  r2.i &&  r1.r >= r2.r)
		return true;
	else 
		return false;


}


ostream &operator<<(ostream &os, const Complex &value)
{
	os << '<' << value.get_real() << ',' << value.get_imaginary() << '>';
    return os;


}

istream &operator>>(istream &is, Complex &value)
{
	return is >> value.i >> value.r;
}



How do I define a class Imaginary?



I suppose you could create a class to represent the imaginary part. Though of course, it would be a useless wrapper around a double.
Just ask your teacher..
what is teacher?
I got this question in the book of Bjarne Stroustrup?

"Define a class Imaginary to represent imaginary numbers. Define class Complex based on that. Implement the fundamental arithmetic operators"
Fine. If you want, write a class Imaginary and overload the +-*/ operators (I assume you know how). Then in class Complex, change member i from double, to Imaginary, and change the code you already wrote to use its operators.
@hamsterman

Can you show the code?

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Imaginary{
   double i;
   Imaginary oparator + (const Imaginary& b) {/*...*/}//write them yourself
   Imaginary oparator - (const Imaginary& b) {/*...*/}
   double oparator * (const Imaginary& b) { return -1*i*b.i; }
   double oparator / (const Imaginary& b) {/*...*/}
   //you probably also want to write the same operators for doubles
};

class Complex{
   double a;
   Imaginary b;
   Complex(/*...*/){/*...*/}
   Complex operator + (const Complex& c){ return Complex(a+c.a, b+c.b); }
   //and so on.
};


By the way, you know, it's a book so no one if forcing you to do that.. If you feel comfortable with operator overloading (which I assume was the key element) then you can move on. That's the power of choice!..
thanks hamsterman
Topic archived. No new replies allowed.