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
Which of the following safety and privacy features is not included in a P2P app or service?
diamong [38]

The option that should not be included in the safety and privacy features is not included in a P2P app or service is the non-inclusion of Representative call for confirming payments over $100.

<h3>What is P2P application?</h3>

This is known to be a form of Peer-to-peer payment services. They are apps or application that has its features which helps one to be able  to  to send money to other people.

Conclusively, a Representative call that is made for confirmation of payments that is over $100 should not be included as people may use this kinds of call to defraud other people.

Learn more about privacy features  from

brainly.com/question/20533576

3 0
2 years ago
Read 2 more answers
Q4. Write down the JavaScript statements to perform the following tasks.
goldenfox [79]

Answer:

Explanation:

In Javascript, you can accept an input value by using the prompt() function and saving the input into a variable. In the following lines of code, I have declared the three variables at the beginning and then prompted the user to enter a value for each and saved the values in the correct variables. In Javascript length is a keyword so I used len instead.

let base, height, len;

base = prompt("Enter Base value: ");

height = prompt("Enter Height value: ");

len = prompt("Enter Length value: ");

5 0
3 years ago
if you see that your bank account has had some money taken out that you did not authorize, which of the following is one action
Savatey [412]
I would go straight to the bank and have it reported along with the police.
7 0
3 years ago
Which of the following is used to keep track of website content?
Alja [10]

Complete Question:

Which of the following is used to keep track of website content?

Group of answer choices.

A. Antivirus software.

B. File management system.

C. Disk management.

D. Control panel.

Answer:

B. File management system.

Explanation:

HTML is an acronym for hypertext markup language and it is a standard programming language which is used for designing, developing and creating web pages.

Generally, all HTML documents are divided into two (2) main parts; body and head. The head contains information such as version of HTML, title of a page, metadata, link to custom favicons and CSS etc. The body of the HTML document contains the contents or informations of a web page to be displayed.

When a website has been created, it is important and necessary to manage and monitor the content or resources contained in the web site so as to improve and enhance its performance. In order to achieve this, a file management software or system such as LogicalDoc, Goo-gle drive, DocuWare, Alfresco, etc.

Hence, a file management system which is typically a cloud-based service can be used to keep track of website content.

6 0
3 years ago
Which of the following is NOT a safety practice for working near power lines?
dimulka [17.4K]
The answer is use rider posts, when available, to avoid getting too close to the power line. 

I hope this helps!  
5 0
3 years ago
Other questions:
  • Ld' is the instruction with the longest latency on the CPU from Section 4.4 (in RISC-V text). If we modified ld and sd so that t
    15·1 answer
  • If userA wants to send a secure message to userB using an asymmetric cryptographic algorithm, which key does userB use to decryp
    11·1 answer
  • ___ Jacking is a crime that takes place when a hacker misdirects URL to a different site. The Link Itself Looks Safe, But the us
    11·1 answer
  • What is the interface in Java programming Language? Why do we need to use it?
    6·1 answer
  • It’s been six months since the disk crash at CSM Tech Publishing, and the owner is breathing a little easier because you install
    7·1 answer
  • A(n) _____ is a firm that delivers a software application, or access to an application, by charging a usage or subscription fee.
    6·1 answer
  • What is the function for displaying differences between two or more scenarios side by side?
    11·2 answers
  • Write the difference between left-sentential form and <br> right-sentential form
    13·1 answer
  • MARK AS BRANLIEST!!!!
    7·2 answers
  • What are the advantage of an e-library​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!