Question
Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right!

(26 points total) Write an inheritance hierarchy for classes of simple shapes. 1. Create a class Shape. Derive the class Thre
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

class Shape
{
   protected:
   double X,Y,Z;
  
  
   public:
   Shape()
   {
       X = 0.0;
       Y = 0.0;
       Z = 0.0;
   }
   Shape(double x,double y,double z)
   {
       X = x;
       Y = y;
       Z = z;
   }
   virtual void move(double x,double y,double z){}
  
  
};

class ThreeDimensionalShape : public Shape
{
  
   protected:
   double volume,surfaceArea;
  
   public:
   ThreeDimensionalShape(double x,double y,double z) : Shape(x,y,z)
   {
      
   }
   virtual void calcArea(){}
   virtual void calcVolume(){}
  
   double getX()
   {
       return X;
   }
   double getY()
   {
       return Y;
   }
   double getZ()
   {
       return Z;
   }
   double getVolume()
   {
       return volume;
   }
   double getSurfaceArea()
   {
       return surfaceArea;
   }
   void move(double x,double y,double z)
   {
       X = X+x;
       Y = Y+y;
       Z = Z+z;
   }
  
};

class Sphere : public ThreeDimensionalShape
{
  
   private:
   double radius;
  
   public:
   Sphere() : ThreeDimensionalShape(0,0,0)
   {
       radius = 0.0;
       calcArea();
       calcVolume();
   }
   Sphere(double x,double y,double z,double radius) : ThreeDimensionalShape(x,y,z)
   {
       this->radius = radius;
       calcArea();
       calcVolume();
   }
   void setRadius(double radius)
   {
       this->radius = radius;
   }
   double getRadius()
   {
       return radius;
   }
   void calcArea()
   {
       surfaceArea = 4 * 3.14* radius*radius;
   }
   void calcVolume()
   {
       volume = 4.0/3 * 3.14* radius*radius *radius;
   }
   Sphere operator +(Sphere s)
   {
       this->radius = this->radius + s.radius;
       return *this;
   }
   friend ostream &operator<<( ostream &,const Sphere &s );
  
};

ostream &operator<<( ostream &output,Sphere &s )
   {
       output << "\nSphere : ("<<s.getX()<<", "<<s.getY()<<","<<s.getZ()<<")"<<" radius : "<<s.getRadius();
return output;   
}
int main() {
  
  
   Sphere s(2,3,4,6);
  
   cout<<s;
  
   cout<<"\nArea of sphere : "<<s.getSurfaceArea();
   cout<<"\nVolume of sphere : "<<s.getVolume();
  
   Sphere s1(4,5,6,5.5);
  
   Sphere s2;
  
   s2 = s +s1;
  
   cout<<s2;
   return 0;
}

Output:

Sphere  : (2, 3,4)  radius : 6
Area of sphere : 452.16
Volume of sphere : 904.32
Sphere  : (2, 3,4)  radius : 11.5

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT