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]
kelvin.njuguna 0 Newbie Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
kelvin.njuguna 0 Newbie Poster
kal_crazy 13 Junior Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
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.