ECE 2036 Test II: Output: - IECE 203 826a
ECE 2036 Test II: Output: - IECE 203 826a
ECE 2036 Test II: Output: - IECE 203 826a
3. (7%) Write C/C++ code that automatically generates a function called “AbsCubeIt” that returns the absolute
value of the cube of the value (i.e. abs(x3) ) for any type (i.e., for integers AbsCubeIt(5) returns 125, or for
doubles AbsCubeIt(-3.0) returns 27.0). You can assume that the “*”, ”<” and *-* operators are defined (i.e.,
overloaded, if needed) for any type that is used with AbsCubeIt, and that a conversion from int “0” to the type
used is available. Use a const reference for the argument. Include sample function calls for an integer value of 2
and a double value of -8.0.
6. (10%) Given the code below, fill in the missing C++ code so that the first cout in main will work and print out
“1.0 i1.0” (just skip the + and – sign stuff before the “i” to keep it simple, after the “i” is OK)
7. (12%) Next, add the additional missing code needed below so that a line like “x = y;” below in main will work
along with the entire program and be mathematically correct. Hint: C++ also has a cos() function and both sin
and cos take radians for the angle argument. The Polar class already has the angle argument in radians. Don’t
just overload the “=” operator, you have seen the idea of what is needed here before in the mbed’s TMP36
temperature sensor class code.
class Polar {
public:
Polar(double m, double a): mag(m), ang(a){}
double mag;
double ang;
operator Complex (){return Complex(mag*cos(ang),mag*sin(ang)); };
};
int main(){
Complex x(1.0, 1.0);
cout << x << endl;
Polar y(1.414213, -3.14159/4.0);
x = y;
cout << x;
return 0;
}
//Note: could also move Polar class first and add a new Complex
//constructor with a Polar arg – compiler knows to use this to convert.
// i.e.Complex(Polar p): real(p.mag*cos(p.ang),imag(p.mag*sin(p.ang)){}
8. (20%) Write the output in the space below that is produced by the Class constructor, destructor and operator
overloading example C/C++ code provided with the test. You may have extra spaces in the table below. Recall
that most compilers also use the copy constructor to make a new copy of the object whenever pass by value is
used (instead of a pass by reference or pointer). Assume this also happens whenever a function returns a value
that was previously setup as a local variable, but not when the return creates a new object. Note: There may be
extra lines in the table provided below.