List of all available Websheets
Viewing cpp/cs104/op_overload/complex_add_int by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Add a member function to allow a Complex number (object) to be added with an integer. <p> Example: <pre> Complex c1(2,3); Complex c2 = c1 + 6; // should yield 8+3i </pre> |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/op_overload/complex_add_member (author: redekopp@usc.edu) Copied from problem cpp/cs104/op_overload/complex_add_nonmember (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; class Complex { public: Complex() { real_ = 0; imag_ = 0; }; Complex(double r, double i) { real_ = r; imag_ = i; } double getReal() const { return real_; } double getImag() const { return imag_; } Complex operator+(const Complex& rhs) const; \[ REDACTED ]\ public: double real_; double imag_; }; Complex Complex::operator+(const Complex& rhs) const { Complex temp; temp.real_ = real_ + rhs.real_; temp.imag_ = imag_ + rhs.imag_; return temp; } \[ REDACTED ]\ int main() { Complex c1(2,3); Complex c2(4,5); // Write a function to allow this to compile Complex c3 = c1 + 6; cout << c3.getReal() << "+" << c3.getImag() << "i" << endl; return 0; } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [{}] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.