Fixing C++ Class Function for Complex Numbers

In summary: Complex * Complex::add(Complex & c1, Complex & c2) { Complex * added_numbers = new Complex; ... added_numbers->real = c2.real+ c1.real; added_numbers->imaginary = c2.imaginary + c1.imaginary; added_numbers->print(); //I added this to make sure the addition was occurring return added_numbers; }This will work because Complex added_numbers will have the same type as Complex c1 and c2.
  • #1
jersiq1
7
0
For one of my assignments, I have to write a program to add or subtract complex numbers. I wrote a class, and the problem I am having is that one of my methods isn't returning the data to the calling object. I am going to omit the things in my code that I don't think are pertinent.

Code:
int main()
{
	Complex num1(2,5),num2(7,8),num3,num4; 

	num3.add(num1,num2); //this is the function that isn't working as expected
	num3.print(); //I am not getting the proper output here. Just the values of the default constructor
	
cin.get();
	return 0;
}

Here's the Class:

Code:
class Complex 
{
public:
		Complex();
		Complex(int , int );
		~Complex();
		Complex add(Complex &,Complex &);
		Complex sub(Complex &,Complex &);
		void print();

private: //data accessed by public functions
	int real;
	int imaginary;
};

Complex Complex::add(Complex & c1, Complex & c2) 
{
	Complex added_numbers;
	added_numbers.real = c2.real+ c1.real;
	added_numbers.imaginary = c2.imaginary + c1.imaginary;
	added_numbers.print(); //I added this to make sure the addition was occurring
	return added_numbers;	
}
}

I added the print function within the add function just to make sure it was adding, and it is. However, it isn't returning the values of added_numbers. I guess I am just really failing to understand why.
 
Physics news on Phys.org
  • #2
add() doesn't set the member values real and parts. it returns a new complex number object.

It would normally be a static function that you could call to create a complex object - a factory in pattern speak
 
  • #3
The problem is here:
jersiq1 said:
Code:
Complex Complex::add(Complex & c1, Complex & c2) 
{
	Complex added_numbers;
	...
	return added_numbers;	
}
}

You are returning something that is going out of scope. In C/C++ parlance, this is "undefined behavior". Your program could make nasal demons fly out of the computer in response to this, but as far as the standards committee is concerned, the compiler would still be compliant. What you need to do is to create a new Complex number and return that.

Code:
Complex * Complex::add(Complex & c1, Complex & c2) 
	Complex * added_numbers = new Complex;
	...
	return added_numbers;	
}

Alternately, pass the number that will contain the result as an argument.
 

What is the purpose of fixing C++ class function for complex numbers?

The purpose of fixing C++ class function for complex numbers is to eliminate any errors or bugs in the existing code and improve its functionality. This will ensure that complex number calculations are performed accurately and efficiently.

What are some common issues with the C++ class function for complex numbers?

Some common issues with the C++ class function for complex numbers include incorrect calculations, memory leaks, and lack of support for certain operations such as division by zero.

How can I fix the C++ class function for complex numbers?

The C++ class function for complex numbers can be fixed by carefully reviewing the code for any errors or bugs, utilizing debugging tools, and implementing proper exception handling and memory management techniques.

Are there any best practices for fixing the C++ class function for complex numbers?

Yes, some best practices for fixing the C++ class function for complex numbers include using standard libraries for complex number operations, following object-oriented programming principles, and thoroughly testing the code for accuracy and efficiency.

Can I contribute to the improvement of the C++ class function for complex numbers?

Yes, anyone with knowledge of C++ programming and complex numbers can contribute to the improvement of the C++ class function for complex numbers. You can suggest changes, report bugs, and even submit your own code for review and integration into the existing codebase.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
982
  • Engineering and Comp Sci Homework Help
Replies
2
Views
941
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
47
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
893
  • Calculus and Beyond Homework Help
Replies
27
Views
729
Back
Top