Answer to Question #69760 in C++ for Urvashi

Question #69760
Implement a class complex no.which has two int nos.as private variables,ine for real part and other one for complex part.define input(),display()and constructor to initialize the object .include a member function to add two complex nos.
1
Expert's answer
2017-08-19T10:25:07-0400
class complex {
int re; // real part
int im; // complex part
public:
complex(int r, int c) {
re = r;
im = c;
}

void input() {
std::cout << "Enter real part: ";
std::cin >> re;
std::cout << "Enter complex part: ";
std::cin >> im;
}

void display() const {
std::cout << re;
if (im>0)
std::cout << " + ";
else if (im<0)
std::cout << " - ";

if (im!=0) {
std::cout << " i * " << abs(im);
}
}

void add(const complex& c) {
re += c.re;
im += c.im;
}
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS