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 circle *height
void perimeter(); //No perimeter for cylinder
void volume();//cylinder volume= area of circle x height
Rectangle
int length, width;
void area();
void perimeter();
void volume();// No volume for rectangle
Square
int length;
void area();
void perimeter();
void volume();// No volume for Square
Cube
int length;
void area();
void perimeter();
void volume();
Functions area(), perimeter(), and volume() are inherited from Shape class. You must declare them as virtual functions. All others should be concrete classes i.e., all classes should inherit these functions from its base class and override.
Certain functions are not defined for certain shapes. For example, the volume is not defined for Circle and, hence, print a warning message in the overridden function. Something like the following:
void Circle::volume ( void ) {
cout << "Volume is not defined for circle." << endl;
}
Along with these functions, you should have proper constructors to construct the objects of each class. You should use appropriate formulae to calculate the area, perimeter, and volume. You can take the value of PI to be 3.14. All methods should print appropriate messages. For example, “Area of the circle is 5.2”
etc.
At this point, we have all the classes set up and now we want to test by creating some dummy shapes and by calculating the area and perimeter for two dimensions shapes and the size for three dimensions shapes.
Draw the inheritance shape.
Write a program to run the above program.
Test your program with different inputs.
#include <iostream>
#include <string>
#define PI 3.141
using namespace std;
// Defines class Shape
class Shape
{
// Prototype of functions
virtual void area() = 0;
virtual void perimeter() = 0;
virtual void volume() = 0;
};// End of class
// Defines class Circle derived from base class Shape
class Circle : public Shape
{
// Data member to store radius
int radius;
public:
// Function to set radius
void setCircle(int r)
{
radius = r;
}// End of function
// Overrides the function to calculate and display area of
circle
void area()
{
cout<<"\n Area of circle: "<<PI * radius *
radius;
}// End of function
// Overrides the function to calculate and display perimeter of
circle
void perimeter()
{
cout<<"\n Perimeter of Circle: "<<2.0 * PI *
radius;
}// End of function
// Overrides the function to display message
void volume()//No volume for circle
{
cout<<"\n Volume is not defined for circle.";
}// End of function
};// End of class Circle
// Defines class Sphere derived from base class Sphere
class Sphere : public Shape
{
// Data member to store radius
int radius;
public:
// Function to set radius
void setSphere(int r)
{
radius = r;
}// End of function
// Overrides the function to calculate and display area of
Sphere
void area()
{
cout<<"\n\n Area of Sphere: "<<4.0 * PI * radius *
radius;
}// End of function
// Overrides the function to display message
void perimeter()// No perimeter for Sphere
{
cout<<"\n No perimeter for Sphere.";
}// End of function
// Overrides the function to calculate and display volume of
Sphere
void volume()
{
cout<<"\n Volume is Sphere: " <<(4.0 / 3.0) * PI *
radius * radius;
}// End of function
};// End of class Sphere
// Defines class Cylinder derived from base class Sphere
class Cylinder : public Shape
{
// Data member to store radius and height
int radius, height;
public:
// Function to set radius and height
void setCylinder(int r, int h)
{
radius = r;
height = h;
}// End of function
// Overrides the function to calculate and display area of
Cylinder
void area()
{
cout<<"\n\n Area of Cylinder: "<<(2.0 * PI * radius) *
height;
}// End of function
// Overrides the function to display message
void perimeter()// No perimeter for Cylinder
{
cout<<"\n No perimeter for Cylinder.";
}// End of function
// Overrides the function to calculate and display volume of
Cylinder
void volume()
{
cout<<"\n Volume is Cylinder: " <<(PI * radius *
radius) * height;
}// End of function
};// End of class Cylinder
// Defines class Rectangle derived from base class Sphere
class Rectangle : public Shape
{
// Data member to store radius and height
int length, width;
public:
// Function to set length and width
void setRectangle(int l, int w)
{
length = l;
width = w;
}// End of function
// Overrides the function to calculate and display area of
Rectangle
void area()
{
cout<<"\n\n Area of Rectangle: "<<length * width;
}// End of function
// Overrides the function to calculate and display perimeter of
Rectangle
void perimeter()
{
cout<<"\n Perimeter for Rectangle: "<<2.0 * (length +
width);
}// End of function
// Overrides the function to display message
void volume()
{
cout<<"\n No Volume for Rectangle.";
}// End of function
};// End of class Rectangle
// Defines class Square derived from base class Sphere
class Square : public Shape
{
// Data member to store radius and height
int length;
public:
// Function to set length
void setSquare(int l)
{
length = l;
}// End of function
// Overrides the function to calculate and display area of
Square
void area()
{
cout<<"\n\n Area of Square: "<<length * length;
}// End of function
// Overrides the function to calculate and display perimeter of
Square
void perimeter()
{
cout<<"\n Perimeter for Square: "<<4.0 * length;
}// End of function
// Overrides the function to display message
void volume()
{
cout<<"\n No Volume for Square.";
}// End of function
};// End of class Square
// Defines class Cube derived from base class Sphere
class Cube : public Shape
{
// Data member to store radius and height
int length;
public:
// Function to set length
void setCube(int l)
{
length = l;
}// End of function
// Overrides the function to calculate and display area of
Cube
void area()
{
cout<<"\n\n Area of Cube: "<<6.0 * length;
}// End of function
// Overrides the function to calculate and display perimeter of
Cube
void perimeter()
{
cout<<"\n Perimeter for Cube: "<<6.0 * length *
length;
}// End of functionshape[0] = &circle;
// Overrides the function to calculate and display volume of
Cube
void volume()
{
cout<<"\n Volume for Cube." <<length * length *
length;
}// End of function
};// End of class Square
// main function definition
int main()
{
// Declares an array of object of class Shape of size 6
Shape *shape[6];
// Creates object of each class
Circle circle;
Sphere sphere;
Cylinder cylinder;
Rectangle rectangle;
Square square;
Cube cube;
// Assigns the object of the class to array of object
shape[0] = &circle;
shape[1] = &sphere;
shape[2] = &cylinder;
shape[3] = &rectangle;
shape[4] = □
shape[5] = &cube;
// Type cast the object to Circle pointer
// Calls the function to set radius
(*(Circle *)shape[0]).setCircle(4);
// Type cast the object to Circle pointer
// Calls the function to display are of Circle
(*(Circle *) shape[0]).area();
// Type cast the object to Circle pointer
// Calls the function to perimeter of Circle
(*(Circle *) shape[0]).perimeter();
// Type cast the object to Circle pointer
// Calls the function to volume of Circle
(*(Circle *) shape[0]).volume();
// Type cast the object to Sphere pointer
// Calls the function to set radius
(*(Sphere *)shape[1]).setSphere(5);
// Type cast the object to Sphere pointer
// Calls the function to display are of Sphere
(*(Sphere *) shape[1]).area();
// Type cast the object to Sphere pointer
// Calls the function to perimeter of Sphere
(*(Sphere *) shape[1]).perimeter();
// Type cast the object to Sphere pointer
// Calls the function to volume of Sphere
(*(Sphere *) shape[1]).volume();
// Type cast the object to Cylinder pointer
// Calls the function to set radius and height
(*(Cylinder *)shape[2]).setCylinder(4, 2);
// Type cast the object to Cylinder pointer
// Calls the function to display are of Cylinder
(*(Cylinder *) shape[2]).area();
// Type cast the object to Cylinder pointer
// Calls the function to perimeter of Cylinder
(*(Cylinder *) shape[2]).perimeter();
// Type cast the object to Cylinder pointer
// Calls the function to volume of Cylinder
(*(Cylinder *) shape[2]).volume();
// Type cast the object to Rectangle pointer
// Calls the function to set length and width
(*(Rectangle *)shape[3]).setRectangle(3, 5);
// Type cast the object to Rectangle pointer
// Calls the function to display are of Rectangle
(*(Rectangle *) shape[3]).area();
// Type cast the object to Rectangle pointer
// Calls the function to perimeter of Rectangle
(*(Rectangle *) shape[3]).perimeter();
// Type cast the object to Rectangle pointer
// Calls the function to volume of Rectangle
(*(Rectangle *) shape[3]).volume();
// Type cast the object to Square pointer
// Calls the function to set length
(*(Square *)shape[4]).setSquare(2);
// Type cast the object to Square pointer
// Calls the function to display are of Square
(*(Square *) shape[4]).area();
// Type cast the object to Square pointer
// Calls the function to perimeter of Square
(*(Square *) shape[4]).perimeter();
// Type cast the object to Square pointer
// Calls the function to volume of Square
(*(Square *) shape[4]).volume();
// Type cast the object to Cube pointer
// Calls the function to set length
(*(Cube *)shape[5]).setCube(2);
// Type cast the object to Cube pointer
// Calls the function to display are of Cube
(*(Cube *) shape[5]).area();
// Type cast the object to Cube pointer
// Calls the function to perimeter of Cube
(*(Cube *) shape[5]).perimeter();
// Type cast the object to Cube pointer
// Calls the function to volume of Cube
(*(Cube *) shape[5]).volume();
return 0;
}// End of main function
Sample Output:
Area of circle: 50.256
Perimeter of Circle: 25.128
Volume is not defined for circle.
Area of Sphere: 314.1
No perimeter for Sphere.
Volume is Sphere: 104.7
Area of Cylinder: 50.256
No perimeter for Cylinder.
Volume is Cylinder: 100.512
Area of Rectangle: 15
Perimeter for Rectangle: 16
No Volume for Rectangle.
Area of Square: 4
Perimeter for Square: 8
No Volume for Square.
Area of Cube: 12
Perimeter for Cube: 24
Volume for Cube.8
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 cla...
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,...
Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Create a program that calculates the area of various shapes.
Console
Specifications
Create an abstract class named Shape. This
class should contain virtual member function named
get_area() that returns a double type.
Create a class named Circle that inherits the
Shape class and contains these constructors and member functions:
Circle(double radius)
double get_radius()
void set_radius(double radius)
double get_area()
Create a class named Square that inherits the
Shape class and contains these constructors and member functions:
Square(double width)
double get_width()
void...
Language is JAVA.
Clarification for the Shape class (highlighted):
The following requirements specify what fields you are expected
to implement in your Shape class;
- A private String color that specifies the color of the
shape
- A private boolean filled that specifies whether the shape is
filled
- A private date (java.util.date) field dateCreated that
specifies the date the shape was created Your Shape class will
provide the following constructors;
- A two-arg constructor that creates a shape
with...
C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle Class Derive a Square Class from the Rectangle Class Derive a Right Triangle Class from the Rectangle Class Create a Circle Class Create a Sphere Class Create a Prism Class Define any other classes necessary to implement a solution Define a Program Driver Class for Demonstration a) Create a Container to hold objects of the types described above b) Create and Add two(2) objects...
The statement in the following program is in the incorrect order. Rearrange the statements so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter, for a circle, it outputs the area and circumference; and for a cylinder, it output the volume and surface area. After rearranging the statements, your...
I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...
JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...
For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...