Define a base class called Shape with two pure virtual functionscalculateArea() and calculatePerimeter(). Then, create two derived classes Rectangleand Circle. Implement these classes such that they inherit from the Shape class andoverride the pure virtual functions to calculate the area and perimeter of rectangles andcircles respectively.
Question
Define a base class called Shape with two pure virtual functionscalculateArea() and calculatePerimeter(). Then, create two derived classes Rectangleand Circle. Implement these classes such that they inherit from the Shape class andoverride the pure virtual functions to calculate the area and perimeter of rectangles andcircles respectively.
Solution
Sure, here is a simple implementation in C++:
#include <iostream>
#include <cmath>
// Define the base class Shape
class Shape {
public:
virtual double calculateArea() = 0; // Pure virtual function
virtual double calculatePerimeter() = 0; // Pure virtual function
};
// Define the derived class Rectangle
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() override {
return length * width;
}
double calculatePerimeter() override {
return 2 * (length + width);
}
};
// Define the derived class Circle
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() override {
return M_PI * pow(radius, 2);
}
double calculatePerimeter() override {
return 2 * M_PI * radius;
}
};
int main() {
Rectangle rect(5.0, 4.0);
std::cout << "Rectangle area: " << rect.calculateArea() << std::endl;
std::cout << "Rectangle perimeter: " << rect.calculatePerimeter() << std::endl;
Circle circ(3.0);
std::cout << "Circle area: " << circ.calculateArea() << std::endl;
std::cout << "Circle perimeter: " << circ.calculatePerimeter() << std::endl;
return 0;
}
In this code, Shape is the base class with two pure virtual functions calculateArea() and calculatePerimeter(). Rectangle and Circle are derived classes that inherit from Shape and override these pure virtual functions to calculate the area and perimeter of rectangles and circles respectively.
Similar Questions
You are designing a program to work with geometric shapes. You've created a base class Shape with virtual functions calculateArea() and calculatePerimeter(). Now, you're asked to implement derived classes Rectangle and Circle. How would you utilize virtual functions in this scenario to calculate the area and perimeter of each shape?
Implement the abstract class Shape with the abstract method calculateArea.Create the derived classes Circle and Rectangle that extend Shape and implement the calculateArea method according to the formulas for each shapeWrite a main program that reads inputs for creating a circle and a rectangle, calculates their areas, and prints the results.Input2 //number of objectsC 10 //Circle with radousR 10 10 //Rectangle with width and height....Output2 lines display the respective area and circumference314.0 62.8100 40Input3C 100R 5 5R 10 10oUTPUT31400.0 62825 20100 40uSE iNHERITANCE WITH ABSTRACT CLASS SHAPE// Find area and Find circumference must be abstract methods Circle and Rectangle should be sub classes
Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
You are tasked with implementing a geometry library in C++ that calculates the area and perimeter of various shapes such as squares, rectangles, circles, and triangles. Utilize function overloading to define separate functions for each shape and handle different combinations of parameters appropriately.Your task is to implement the following functions in the Geometry class:1) calculateArea and calculatePerimeter for squares. These functions should take integer as input to represent the side length of the square.2) calculateArea and calculatePerimeter for rectangles. These functions should take the length and width of the rectangle as inputs.3) calculateArea and calculatePerimeter for circles. These functions should take the radius of the circle as input which is not a integer.4) calculateArea for triangles. This function should take the lengths of the three sides of the triangle as inputs. Use Heron's formula to calculate the area.Your implementation should demonstrate the use of function overloading to handle different shapes and parameter combinations.
Write a Java program using " static" that utilizes your ShapeUtility class to demonstrate the calculation of area and perimeter for a circle and a rectangle. Display the total number of shapes processed at the end.The ShapeUtility class to calculate the area and perimeter of a circle and a rectangle. The totalShapes variable (static) in the ShapeUtility class should be used to keep track of the total number of shapes processed, then display the results and the total number of shapes processed at the end ( as per the Output Format).Input Format:The first line contains the radius of a circle (a double variable)The second line contains the length of a rectangle (a double variable)The third line contains the breadth of a rectangle (a double variable) Output Format:First line contains the area of a circle (a float : restrict to two precisions)Next line contains the perimeter of a circle (a float : restrict to two precisions)Next line contains the area of a rectangle (a float : restrict to two precisions)Next line contains the perimeter of a rectangle (a float : restrict to two precisions)Last line contains the total number of shapesprocesed. Testcase-1:Input:1.12.23.3Output:3.806.917.2611.004 Testcase-2:Input:1.23.54.2Output:4.527.5414.7015.404
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.