Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++, 5) Show the code for an addition method for a Complex class. (You of course

ID: 3840322 • Letter: C

Question

c++,

5) Show the code for an addition method for a Complex class. (You of course recall that a complex number

has both a real and an imaginary part.) (Reminder: Adding two values does not change either of the values, but

creates a third value which is the originals’ sum.) (Hint: There is a perfectly good call signature for such a function

such as  Complex Complex::add(const Complex & b) const;)

Since you are here and thinking about it, some programmers might find it useful if Complex addition were

compatible with the built-in types — double, for instance. Write such an overload here:

Q)

If a class has the following private data members:

string site; // location of sample collection

vector<double> sample_values; // values of samples taken

Discuss/code appropriate accessors/mutators for these two members. (Thought-provokers: How many functions

would be needed for each member? Are any meta-level functions required? Do programmers outside the class

want the entire member or just a piece of it at a time?)

Explanation / Answer

C++ program to add two complex number by using the concept of operator overloading.

#include<iostream>
using namespace std;
class complex
{
int real,img;
public:
void read()
{
cout<<" Enter the Real Part of complex Number:"; // Real part of complex no is entered here.
cin>>real;
cout<<" Enter the Imaginary Part of complex Number:"; // Imaginary part of complex no is entered here.
cin>>img;
}
void display()
{
cout<<" = "<<real<<"+"<<img<<"i"; // entered value is displayed in the form of (real_part)+(img_part)*i
}
complex operator+(complex x2) // overloading concept is applied here.
{
complex x;
x.real=real+x2.real; // real_part is added with the real_part of other complex no.
x.img=img+x2.img; // img_part is added with the img_part of other complex no.
return x;
}
   };
main()
{
complex a,b,c; // a,b & c variable are initialized as complex so that the data entered and displayed output must be of complex datatype.

cout<<" Enter The First Complex Number:";
a.read(); // first complex no is read here.
a.display(); // first complex no is displayed in the output section.
cout<<" Enter The Second Complex Number:";
b.read(); // second complex no is read here.
b.display();   // second complex no is displayed in the output section.
c=a+b;
c.display(); //the addition value of a & b is stored in the c variable.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote