Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using C++ code /* FILE: const4.cpp */ /* Access to the invoking object is provided implicitly thru a pointer named this. this can be used
Using C++ code
/*
FILE: const4.cpp */
/* Access to the invoking object is provided implicitly thru a pointer named "this".
"this" can be used explicitly. */
#includeusing namespace std;
class COMPLEX{ double Re; double Im;
public: COMPLEX Mult(const COMPLEX & b); void print( ); void print2( );
COMPLEX(double r, double im);
COMPLEX( ){} };
COMPLEX COMPLEX::Mult(const COMPLEX & b)
{ COMPLEX result(0,0);
result.Re = Re*b.Re - Im*b.Im; result.Im = Re*b.Im + Im*b.Re;
return result; }
void COMPLEX::print( )
{ cout << "(" << Re << " + " << Im << "i)" ;
}
void COMPLEX::print2( )
{ cout << "(" << this->Re << " + " << this->Im << "i)" ;
}
COMPLEX::COMPLEX(double r, double im)
{ Re = r;
Im = im;
}
int main( )
{ COMPLEX c1(2,3), c2(2,3), cresult;
cresult = c1.Mult(c2);
cout << "Result of "; c1.print( ); cout << " * "; c2.print( );
cout << " = "; cresult.print( ); cout << endl;
cout << "Result of "; c1.print2( ); cout << " * "; c2.print2( );
cout << " = "; cresult.print2( ); cout << endl;
return 0;
}
Exercise #8: ------------ - Make the following changes to the const4.cpp example program: - Have the default constructor initialize both the real and imaginary parts/attributes to 0 - Add addition and subtraction methods to the COMPLEX datatype/class - Treat a method's parameters and invoking object as constant when appropriate - Modify main( ) to demonstrate that the arithmetic methods and new constructor all do their jobs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started