Complex number with overloading,given a partial program

you have been tasked to write a program that takes two complex number and return theie sum.However the + operator will not worl with complex numbers and you figure you need to verload the + and the assignment opeartor=.Ypu have come across the program (http://wwww.cprogramming.com/tutorial/operator_overloading.html)

implement it and the client code to see it rune for the following complex numbers:
c1=3.0-4i,c2=8+4i


i have 3 files,driver.cpp,Complexnumber.cpp and complexNumber.h

complex.cpp is as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
class ComplexNumber
{
	private:
	  double real;
      double imag;
Public:
	
	
	Complex(double re,double im)
	
	:real(re),imag(im)
	Complex operator+(const Complex& other);
	Complex operator=(const Complex& other);

};
Complex Complex::operator+(const Complex& other)
{
	double result_rela = real + other.real;
	double result_imaginary = imag + other.imag;
	return Complex(result_real,result_imaginary);
}


the driver.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include"ComplexNumber.h"
using namespace std;


int main()
{
	double c1,c2;
	ComplexNumber userInput(c1,c2);

    cout << "Please enter in a value for the real part of the number: "<< endl;
    cin >> c1;

    cout << "Please enter in a value for the imaginary part of the number: "<<endl;
    cin >> c2;
    cout << "The value of the complex number created is: "<< endl;
    
    system ("PAUSE");
    return 0;

}


the Complex.h as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
class ComplexNumber
{
private:
	double real;
	double imag;

public:
	//default constructor
	ComplexNumber();
	//parametarized Construtcor
	ComplexNumber (double re,double im);
		double getreal() const;
	    double getimag() const;
		void printComplexNumber();
};]

Last edited on
any help would be greatly appreciated
What, specifically, are you having trouble with?
is overloading + operator and the = operator
and implementing the code as well,the .cpp file info is Given....
Topic archived. No new replies allowed.