Create class definitions using the following:
Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape
Functions: Draw, GetArea, GetDimensions, GetHeight, GetLength, GetSurfaceArea, GetVolume
Members: height, length, width – all of type double
Don’t actually write function implementations, just define the classes as you would in a header file. Use inheritance as appropriate. Use keywords such as “const” and “virtual” as appropriate. Make functions and members public, private, or protected as appropriate. Don’t forget constructors and destructors. Some classes may be abstract, meaning some functions may be pure virtual. Hopefully, given your knowledge of shapes, you can determine which Functions and Members belong to which classes.
//Shape
package ShapeHierarchy;
public abstract class Shape
{
private double length;
private double width;
public Shape()
{
length = 0;
width = 0;
}
public Shape(double len)
{
setLength(len);
}
public Shape(double len, double wid)
{
setLength(len);
setWidth(wid);
}
public void setLength(double len)
{
length = len > 0? len : 0;
}
public double getLength()
{
return length;
}
public void setWidth(double wid)
{
width = wid > 0? wid : 0;
}
public double getWidth()
{
return width;
}
public abstract double getArea();
}
// TwoDimensionalShape
package ShapeHierarchy;
public abstract class TwoDimensionalShape extends Shape
{
public TwoDimensionalShape()
{}
public TwoDimensionalShape(double len)
{
super(len);
}
public TwoDimensionalShape(double len, double wid)
{
super(len, wid);
}
public abstract double getArea();
}
// Square
package ShapeHierarchy;
public class Square extends TwoDimensionalShape
{
public Square(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Square\nSide: %.2f\n",
super.getLength());
}
}
// Circle
package ShapeHierarchy;
public class Circle extends TwoDimensionalShape
{
private final double PI = 3.14159;
public Circle(double len)
{
super(len);
}
@Override
public double getArea()
{
return PI * super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Circle\nRadius: %.2f\n",
super.getLength());
}
}
// ThreeDimensionalShape
package ShapeHierarchy;
public abstract class ThreeDimensionalShape extends Shape
{
private double height;
public ThreeDimensionalShape()
{
height = 0;
}
public ThreeDimensionalShape(double len)
{
super(len);
}
public ThreeDimensionalShape(double len, double wid)
{
super(len, wid);
}
public ThreeDimensionalShape(double len, double wid,
double hgt)
{
super(len, wid);
setHeight(hgt);
}
public void setHeight(double hgt)
{
height = hgt > 0? hgt : 0;
}
public double getHeight()
{
return height;
}
public abstract double getArea();
public abstract double getVolume();
}
// Sphere
package ShapeHierarchy;
public class Sphere extends ThreeDimensionalShape
{
private final double PI = 3.14159;
public Sphere(double len)
{
super(len);
}
@Override
public double getArea()
{
return 4 * PI * super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return 4.0/3.0 * PI * super.getLength()
* super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Sphere\nRadius: %.2f\n",
super.getLength());
}
}
//Cube
package ShapeHierarchy;
public class Cube extends ThreeDimensionalShape
{
public Cube(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return getArea() * super.getLength();
}
@Override
public String toString()
{
return String.format("Cube\nSide: %.2f\n",
super.getLength());
}
}
//Shape
package ShapeHierarchy;
public abstract class Shape
{
private double length;
private double width;
public Shape()
{
length = 0;
width = 0;
}
public Shape(double len)
{
setLength(len);
}
public Shape(double len, double wid)
{
setLength(len);
setWidth(wid);
}
public void setLength(double len)
{
length = len > 0? len : 0;
}
public double getLength()
{
return length;
}
public void setWidth(double wid)
{
width = wid > 0? wid : 0;
}
public double getWidth()
{
return width;
}
public abstract double getArea();
}
// TwoDimensionalShape
package ShapeHierarchy;
public abstract class TwoDimensionalShape extends Shape
{
public TwoDimensionalShape()
{}
public TwoDimensionalShape(double len)
{
super(len);
}
public TwoDimensionalShape(double len, double wid)
{
super(len, wid);
}
public abstract double getArea();
}
// Square
package ShapeHierarchy;
public class Square extends TwoDimensionalShape
{
public Square(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Square\nSide: %.2f\n",
super.getLength());
}
}
// Circle
package ShapeHierarchy;
public class Circle extends TwoDimensionalShape
{
private final double PI = 3.14159;
public Circle(double len)
{
super(len);
}
@Override
public double getArea()
{
return PI * super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Circle\nRadius: %.2f\n",
super.getLength());
}
}
// ThreeDimensionalShape
package ShapeHierarchy;
public abstract class ThreeDimensionalShape extends Shape
{
private double height;
public ThreeDimensionalShape()
{
height = 0;
}
public ThreeDimensionalShape(double len)
{
super(len);
}
public ThreeDimensionalShape(double len, double wid)
{
super(len, wid);
}
public ThreeDimensionalShape(double len, double wid,
double hgt)
{
super(len, wid);
setHeight(hgt);
}
public void setHeight(double hgt)
{
height = hgt > 0? hgt : 0;
}
public double getHeight()
{
return height;
}
public abstract double getArea();
public abstract double getVolume();
}
// Sphere
package ShapeHierarchy;
public class Sphere extends ThreeDimensionalShape
{
private final double PI = 3.14159;
public Sphere(double len)
{
super(len);
}
@Override
public double getArea()
{
return 4 * PI * super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return 4.0/3.0 * PI * super.getLength()
* super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Sphere\nRadius: %.2f\n",
super.getLength());
}
}
//Cube
package ShapeHierarchy;
public class Cube extends ThreeDimensionalShape
{
public Cube(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return getArea() * super.getLength();
}
@Override
public String toString()
{
return String.format("Cube\nSide: %.2f\n",
super.getLength());
}
}
Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...
in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...
Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...
C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight...
Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...
C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows: class Creature { private: int type; // 0 Human, 1 Cyberdemon, 2 Balrog, 3 elf int strength; // how much damage this Creature inflicts int hitpoints; // how much damage this Creature can sustain string getSpecies() const; //...