Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: 1. create a third class “sphere” that inherits from class “circle” -Cr

ID: 3805805 • Letter: Q

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 [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 don’t 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');
}

Explanation / Answer

// main.cpp. j can't be done without removing pure virtual function so leaving it here as this program seems to be interactive to teach on how to use pure virtual function.

#include <iostream>
#include "sphere.h"

using namespace std;

int main()
{
   cout << "Enter coordinate x: ";
   int x, y, radius;
   cin >> x;
   sphere s;
   if (x >= 0)
   {
       cout << "Enter coordinate y: ";
       cin >> y;
      
       cout << "Enter radius: ";
       cin >> radius;
       s = sphere(x, y, radius);
   }
   else
   {
       s = sphere();
   }
  
   cout << "Surface area of sphere is : " << s.computeSurface() << endl;
   cout << "Volume of sphere is : " << s.computeVolume() << endl;
  
   cout << s;
  
   sphere *spherePTR;
   spherePTR = &s;
      
   cout << "Surface area of sphere is : " << spherePTR->computeSurface() << endl;
   cout << "Volume of sphere is : " << spherePTR->computeVolume() << endl;
   cout << *spherePTR;
  
   return 0;
}

// sphere.h

#include <iostream>
#include "circle.h"
using namespace std;
class sphere : public circle
{
private:
public:
   sphere() : circle()
   {
      
   }
sphere(int x, int y, int r) : circle(x, y, r) {
     
}
friend ostream &operator << (ostream &output, sphere s) {
output << "sphere Radius: " << s.getRadius() << endl;
output << "sphere Center Coordinates: [" << s.getX() << ", " << s.getY() << "]" << endl;
output << "Surface Area: " << s.computeSurface() << endl;
   output << "Volume Area: " << s.computeVolume() << endl;
return output;
}

double computeSurface()
{
       double pi = 3.14159265;
       double surface = 4*pi * (getRadius() * getRadius());
       return surface;
   }
  
   double computeVolume()
   {
       double pi = 3.14159265;
       double surface = (4*pi * (getRadius() * getRadius()*getRadius()))/3;
       return surface;
   }
};

// circle.h

#include <iostream>
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; };
int getX() { return x;}
int getY() {return y;}
void setRadius(int r) { radius = r; };
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; };
virtual double computeSurface() = 0; // Pure virtual function;
virtual double computeVolume() = 0; // Pure virtual function;
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote