print complex number as format a+bi , a-bi

A print function that prints the date in the following format of complex numbers such as: a + bi , a – bi

this problem how can i solve

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H


class complexnumber
{
    double real_part,imaginary_Part;
    public:
        complexnumber(double,double);
        void Set_Real_Part(double);
        double Get_Real_Part();
        void Set_Imaginary_part(double);
        double Get_Imaginary_part();
        void Print();

};

#endif // COMPLEXNUMBER_H 





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "complexnumber.h"
#include<iostream>
using namespace std;

complexnumber::complexnumber(double RealPart,double ImaginaryPart)
{
    real_part = RealPart ;
    imaginary_Part = ImaginaryPart ;
}
void complexnumber::Set_Real_Part(double realpart)
{
    real_part = realpart;
}
double complexnumber::Get_Real_Part()
{
    return real_part;
}
void complexnumber::Set_Imaginary_part(double imaginarypart)
{
    imaginary_Part = imaginarypart;
}
double complexnumber::Get_Imaginary_part()
{
    return imaginary_Part;
}
void complexnumber::Print()
{


}


i am understand The rest of the question

Create a class ComplexNumber, that includes two data members :
double realPart
double imaginaryPart
The class member functions:
A set and a get declaration/definition for each of the data members of the class complexNumber.
A print function that prints the date in the following format of complex numbers such as: a + bi , a – bi
Parameterized Constructor ComplexNumber (double r, double i);
In the main:
Define the 3 objects from class complexNumber, and initialize its values.
Print the information of the three objects by the member function print.

Are you familiar with complex numbers and how to print stuff in C++?

When they say print "a + bi" they mean a should be replaced by the real part and b with the imaginary part.

Example: 5 + 2i is the a complex number with real part 5 and imaginary part 2.
yes the lang is c++

i want print complex number as format a+bi , a-bi

i have idea but it is not solve problem

what about this sentence for the output

cout<<Get_Real_Part() << '+' <<Get_Imaginary_part() << 'i' << Ge_Real_Part() << '-' << Get_Imaginary_part() << endl;
You should define an operator<< for your class that writes stuff in the correct format.

That will allow you to write code like:
1
2
complexnumber a(2, 3);
std::cout << a << std::endl;


So, you need a function like:
1
2
3
4
5
6
std::ostream& operator<<(std::ostream& s, const complexnumber& n)
{
    // write your stuff

    return s;
}
Last edited on
but I do not want access to the depth
you can declare a friend function in class complexnumber like

friend ostream& operator<<(ostream& os,const complexnumber& com)
{
return os << com.real_part << '+' << com.imaginary_part << 'i';
}
Registered users can post here. Sign in or register to post.