160k views
3 votes
Given two complex numbers, find the sum of the complex numbers using operator overloading.Write an operator overloading functionProblemSolution operator + (ProblemSolution const &P)which adds two ProblemSolution objects and returns a new ProblemSolution object

User Mtruesdell
by
4.7k points

1 Answer

6 votes

Answer:

I am writing the program in C++ programming language.

#include<iostream> // to use input output functions

using namespace std; // to identify objects like cin cout

class ProblemSolution { //class name

private:

// private data members that only be accessed within ProblemSolution class

int real, imag;

// private variables for real and imaginary part of complex numbers

public:

// constructor ProblemSolution() to initialize values of real and imaginary numbers to 0. r is for real part and i for imaginary part of complex number

ProblemSolution(int r = 0, int i =0) {

real = r; imag = i; }

/* print() method that displays real and imaginary part in output of the sum of complex numbers */

void print(){

//prints real and imaginary part of complex number with a space between //them

cout<<real<<" "<<imag;}

// computes the sum of complex numbers using operator overloading

ProblemSolution operator + (ProblemSolution const &P){ //pass by ref

ProblemSolution sum; // object of ProblemSolution

sum.real = real + P.real; // adds the real part of the complex nos.

sum.imag = imag + P.imag; //adds imaginary parts of complex nos.

//returns the resulting object

return sum; } //returns the sum of complex numbers

}; //end of the class ProblemSolution

int main(){ //start of the main() function body

int real,imag; //declare variables for real and imaginary part of complex nos

//reads values of real and imaginary part of first input complex no.1

cin>>real>>imag;

//creates object problemSolution1 for first complex number

ProblemSolution problemSolution1(real, imag); //creates object

//reads values of real and imaginary part of first input complex no.2

cin>>real>>imag;

//creates object problemSolution2 for second complex number

ProblemSolution problemSolution2(real,imag);

//creates object problemSolution2 to store the addition of two complex nos.

ProblemSolution problemSolution3 = problemSolution1 + problemSolution2;

problemSolution3.print();} //calls print() method to display the result of the //sum with real and imaginary part of the sum displayed with a space

Step-by-step explanation:

The program is well explained in the comments mentioned with each statement of the program. The program has a class named ProblemSolution which has two data members real and imag to hold the values for the real and imaginary parts of the complex number. A default constructor ProblemSolution() which initializes an the objects for complex numbers 1 and 2 automatically when they are created.

ProblemSolution operator + (ProblemSolution const &P) is the operator overloading function. This performs the overloading of a binary operator + operating on two operands. This is used here to add two complex numbers. In order to use a binary operator one of the operands should be passed as argument to the operator function. Here one argument const &P is passed. This is call by reference. Object sum is created to add the two complex numbers with real and imaginary parts and then the resulting object which is the sum of these two complex numbers is returned.

In the main() method, three objects of type ProblemSolution are created and user is prompted to enter real and imaginary parts for two complex numbers. These are stored in objects problemSolution1 and problemSolution2. Then statement ProblemSolution problemSolution3 = problemSolution1 + problemSolution2; creates another object problemSolution3 to hold the result of the addition. When this statement is executed it invokes the operator function ProblemSolution operator + (ProblemSolution const &P). This function returns the resultant complex number (object) to main() function and print() function is called which is used to display the output of the addition.

Given two complex numbers, find the sum of the complex numbers using operator overloading-example-1
User Rgerganov
by
5.2k points