complex class

Hey guys ., this is urgent; so i need help with my code ; well i have to Create a class called Complex for performing arithmetic with complex numbers.

Write a driver program to test your class. Complex numbers have the form:
realPart + imaginaryPart * i
where i is the square root of -1

Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following:

a) Addition of two Complex numbers: The real parts are added together and the imaginary parts are added together.

b) Subtraction of two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.

c) Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part

I'm not sure if the class is write. And if possible can i be provided with a driver program
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
  class complex {
	
	double realPart , imaginaryPart;

public:

	double set_values (double real, double imag){

	realPart = real;
	
	imaginaryPart = imag;

}
	
	double get_real (){
		
		return realPart;
	}
	
	double get_imag () {
		
		return imaginaryPart;
	}

	double comp_add (complex c1, complex c2){
		
		double r, i;
		
		r = c1.get_real() + c2.get_real();

		i = c1.get_imag() + c2.get_imag();

	}

	double comp_subt (complex c1, complex c2){

		double r, i;
		
		r = c1.get_real() - c2.get_real();

		i = c1.get_imag() - c2.get_imag();

	}
		void print_complex () {
			cout << "(" << r << "," << i << ")" << endl;
		}
};
Last edited on
Some problems with your class:

1) set_values is declared to return a double, but is missing a return statement.
Should probably be a void function.
Same with comp_add and comp_subt.

2) comp_add computes r and I, but doesn;t do anything with them.
Same problem in comp_subt.

3) print_complex uses r and i but they're undefined

4) Assignment says to provide a contructor.
You have no constructor.
Last edited on
modified code:

class complex {

double realPart , imaginaryPart;

public:

public Complex( double real, double imaginary )
{
realPart = real;
imaginaryPart = imaginary;
}

double get_real (){

return realPart;
}

double get_imag () {

return imaginaryPart;
}

void comp_add (complex c1, complex c2){

double r, i;

r = c1.get_real() + c2.get_real();

i = c1.get_imag() + c2.get_imag();

}

void comp_subt (complex c1, complex c2){

double r, i;

r = c1.get_real() - c2.get_real();

i = c1.get_imag() - c2.get_imag();

}
void print_complex () {
cout << "(" << r << "," << i << ")" << endl;
}
};



i'd appreciate it if you could just check this code and provide with driver test program.
Last edited on
Issues #2 and #3 still exist. comp_add and comp_subt compute r and i but the values are lost when the functions exit.

As for a driver program, that is part of your homework and I won't do your homework for you. I will tell you that a driver program is nothing more than a main that instantiates a few complex numbers and performs some operations on them.

You might want to consider overloading the + and - and << operators. This will allow you to use complex numbers in a very intuitive way.
1
2
3
4
5
6
7
int main () 
{ complex c1(1,2), c2(3,4), rslt1, rslt2;
   rslt1 = c1 + c2;
   rslt2 = c2 - c1;
   cout << "rslt1=" << rslt1 << endl;
   cout << "rslt2=" << rslt2 << endl;
}



PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

Last edited on
Topic archived. No new replies allowed.