Question: 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. */ 
#include  using 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!