Question: In this lab assignment, you will define and implement a class Cylinder which implements the operator overloading to execute the following tester program. Data members
In this lab assignment, you will define and implement a class Cylinder which implements the operator overloading to execute the following tester program.
Data members
radius
height
Member functions
Constructor without parameter initialize the radius and height using default value 1.
Constructor with parameters.
Operator function to overload = cylinder1 = cylinder2
Assign cylinder2s radius to cylinder1s radius and assign cylinder2s height to cylinder1s height.
Operator function to overload + cylinder1 + cylinder2
Return a new Cylinder object by adding two cylinders radius and adding two cylinders height.
Operator function to overload - cylinder1 - cylinder2
Return a new Cylinder object by subtracting cylinder2s radius from cylinder1s radius and subtracting cylinder2s height from cylinder1s height.
Operator function to overload * cylinder1 - cylinder2
Return a new Cylinder object by multiplying cylinder1s radius by cylinder2s radius and multiplying cylinder1s height by cylinder2s height.
Operator function to overload ++ cylinder1 = cylinder2++
This is a post-increment operator.
Assign current cylinder2 to cylinder1, then add 1 to cylinder2s radius and height.
Operator function to overload == if (cylinder1 == cylinder2)
Return true if cylinder1s radius equal to cylinder2s radius, and cylinder1s height equal to cylinder2s height, otherwise, return false.
Operator function to overload != if (cylinder1 != cylinder2)
Return true if cylinder1s radius not equal to cylinder2s radius, and cylinder1s height not equal to cylinder2s height, otherwise, return false.
Operator function to overload <= if (cylinder1 <= cylinder2)
Return true if cylinder1s radius is <= cylinder2s radius, and cylinder1s height is <= cylinder2s height, otherwise, return false.
Operator function to overload < if (cylinder1 < cylinder2)
Return true if cylinder1s radius is less than cylinder2s radius, and cylinder1s height is less than cylinder2s height, otherwise, return false.
Operator function to overload >= if (cylinder1 >= cylinder2)
Return true if cylinder1s radius is >= cylinder2s radius, and cylinder1s height is >= cylinder2s height, otherwise, return false.
Operator function to overload > if (cylinder1 > cylinder2)
Return true if cylinder1s radius is greater than cylinder2s radius, and cylinder1s height is greater than cylinder2s height, otherwise, return false.
Operator function to overload << cout << cylinder1
Display cylinder1s radius and height.
NOTE: If the operation results in a radius or height <=0 then change the Cylinders radius and height to 1 by default.
Here is a tester program, you may copy the code to your projects tester program file ----
#include
#include "Cylinder.h"
using namespace std;
int main()
{
Cylinder Cylinder1(5.0, 10.0); //radius = 5.0 and height = 10.0
Cylinder Cylinder2(3.5, 20.0); //radius = 3.5 and height 20.0
Cylinder Cylinder3;
Cylinder Cylinder4;
Cylinder Cylinder5;
cout << "Cylinder1 --- " << Cylinder1 << endl;
cout << "Cylinder2 --- " << Cylinder2 << endl;
Cylinder3 = Cylinder1 + Cylinder2;
cout << "Cylinder1 + Cylinder2 = " << "Cylinder3 --- " << Cylinder3 << endl;
Cylinder4 = Cylinder1 - Cylinder2;
cout << "Cylinder1 - Cylinder2 = " << "Cylinder4 --- " << Cylinder4 << endl;
Cylinder5 = Cylinder1 * Cylinder2;
cout << "Cylinder1 * Cylinder2 = " << "Cylinder5 --- " << Cylinder5 << endl;
cout << "Compare Cylinder1 > Cylinder2 --- " << endl;
if (Cylinder1 > Cylinder2)
cout << "Cylinder1 is greater than Cylinder2." << endl;
else
cout << "Cylinder1 is less than or equal to Cylinder2." << endl << endl;
Cylinder1++;
cout << "Cylinder1++ --- " << endl;
cout << "New dimension of Cylinder1: " << Cylinder1 << endl;
Cylinder4 = Cylinder3++;
cout << "Cylinder4 = Cylinder3++ --- " << endl;
cout << "New dimension of Cylinder3: " << Cylinder3 << endl;
cout << "New dimension of Cylinder4: " << Cylinder4 << endl;
cout << "Cylinder4 != Cylinder3 --- Cylinder4 is "
<< (Cylinder4 != Cylinder3 ? "not equal to Cylinder3 " : "equal to Cylinder3") << endl;
cout << "Cylinder4 >= Cylinder3 --- Cylinder4 is "
<< (Cylinder4 >= Cylinder3 ? "greater or equal to Cylinder3 " : "less than Cylinder3 ") << endl;
cout << "Cylinder4 == Cylinder3 --- Cylinder4 is "
<< (Cylinder4 == Cylinder3 ? "equal to Cylinder3 " : "not equal to Cylinder3") << endl;
cout << "Cylinder4 < Cylinder3 --- Cylinder4 is "
<< (Cylinder4 < Cylinder3 ? "less than Cylinder3 " : "greater than or equal to Cylinder3") << endl;
cout << "Cylinder4 <= Cylinder3 --- Cylinder4 is "
<< (Cylinder4 <= Cylinder3 ? "less than or equal to Cylinder3 " : "greater than Cylinder3") << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
