#include <iostream>
#include "Complex.h"
usingnamespace std;
int main()
{
Complex x,y,z;
x.setdata(1.2,2.3); //set x as 1.2 + 2.3i
y.setdata(3.4,4.5); //set y as 3.4 + 4.5i
//print out the value of x
cout << "The value of x is: ";
x.print_complex();
cout << endl;
//print out the value of y
cout << "The value of y is: ";
y.print_complex();
cout << endl;
//add y and x, i.e., z = x+y)
z = x.add_complex(y);
//print out the value of x
cout << "The sum of x and y is: ";
z.print_complex();
cout << endl;
}
complex.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Declare the class Complex
usingnamespace std;
class Complex
{
private:
double a,b; //this complex number is a+bi
public:
void setdata(double, double); // this function sets the values of a and b (the first parameter is set as a; the second is set as b)
double get_real(); // return the value of a;
double get_imag(); // return the value of b;
void print_complex(); // when this function is called, print out the value: a+bi
Complex add_complex(Complex); // addition of two complex numbers, return the sum
};