I'm having problems. I get the addition part. can somebody help.

Here is the code i wrote so far.
thanks,

#ifndef complex.h
#define complex.h

	class complex
	{
	public:
		complex();
		complex(double real, double imaginary);
		void complex addition(const complex &a);
		
		void complex printcomplex();
		void complex setcomplexnumber();

	private:
		double realpart, imaginarypart;
	};
#endif

//constructor
	complex::complex(double real, double imaginary)
	{
		setcomplexnumber(real, imaginary);
	}

	void complex:: addition( const complaex &a)
	{
	//adding-This is where i'm stuck\\
	}
	void complex:: printcomplex()
	{
		cout<< '(' <<realpart <<", " << imaginarypart<<')'
	}
	void complex::setcomplexnumber(double real, double imaginary)
	{
		realpart=real;
		imaginarypart=imaginary;
	}

Recommended Answers

All 4 Replies

How would you express the addition of two complex numbers without code?

Then try to translate that into code.

(And keep in mind what you intend to do: add to the current object, or add two distinct objects and return a third.)

[And you can edit your post to add the missing closing tag:
[/code].]

my brain is about to explode. Its not coming to me. Need Help/.

I need to write a statement to add the realpart of a to the class realpart.
and write a statement too add the realpart of a to class imaginarypart.

Here's the rest of the code

int main()
{
	complex b(1,7), c(9,2);

	b.printcomplex();
	cout<< "+";
	c.printcomplex();
	cout<< "=";
	b.addition(c);
	b.printcomplex();

	cout<< endl;
	return 0;

You're working in C++. C++ has operator overloading.
You're working on a mathematical addition operation. It makes in this context perfect sense to define an operator in (or as a friend of) your complex class.

As a start I'll give you the headerfile for a complex number class I implemented for fun a while ago.
The implementation of the methods and operator is trivial and I leave that to you.

#include <iostream>

using namespace std;

class complexNumber
{
private:
	float real;
	float imag;

public:
	complexNumber();
	complexNumber(float rPart, float iPart);
	~complexNumber();

	complexNumber operator+(const complexNumber& c);
	complexNumber operator-(const complexNumber& c);
	friend ostream& operator<<(ostream& s,  const complexNumber& c);
};

When properly implemented you can use the following testcode on it:

#include "complexnum.h"

int main()
{
	complexNumber t(1, 1);
	complexNumber t2(2, 2);
	complexNumber t3 = t + t2;
	cout << t << endl << t2 << endl << t3;
	return 0;
}

As you see I created not just a + operator but also a << operator so I can output a complex number directly and consistently.

The output of that small program should be

1+i
2+2i
3+3i

write a statement too add the realpart of a to class imaginarypart.

that doesnt sound right! For 2 complex numbers

(a+bi) + (c+di) = z (z is the result)

real(z) = a+c
im(z) = b+d

So why do you want to add the real of one complex number to a different complex number's imaginary?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.