Question: Question: 1. create a third class sphere that inherits from class circle -Create the appropriate accessors and mutators -a default constructor (sets the center to
Question:
1. create a third class sphere that inherits from class circle
-Create the appropriate accessors and mutators
-a default constructor (sets the center to [0,0], and the radius to 1)
-an (appropriate) overloaded constructor
-computeSurface() function
-computeVolume() function
-Overloaded << operator that prints coordinates of the center, radius, surface, and the volume
-calls the computeSurface() and computeVolume()
2. Change the Class circle to become an Abstract one by:
a. re-declaring computeSurface() as pure virtual
b. Declare a function computeVolume() as pure virtual (5 pts)
3. In the main function:
a. Ask the user to enter a point (i.e., x and y coordinates)
i.If the user types a negative value for coordinate x, then call the default constructor and dont ask for coordinate y and the radius.
b.Ask for the radius
c. Construct a Sphere S
d.Compute its surface using (overridden/redefined) computeSurface()
e. Compute its volume using (overridden/redefined) computeVolume()
f. Call the << operator (for Display).
g. Declare a pointer spherePTR, and make it point to S
h. Re-Print the surface and volume using spherePTR
i. RE-do steps:
i.Step a
ii.Step b
j. Construct a circle
i.What happens?
.
ii.Why?
.
iii.Fix the problem in order to have a circle object constructed
Briefly explain, how you fixed the problem:
iv.Now that the circle is constructed, declare a circlePTR pointer and have it point to the created circle
v.Print the circle surface using circlePTR and computeSurface()
-(You should have redefined the computeSurface())
k. Now, have/make the circle pointer (circlePTR) point to the previously created sphere
l. Call computeSurface() using circlePTR, and print the result
m. Which computeSurface() is called? i.e., the circle or the sphere one?
.....
n. Ask the user if he wants to repeat! (y/n)? [use a doWhile loop]
Initial Code:
#include using namespace std;
class circle { private: int x; int y; int radius; public: circle(int x, int y, int r) { x = x; y = y; radius = r; } circle() { x = 0; y = 0; radius = 1; }
int getRadius() { return radius; }; void setRadius(int r) { radius = r; }; void setX(int x) { this->x = x; } void setY(int y) { this->y = y; };
double computeSurface(); friend ostream &operator << (ostream &output, circle c) { output << "Circle Radius: " << c.radius << endl; output << "Circle Center Coordinates: [" << c.x << ", " << c.y << "]" << endl; output << "Surface Area: " << c.computeSurface() << endl; return output; } };
double circle::computeSurface() { double pi = 3.14159265; double surface = pi * (getRadius() * getRadius()); return surface; }
//cylinder class inheriting from circle class cylinder : circle { private: int height; public: cylinder(int x, int y, int r, int h) : circle(x, y, r) { height = h; } cylinder() : circle() { height = 2; } void setHeight(int h) { this-> height = h; } double computeVolume(); friend ostream &operator << (ostream &output, cylinder c) { output << "Cylinder Radius: " << c.getRadius() << endl; output << "Cylinder Height: " << c.height << endl; output << "Volume: " << c.computeVolume(); return output; }
};
double cylinder::computeVolume() { double volume = computeSurface() * height; return volume; }
int main() { int x, y, r, h; circle c; cylinder cyl; char repeat; do { //circle cout << "Enter x point: "; cin >> x; if (x <= 0) { circle(); } else { cout << "Enter y point: "; cin >> y; cout << "Enter radius: "; cin >> r; c.setX(x); c.setY(y); c.setRadius(r); } cout << c << endl; //cylinder cout << "Enter height: "; cin >> h; if (h < 0) { cylinder(); } else { cyl.setHeight(h); } cout << cyl;
cout << " Repeat process? (y/n)"; cin >> repeat; cout << endl; } while (repeat == 'y' || repeat == 'Y'); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
