Answer to Question #203731 in C++ for python

Question #203731

Write the definition for a class called complex that has floating point data members for

storing real and imaginary parts. The class has the following member functions:

void set(float, float) to set the specified value in object

void disp() to display complex number object

complex sum(complex) to sum two complex numbers & return complex number


1. Write the definitions for each of the above member functions.

2. Write main function to create three complex number objects. Set the value in two objects and

call sum() to calculate sum and assign it in third object. Display all complex numbers.

Note: For user understanding purposes you should write comment with each line of code.


1
Expert's answer
2021-06-05T23:55:33-0400


#include <iostream>
using namespace std;
class complex{
    private:
        float real;
	    float img;
    public:
        void set(float x, float y){
            real=x;
            img=y;
        }
        void disp(){
            cout<<real<<" + j"<<img<<endl;
        }
        complex sum(complex c){
            complex n;
	        n.real = real + c.real;
	        n.img = img + c.img;
	        return n;
        }
};
int main()
{
    complex comp1,comp2,comp3;
	comp1.set(3.2,8.9);
	comp2.set(7.3,6.5);
	comp3=comp1.sum(comp2);
	cout<<"\nComplex Number 1: ";
	comp1.disp();
	cout<<"\nComplex Number 2: ";
	comp2.disp();
	cout<<"\nComplex Number 3: ";
	comp3.disp();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog