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]
3 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]3 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
The objectivity of a site relates to its a. Appearance c. Graphics b. Biases d. Quotes from other Internet authors
nadya68 [22]

The answer is Biases

The objectivity of evaluating a website relates to its ability to present issues based on different points of view. It is based on whether or not the information provided is presented in a fair and balanced way. For example, is the purpose of a certain website like Coca Cola or Pepsi meant to entertain, sell something, or sway public opinion? Do you think these websites will provide information on the negative effects of drinking carbonated beverages? I do not think so! Thus, these websites have bias. Their job is to sell you their product, not to make you think deep about it. We need to ask ourselves more questions about the websites we visit. Is this website balanced or biased in a way it presents information?


5 0
3 years ago
Read 2 more answers
Describe the procedure for creating a table or spreadsheet in a presentation slide.
vodomira [7]

Answer:

Select the slide that you want to insert a table on.On the Insert tab, in the Tables group, click Table, and then click Excel Spreadsheet.To add text to a table cell, click the cell, and then enter your text

Explanation:

7 0
3 years ago
Read 2 more answers
​The US-CERT newsletter has alerted you about vulnerabilities in some software installed in your organization. To detect if ther
NISA [10]

Answer:

Knowledge-based intrusion detection system.

8 0
3 years ago
Need help coding this it uses input and I need to use the words good and morning
Masja [62]

Answer:

x = input ("Enter a word: ")

y = input ("Enter a word: ")

print ( x, " ", y)

Explanation:

This is the simplest way to write it using Python.

6 0
3 years ago
Help pls . computer class
ladessa [460]

Answer:

3rd , 4th, 1th

Explanation:

8 0
3 years ago
Other questions:
  • Write a program that responds to a positive integer passed on the command line with the number of bits needed to express that nu
    10·1 answer
  • Bob flys a drone which has a 20 megapixel camera attached, what is the definition of "megapixel in this context? Why does it mat
    7·1 answer
  • Assume you're using a three button mouse. To connect shortcut menus, you would what
    6·1 answer
  • All portions, to include subject, title, paragraphs, sub-paragraphs, graphics, tables, charts, and bullet statements, must be pr
    12·1 answer
  • When you expect a reader of your message to be uninterested, unwilling, displeased, or hostile, you should Group of answer choic
    15·1 answer
  • Describe four features of a well designed input screen
    10·2 answers
  • What type of computer is IBM 1401 ?<br><br>​
    13·1 answer
  • Which of the following is an example of an incremented sequence?
    13·1 answer
  • Write code that outputs variable numCats. End with a newline.
    6·1 answer
  • 3. Which part of the computer is used<br> for typing?<br> a. Mouse<br> b. Keyboard
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!