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
How does the PC support page of About.com make it quick and easy for users to identify and fix errors?
Simora [160]

Answer:

It offers advice on how to repair and maintain your computer.

Explanation:

About.com does not exist anymore sadly. However, it is available with a new name dot dash. And the PC support option is available under livewire. And here you can get advice on how to repair and maintain your computer. And that was the theme behind the About.com. And it has nothing to do with the links to useful tech support websites and forums, or it does not advertise the software which protects you from third-party threats. And it does not as well detail the list of error messages and the symptoms. And hence the correct option is as mentioned in the Answer section.

6 0
4 years ago
For a data structure, such as a stack, who is responsible for throwing an exception if the stack is empty and a pop() is called:
lubasha [3.4K]

Answer:

D. End-User Programmer.

Explanation:

A stack data structure is used to holds data for programs. The first data to go into a stack is always the last to be extracted (First-in-Last-out). Data is read into the stack with the push function and retrieved with the pop function.

When the stack is empty, it means there are no data left to pop from it. If a pop function is issued at this time, the program conventionally throws an error, there is no need for the end-user to write an exception handler for it because the end-user programmer has done that already.

4 0
3 years ago
What are the process of boots up a computer?​
Sindrei [870]

Answer:

in computer booting is the process of starting a computer it can be initiated By hardware such as button press or by a software command after it is switched on a computer central processing unit ( CPU)

3 0
3 years ago
Hola, alguien me puede ayudar¡¡¡¡¡¡¡¡¡¡¡¡
WARRIOR [948]
Hola<span>, </span>Alguien me<span> podría </span>ayudar<span> a realizar una buena sinopsis de la película "</span><span>Jardín de Amapolas" dirigida por Carlos Melo Guevara, gracias? i hope i helped you</span>
4 0
4 years ago
Which option is used to ensure the integrity and authenticity of a Word document but requires additional services to be availabl
mel-nik [20]

Answer:

Digital signature.

Explanation:

Digital signature is used to ensure the integrity and authenticity of a Word document but requires additional services to be available on the internal network.

This is important as it makes sure that the file is authentic and not malware.

3 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following can be both an input device and an output device? mouse. keyboard. display screen. laser printer .
    9·1 answer
  • Which type of view is created from the following SQL statement? CREATE VIEW balancedue AS SELECT customer#, order#, SUM(quantity
    15·1 answer
  • Structured walk-throughs take place throughout the SDLC and are called _____ reviews, depending on the phase in which they occur
    9·1 answer
  • An electronic device that can accept data as input, process it according to a program, store it, and produce information as outp
    6·1 answer
  • Write a MASM program to calculate Fibonacci numbers:
    10·1 answer
  • Universal Containers sells its products online using a system built on Force Sites. The orders are captured and processed in Sal
    7·1 answer
  • i give up on understanding and trying to pass science. ive now started clicking random letters as the choices. i actually got a
    5·1 answer
  • A(n) _____ is substance in which one or more atoms have the same number of protons (i.e. the same atomic number).
    6·1 answer
  • List any three importance of computer​
    14·1 answer
  • PLEASE HELP!!!
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!