Answer to Question #193814 in C++ for Sankalp

Question #193814

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-15T13:52:35-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;






class Complex{
	
	public:
		static int count;
		int real, img;
		
		Complex(int real=0, int img=0)
		{
			this->real = real;
			this->img = img;
		}
		
		
		
		Complex (Complex &c)
		{
			this->real = c.real;
			this->img = c.img;
		}
		
		void operator-()
		{
			this->real = -real;
			this->img = -img;
		}
		
		void accept()
		{
			count++;
			int re, im;
			cout<<"Enter Real part of complex number : ";
			cin>>re;
			cout<<"Enter Imaginary part of complex number : ";
			cin>>im;
			real = re;
			img = im;
		}
		
		void display()
		{
			cout<<"("<<this->real<<") + ("<<this->img<<")i"<<endl;
		}
		
		Complex add(const Complex &c)
		{
			Complex res;
			int re = this->real+c.real;
			int im = this->img + c.img;
			res.real = re;
			res.img= im;
			return res;
		}
};


int Complex :: count =0;


int main()
{
	
	
	Complex c1;
	c1.accept();
	cout<<endl<<"Value of complex number "<<Complex::count<<" : ";
	c1.display();
	Complex c2;
	cout<<endl;
	c2.accept();
	cout<<endl<<"Value of complex number "<<Complex::count<<" : ";
	c2.display();
	
	Complex c3 = c1;
	
	c3 = c3.add(c2);
	cout<<endl<<"After addition"<<endl;
	c3.display();
	
}

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