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
1.Public policy begins with . 2.Then, government officials . 3.Finally, a government agency .
dexar [7]

Answer:

what was the wuestion

Explanation:

cause we dont know how to help you

4 0
3 years ago
What help the ict in your life?and write own idea three or four sentence
My name is Ann [436]

Answer:

I am a software engineering student

computers have createed an impact in my life because through computers i work, I do research and many more

7 0
2 years ago
The code size of 2-address instruction is________________.? 5 bytes? 7 bytes? 3 bytes? 2 bytes
Finger [1]

Answer:

7 bytes

Explanation:

<u>2 Address Instruction</u>

The 2 address instruction consist 3 components in the format.

One is opcode,other two are addresses of destination and source.

<u>Example-</u>

load b,c | Opcode   destination address,source address

add a,d  | Opcode   destination address,source address

sub c,f    | Opcode   destination address,source address

Opcode consists of 1 bytes whereas destination address and source address consist of 3 bytes each.

(1+3+3) bytes=7 bytes

5 0
3 years ago
In which slot is a video card most commonly installed within a laptop?
IgorLugansk [536]

Answer:

<u>PCIe</u> is used for video card installation on Laptop.

Explanation:

Good quality applications, videos and games required graphics card for better results. These card can be installed on laptops, that required some ports.

PCI-e is a slot, that is used in laptop for video or graphic card interface. Video card or graphic card is used to enable high quality video or graphics on Laptop.

PCI- E stand for Peripheral Component Interconnect Express. It work as serial communication interface for high speed device.

7 0
3 years ago
What is the famous saying among computer programmers that refers to the fact that computers cannot tell the difference between g
jek_recluse [69]

Answer:

Garbage in, garbage out

Explanation:

Garbage in, garbage out is the famous saying among computer programmers that incorrect, poor-quality, flawed, or nonsense input will produce incorrect, poor-quality, flawed, or nonsense output while correct, good quality or valuable input will produce correct, good quality or valuable output. it is basically saying that computers cannot tell the difference between good and bad data.

7 0
3 years ago
Read 2 more answers
Other questions:
  • What type of software translates a programmer’s statements to binary form?
    10·1 answer
  • Some technologies like vertical farming have a number of negative effects. Which is a negative outcome of this technology? A. In
    10·1 answer
  • which of the following is involved in ordering an outline. A.grouping B.merging C.organizing D.arranging
    10·1 answer
  • Write a program that will open the file random.txt and calculate and display the following: A. The number of numbers in the file
    12·1 answer
  • How did the invention of the printing press lead eventually to an increase in the diversity of religious expression?
    7·1 answer
  • Joe, a user, has just installed his first home wireless router. Which of the following tasks should be considered to help secure
    8·1 answer
  • Case 1-1Tom has just started working as an intern for a local radio station. He is responsible for managing the request line and
    8·1 answer
  • A technician needs to boot a PC from a USB drive.
    5·1 answer
  • Why is it important to use random assignment when determining which research participants will comprise the different treatment
    12·1 answer
  • Select the correct word to complete the sentence.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!