
Patrick B. answered 04/14/21
Math and computer tutor/teacher
/************************************************
ISSUES:
(1)
What is the purpose of the 2D array of characters?
Why do you need the characters from 48-128?
This is VERY UNCLEAR!!
If it is for graphical purposes, you must specify
the dimensions of the 2-D array, and how many blocks
are in each of the 4 quadrants of the graph.
How are the edges represented? Are they ordered pairs
of co-ordinates?
A graphical package would be better than the 2-D array
2-D array is returned by function, which is a memory leak...
NEVER return the original address. Always return a COPY.
(2) Cannot have length and width dimensions in the
base class as circle, triangle do not use them
**************************************************************/
using namespace std;
#include
#include
class Shape
{
public:
virtual double GetArea()=0;
virtual double GetVolume()=0;
virtual double GetPerimeter()=0;
};
class Rectangle
{
protected:
double length;
double width;
public:
Rectangle( double l, double w)
{
length=l; width=w;
}
double GetArea() { return(length*width); }
double GetPerimeter() { return(2*length+2*width); }
void SetLength(double l) { length=l; }
void SetWidth(double w) { width = w; }
double GetLength() { return(length); }
double GetWidth() { return(width); }
};
class Triangle
{
protected:
double leg1;
double leg2;
double hypotenuse;
public :
Triangle() { leg1=leg2=hypotenuse=-1; }
Triangle( double a, double b) { leg1=a; leg2=b; }
double GetLeg1() { return(leg1); }
double GetLeg2() { return(leg2); }
double GetHypotenuse() { return(hypotenuse); }
void SetLeg1( double x) { leg1 = x; Pythagorean(); }
void SetLeg2(double x) { leg2 = x; Pythagorean(); }
void Pythagorean() { hypotenuse = sqrt( leg1*leg1 + leg2*leg2); }
double GetArea() { return( leg1 * leg2 / 2); }
double GetPerimeter() { return(leg1+leg2+hypotenuse); }
};
class Circle
{
protected:
double radius;
public:
Circle( double r) { radius = r; }
double GetPerimeter() { return(2*M_PI * radius); }
double GetArea() { return(M_PI * radius*radius); }
void SetRadius(double r) { radius = r; }
double GetRadius() { return(radius); }
};
class Trapezoid
{
protected:
double top;
double bottom;
double height;
public:
Trapezoid(double t, double b, double h) { top=t; bottom= b; height=h; }
GetArea() { return(height/2 * (top + bottom)); }
GetPerimeter()
{
double t = (bottom-top)/2;
double slant = sqrt( t*t + height*height);
return(slant*2+top+bottom);
}
};
class Box : public Rectangle
{
protected:
double depth;
public:
Box(double l, double w, double d) : Rectangle(l,w)
{
depth=d;
}
double GetVolume() { return(length*width*depth); }
};
class Ball : public Circle
{
public:
Ball (double r) : Circle(r) { }
double GetVolume()
{
return (4 * M_PI/3 * radius*radius*radius);
}
};
int main()
{
Box box(10,5,4);
cout << box.GetVolume() << endl;
Ball ball(10);
cout << ball.GetVolume();
}