Answer to Question #241696 in C++ for Noah

Question #241696

Write a user-defined program to declare a class which stores a complex number. Demonstrate the use of constant objects , constant member function and constant arguments, using this class.


1
Expert's answer
2021-09-25T03:03:52-0400
#include <iostream>
#include <string>
#include <sstream>
class Complex
{
private:
double x,y;
public:
Complex() { x = 0; y = 0; }
Complex(double x, double y) { this->x = x; this->y = y; }
Complex operator + (Complex const& object) {
Complex re;
re.x = x + object.x;
re.y = y + object.y;
return re;
}


Complex operator - (Complex const& object) {//use of constant objects


Complex re;
re.x = x - object.x;


re.y = y - object.y;
return re;


}
std::string print()
//demonstrating constant member function and constant arguments
{


std::string result = "";
std::ostringstream x_sstream;


x_sstream << x;


std::string x_str = x_sstream.str();


std::ostringstream y_sstream;


y_sstream << y;


std::string y_str = y_sstream.str();


if (y < 0)
result = y_str + y_str + "i";
else


result = x_str + "+" + x_str + "i";
return result;


}


};
int main()


{
Complex number1(3.5, 5.1),
number2(-4.4, -3.8);


Complex number3 = number1 - number2;
std::cout << number3.print();


return 0;


}

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

Noah
06.10.21, 09:49

Thanks a lot Assignmentexpert

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS