Develop a C++ program that manages a collection of rectangle objects. Each rectangle is defined by its width and height. Implement the solution using a class hierarchy where a base class Shape provides an interface for calculating the area, and a derived class Rectangle inherits from Shape and implements the area calculation. Store multiple rectangle objects in an array, compute the area for each, and determine which rectangle has the largest area. Display the index of the rectangle with the largest area.Class Structure and Implementation:Shape Class (Base Class):Attributes: None specific, mainly used for interface purposes.Member Functions:virtual double area() const = 0; Pure virtual function to compute the area of the shape.Rectangle Class (Derived Class):Inherits from Shape.Attributes:double width, height: Dimensions of the rectangle.Constructor:Rectangle(double w, double h): Initializes a rectangle with specified width and height.Member Functions:double area() const override: Computes and returns the area of the rectangle.Sample Input/Output Cases:Input:33 45 62 8Output:1
Question
Develop a C++ program that manages a collection of rectangle objects. Each rectangle is defined by its width and height. Implement the solution using a class hierarchy where a base class Shape provides an interface for calculating the area, and a derived class Rectangle inherits from Shape and implements the area calculation. Store multiple rectangle objects in an array, compute the area for each, and determine which rectangle has the largest area. Display the index of the rectangle with the largest area.Class Structure and Implementation:Shape Class (Base Class):Attributes: None specific, mainly used for interface purposes.Member Functions:virtual double area() const = 0; Pure virtual function to compute the area of the shape.Rectangle Class (Derived Class):Inherits from Shape.Attributes:double width, height: Dimensions of the rectangle.Constructor:Rectangle(double w, double h): Initializes a rectangle with specified width and height.Member Functions:double area() const override: Computes and returns the area of the rectangle.Sample Input/Output Cases:Input:33 45 62 8Output:1
Solution
Sure, here is the C++ code for the problem:
#include <iostream>
#include <vector>
#include <algorithm>
// Base class
class Shape {
public:
virtual double area() const = 0; // Pure virtual function
};
// Derived class
class Rectangle : public Shape {
private:
double width, height;
public:
// Constructor
Rectangle(double w, double h) : width(w), height(h) {}
// Overriding area() function
double area() const override {
return width * height;
}
};
int main() {
// Create a vector of Rectangle pointers
std::vector<Rectangle*> rectangles;
rectangles.push_back(new Rectangle(33, 45));
rectangles.push_back(new Rectangle(62, 8));
// Find the rectangle with the largest area
int maxIndex = 0;
double maxArea = rectangles[0]->area();
for (int i = 1; i < rectangles.size(); i++) {
double currentArea = rectangles[i]->area();
if (currentArea > maxArea) {
maxArea = currentArea;
maxIndex = i;
}
}
// Print the index of the rectangle with the largest area
std::cout << "The rectangle with the largest area is at index: " << maxIndex << std::endl;
// Clean up
for (Rectangle* rectangle : rectangles) {
delete rectangle;
}
return 0;
}
This program first creates a vector of Rectangle pointers and adds two rectangles to it. It then iterates over the vector to find the rectangle with the largest area, keeping track of the maximum area and the index of the rectangle with this area. Finally, it prints the index of the rectangle with the largest area and cleans up the dynamically allocated memory.
Similar Questions
Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
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)
Write a VB Program that calculates the area of a rectangle give that the area = lengthmultiplied by width.
Compare local and global variables. Write a program with function to find area of rectangle in C with and without global variable.
Create a module named area having four functions named triangle, square, rectangle,and circle. All these functions takes arguments according to their shape; compute andreturns the area of the shape. Import the module in the main program and call therespective function depending on the user requirement and compute the area ofrespective shape
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.