ou 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
[code#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();
};][/code]

You aren't creating a complex number inside of main(). You take in the real and imaginary parts, but that is all. You don't construct a Complex object from that input (c1 and c2).

so how would you approach it?

Line 96 cout << "The value of the complex number created is: "<< endl;

what? You are not really doing anything after line 96. It seems like your code is unfinished.

The problem here lies in the order you are doing things, and I suspect in a conceptual error that is fairly common among new programmers. When you are declaring and initializing the variable userInput, the values of c1 and c2 have not yet been set, so the values passed to the c'tor aren't valid; it will set real and imag to whatever garbage is in those values. Changing the values of c1 and c2 after the fact won't change this; there is no connection between those variables and the instance variables in the ComplexNumber object once you've created it.

One thing you have to remember is that all variable passing in C is really pass by value; even 'pass by reference' is really just passing the value of the reference or pointer, and has no special compiler support. Once you pass a value to a function - including a constructor - the function is working from a copy of the values passed. If this were not the case, there would be no way to pass a literal, or the result of an expression, without resorting to some complex Pass by Name system involving thunks, which is contrary to how C++ works.

The practical upshot of this is that you will want to move the declaration of userInput to past where the user input is read.

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.