Answer to Question #193316 in C++ for Sankalp

Question #193316

Write a class and member functions for a class complex as follows

Class complex

{

    Int re, img;

     public:

    complex (int =0,int=0);

    complex(complex &);

    void accept();

    void display();

   complex add(const complex &);


1
Expert's answer
2021-05-18T19:51:27-0400
#include <iostream>


using namespace std;


class complex
{
    int re, img;
    public:
    complex (int =0,int=0);
    complex(complex &);
    void accept();
    void display();
    complex add(const complex &);
};


complex::complex(int r, int i)
{
	re = r;
	img = i;
}


complex::complex(complex& rhs)
{
	re = rhs.re;
	img = rhs.img;
}


void complex::display()
{
	cout << re;
	if(img >= 0) cout << "+" << img << "*i" << endl;
	else cout << img << "*i" << endl;
}


complex complex::add(const complex& rhs)
{
	complex temp(re + rhs.re, img + rhs.img);
	return temp;
}


void complex::accept()
{
	cout << "Accept complex number ";
	this->display();
}


int main()
{
	complex obj1(5, -3);
	obj1.display();
	complex obj2(obj1);
	obj2.display();
	complex obj3(1, 1);
	obj3 = obj1.add(obj2);
	obj3.display();
	obj3.accept();
	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