Assume we analyze a problem related to Geometrical Shapes and identify the following conceptual classes: Square, Circle, Cube, and Sphere. We also identify the following polymorphic behaviors: area). circumference(), and volume(). Square, Circle, Cube, and Sphere classes all have area() behavior. Square and Circle classes have circumference() ability. Cube and Sphere classes have volume() ability. Design and code a solution in C++ usingObject Oriented approach.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Assume we analyze a problem related to Geometrical Shapes and identify the following conceptual classes: Square, Circle, Cube, and Sphere. We also identify the following polymorphic behaviors: area). circumference(), and volume(). Square, Circle, Cube, and Sphere classes all have area() behavior. Square and Circle classes have circumference() ability. Cube and Sphere classes have volume() ability. Design and code a solution in C++ usingObject Oriented approach. We also provide you with an application code below to guide you.

Note:  Volume of a sphere of radius R is  4/3 π R3.   . Surface area of a sphere of radius R is 4πR2.

### Artificial Intelligence Engineering Department
#### Bahçeşehir University

**Course Code: AIN-1002**

---

#### Sample Code for Managing Shape Objects in C++

The following sample code demonstrates how to manage and manipulate shape objects in C++. This code is intended to be used as a reference for learning purposes in the AI Engineering Department at Bahçeşehir University.

```cpp
for (int i=0; i<lengthOfShapes; ++i) {
    cout << "Area of " << i << "th shape is " << shapes[i]->area() << endl;
}

for (int i=0; i<lengthOfPlanar; ++i) {
    delete planar[i];
}
for (int i=0; i<lengthOfVolumetric; ++i) {
    delete volumetric[i];
}
delete [] shapes;
return 0;
```

##### Explanation:

1. **Displaying Areas of Shapes**:
   The first `for` loop iterates over all the shapes in the `shapes` array and prints out their respective areas using the `area()` method.

   ```cpp
   for (int i=0; i<lengthOfShapes; ++i) {
       cout << "Area of " << i << "th shape is " << shapes[i]->area() << endl;
   }
   ```

2. **Deleting Planar Shapes**:
   The second `for` loop iterates over all the planar shapes in the `planar` array to delete each object and free up memory.

   ```cpp
   for (int i=0; i<lengthOfPlanar; ++i) {
       delete planar[i];
   }
   ```

3. **Deleting Volumetric Shapes**:
   The third `for` loop iterates over all the volumetric shapes in the `volumetric` array to delete each object and free up memory.

   ```cpp
   for (int i=0; i<lengthOfVolumetric; ++i) {
       delete volumetric[i];
   }
   ```

4. **Cleaning Up Remaining Shapes**:
   Finally, the remaining shapes array is deleted to ensure all allocated memory is freed.

   ```cpp
   delete [] shapes;
   ```

5. **Returning from the Main Function**:
   The `return 0;` line indicates that the program has terminated successfully.

   ```cpp
   return 0;
   ```

This
Transcribed Image Text:### Artificial Intelligence Engineering Department #### Bahçeşehir University **Course Code: AIN-1002** --- #### Sample Code for Managing Shape Objects in C++ The following sample code demonstrates how to manage and manipulate shape objects in C++. This code is intended to be used as a reference for learning purposes in the AI Engineering Department at Bahçeşehir University. ```cpp for (int i=0; i<lengthOfShapes; ++i) { cout << "Area of " << i << "th shape is " << shapes[i]->area() << endl; } for (int i=0; i<lengthOfPlanar; ++i) { delete planar[i]; } for (int i=0; i<lengthOfVolumetric; ++i) { delete volumetric[i]; } delete [] shapes; return 0; ``` ##### Explanation: 1. **Displaying Areas of Shapes**: The first `for` loop iterates over all the shapes in the `shapes` array and prints out their respective areas using the `area()` method. ```cpp for (int i=0; i<lengthOfShapes; ++i) { cout << "Area of " << i << "th shape is " << shapes[i]->area() << endl; } ``` 2. **Deleting Planar Shapes**: The second `for` loop iterates over all the planar shapes in the `planar` array to delete each object and free up memory. ```cpp for (int i=0; i<lengthOfPlanar; ++i) { delete planar[i]; } ``` 3. **Deleting Volumetric Shapes**: The third `for` loop iterates over all the volumetric shapes in the `volumetric` array to delete each object and free up memory. ```cpp for (int i=0; i<lengthOfVolumetric; ++i) { delete volumetric[i]; } ``` 4. **Cleaning Up Remaining Shapes**: Finally, the remaining shapes array is deleted to ensure all allocated memory is freed. ```cpp delete [] shapes; ``` 5. **Returning from the Main Function**: The `return 0;` line indicates that the program has terminated successfully. ```cpp return 0; ``` This
**Geometrical Shapes Analysis and Computation**

In this module, we will analyze a problem related to Geometrical Shapes and identify the following conceptual classes: Square, Circle, Cube, and Sphere. Additionally, we will identify the following polymorphic behaviors: `area()`, `circumference()`, and `volume()`. The following are the behaviors for each class:
- **Square and Circle:** Both have `area()` and `circumference()` abilities.
- **Cube and Sphere:** Both have `area()` and `volume()` abilities.

A solution in C++ using Object-Oriented Programming is provided below. This code illustrates the implementation of these behaviors.

### C++ Code for Geometrical Shapes

```cpp
#include <iostream>
#include <cmath>

using namespace std;

class PlanarShape {
public:
    virtual double area() const = 0;
    virtual double circumference() const = 0;
};

class Circle : public PlanarShape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() const override { return M_PI * radius * radius; }
    double circumference() const override { return 2 * M_PI * radius; }
};

class Square : public PlanarShape {
    double side;
public:
    Square(double s) : side(s) {}
    double area() const override { return side * side; }
    double circumference() const override { return 4 * side; }
};

class VolumetricShape {
public:
    virtual double area() const = 0;
    virtual double volume() const = 0;
};

class Sphere : public VolumetricShape {
    double radius;
public:
    Sphere(double r) : radius(r) {}
    double area() const override { return 4 * M_PI * radius * radius; }
    double volume() const override { return (4.0 / 3.0) * M_PI * pow(radius, 3); }
};

class Cube : public VolumetricShape {
    double side;
public:
    Cube(double s) : side(s) {}
    double area() const override { return 6 * side * side; }
    double volume() const override { return side * side * side; }
};

int main() {
    PlanarShape *planar[] = {
        new Circle(1), // unit circle at (0,0)
        new Square(1), // unit square, top-left corner
Transcribed Image Text:**Geometrical Shapes Analysis and Computation** In this module, we will analyze a problem related to Geometrical Shapes and identify the following conceptual classes: Square, Circle, Cube, and Sphere. Additionally, we will identify the following polymorphic behaviors: `area()`, `circumference()`, and `volume()`. The following are the behaviors for each class: - **Square and Circle:** Both have `area()` and `circumference()` abilities. - **Cube and Sphere:** Both have `area()` and `volume()` abilities. A solution in C++ using Object-Oriented Programming is provided below. This code illustrates the implementation of these behaviors. ### C++ Code for Geometrical Shapes ```cpp #include <iostream> #include <cmath> using namespace std; class PlanarShape { public: virtual double area() const = 0; virtual double circumference() const = 0; }; class Circle : public PlanarShape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return M_PI * radius * radius; } double circumference() const override { return 2 * M_PI * radius; } }; class Square : public PlanarShape { double side; public: Square(double s) : side(s) {} double area() const override { return side * side; } double circumference() const override { return 4 * side; } }; class VolumetricShape { public: virtual double area() const = 0; virtual double volume() const = 0; }; class Sphere : public VolumetricShape { double radius; public: Sphere(double r) : radius(r) {} double area() const override { return 4 * M_PI * radius * radius; } double volume() const override { return (4.0 / 3.0) * M_PI * pow(radius, 3); } }; class Cube : public VolumetricShape { double side; public: Cube(double s) : side(s) {} double area() const override { return 6 * side * side; } double volume() const override { return side * side * side; } }; int main() { PlanarShape *planar[] = { new Circle(1), // unit circle at (0,0) new Square(1), // unit square, top-left corner
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Software Development
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education