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

Here is the code i wrote so far.

#ifndef complex.h
#define complex.h

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

		double realpart, imaginarypart;

	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)

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:

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);

	cout<< "+";
	cout<< "=";

	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
	float real;
	float imag;

	complexNumber(float rPart, float iPart);

	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


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?

This article has been dead for over six months. Start a new discussion instead.