1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
hram777 [196]
4 years ago
10

Given two complex numbers, find the sum of the complex numbers using operator overloading.Write an operator overloading function

ProblemSolution operator + (ProblemSolution const &P)which adds two ProblemSolution objects and returns a new ProblemSolution object

Computers and Technology
1 answer:
Inessa05 [86]4 years ago
4 0

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

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.

You might be interested in
Which button do you click to add up a series of numbers
UNO [17]
It’s autosum



Explanation
5 0
3 years ago
) Briefly describe the purpose of the DASH protocol in streaming videos.
Dennis_Churaev [7]

Answer:

The purpose of the DASH protocol in the streaming videos are that it provide the high quality for the streaming media content from the HTTP servers.

DASH stands for the dynamic streaming over HTTP and it is a standard for the steaming over the HTTP that has potential for replacing the various technology. It is basically produces various on demand resources file depending upon the processing power and the bandwidth of the delivery to the clients.

3 0
3 years ago
: Write a function "def countWords(string)" that returns a count of all words in an input string represented by the variable 'st
Zina [86]
We can define a word as a group of characters without a space between them. To find the words of the input string , w can use split(delimiter) which returns a list of strings which had the defined delimiter between them in the input string.

def countWords(string):
words = string.split(" ")
count = len(words)
return count

Here we set the delimiter as the space character, and returned the length of the words list. I split each step into its own line for readability, however the function could be one line:

return len(string.split())

Here, no delimiter is specified. If one isn't given, it will default to split at any whitespace, including space.

3 0
3 years ago
Help please im not sure what this means T^T
Vesna [10]

Answer:

14

Explanation:

7 0
3 years ago
Read 2 more answers
What is a Boolean operator?
ASHA 777 [7]

Answer:

The correct answer is search parameters set to identify specific information during Internet searches.

Explanation:

A Boolean operator is used to connect words or symbols that allows setting search parameters to include or exclude items in a text search.  

The booleans operators are:

  • AND.
  • OR.
  • NOT.
8 0
3 years ago
Other questions:
  • What is the name of the feature that can be enabled on slower-speed WAN links to prevent a large data transfer from affecting th
    10·1 answer
  • Do AirPods Pro have a green flashing light when charging?
    11·2 answers
  • Which of the following would be an example of a loss of record integrity in a
    12·1 answer
  • After you post a video of yourself defacing school property You can likely expect that
    11·2 answers
  • Do you think our attitude (whether positive or negative) is something we are born with or that we have power to control within o
    5·2 answers
  • An expression involving byte, int, and literal numbers is promoted to which of these?
    12·1 answer
  • Press the _______ key to move to the next cell in a row.
    5·1 answer
  • Exercise 3.6.9: 24 vs. "24"5 points
    7·1 answer
  • Nicole is in a study group to prepare for a test on plant biology, a subject she knows a lot about. During their meetings, she a
    8·2 answers
  • What is the name of the option in most presentation applications with which you can modify slide elements?
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!