Overloaded Operators Exercise |
< Previous Next > |
#include <iostream> #include <math.h> using namespace std; #define PI 3.14159 class Circle{ public: Circle() : m_Radius( 0.0), m_Area( 0.0) { }; Circle(double r) : m_Radius( r), m_Area( PI*r*r) { }; // Based on the last digit of your M-Number write the following overloaded operator: // 1) + add areas together, calculate radius [= sqrt(area/PI)] // 2) - subtract area of other circle's area from this, calculate radius // 3) > test if this circle's area is > other circle's area // 4) <= test if other circle's area is >= this circle's area // 5) == test is this circle's area == the other circle's area // 6) << print out something informative about this circle // 7) >> read in a radius // 8) ++ Postfix increment the radius by 1.0 (and update the area) // 9) ++ Prefix increment the radius by 1.0 (and update the area) // 0) Choose one from above :) private: double m_Radius; double m_Area; }; int main(){ Circle largeCir( 33.3), smallCir( 2.2), resultCir; resultCir = largeCir + smallCir; cout << largeCir << " + " << smallCir << " = " << resultCir << endl; resultCir = largeCir - smallCir; cout << largeCir << " - " << smallCir << " = " << resultCir << endl; cout << largeCir << " > " << smallCir << ": " << (largeCir > smallCir) << endl; cout << largeCir << " >= " << smallCir << ": " << (largeCir >= smallCir) << endl; cout << largeCir << " < " << smallCir << ": " << (largeCir < smallCir) << endl; cout << largeCir << " <= " << smallCir << ": " << (largeCir <= smallCir) << endl; cout << largeCir << " == " << smallCir << ": " << (largeCir == smallCir) << endl; cout << "Enter the radius of a new circle: "; cin >> resultCir; cout << "resultCir: " << resultCir << endl; cout << "++" << largeCir << " = "; resultCir = ++largeCir; cout << resultCir << endl; cout << smallCir << "++ = "; resultCir = smallCir++; cout << resultCir << endl; return 0; }
Last Modified: