Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

3D Shapes

The Shape3D classes provide an easy way to calculate surface areas and volumes of common 3D shapes (solid objects).

Overview

All 3D shapes inherit from the Shape3D base class and provide two main methods:

  • area() - Calculate the surface area (total area of all outer surfaces)
  • volume() - Calculate the volume (space inside the shape)

Include

#include <imeth/shape/3D.hpp>

Base Class

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

Available Shapes

Sphere

A perfectly round 3D object (like a ball or globe).

Sphere(double radius);

Methods:

  • area() - Returns 4πr² (surface area)
  • volume() - Returns (4/3)πr³

Examples:

Sphere sphere(5.0);
std::cout << "Surface area: " << sphere.area() << "\n";   // 314.16
std::cout << "Volume: " << sphere.volume() << "\n";       // 523.6

// Basketball (radius ≈ 12 cm)
Sphere basketball(12);
std::cout << "Surface area: " << basketball.area() << " sq cm\n"; // 1809.6
std::cout << "Volume: " << basketball.volume() << " cu cm\n";     // 7238.2

Formulas:

  • Surface Area = 4 × π × r²
  • Volume = (4/3) × π × r³

Real-world: Balls, planets, bubbles, oranges


Cube

A 3D square - all sides, edges, and angles are equal.

Cube(double side);

Methods:

  • area() - Returns 6 × side² (6 square faces)
  • volume() - Returns side³

Examples:

Cube cube(4);
std::cout << "Surface area: " << cube.area() << "\n";     // 96
std::cout << "Volume: " << cube.volume() << "\n";         // 64

// Rubik's Cube (side ≈ 5.7 cm)
Cube rubiksCube(5.7);
std::cout << "Surface area: " << rubiksCube.area() << " sq cm\n"; // 194.94
std::cout << "Volume: " << rubiksCube.volume() << " cu cm\n";     // 185.19

Formulas:

  • Surface Area = 6 × side²
  • Volume = side³

Real-world: Dice, boxes, Minecraft blocks, ice cubes


Cylinder

A tube shape with circular ends (like a can or pipe).

Cylinder(double radius, double height);

Methods:

  • area() - Returns 2πr² + 2πrh (top + bottom + side)
  • volume() - Returns πr²h

Examples:

Cylinder cylinder(3, 10);
std::cout << "Surface area: " << cylinder.area() << "\n";   // 245.04
std::cout << "Volume: " << cylinder.volume() << "\n";       // 282.74

// Soda can (radius ≈ 3 cm, height ≈ 12 cm)
Cylinder sodaCan(3, 12);
std::cout << "Can surface: " << sodaCan.area() << " sq cm\n";    // 282.74
std::cout << "Can volume: " << sodaCan.volume() << " cu cm\n";   // 339.29
std::cout << "That's " << sodaCan.volume() / 1000 << " liters\n"; // 0.339 L

Formulas:

  • Surface Area = 2πr² + 2πrh = 2πr(r + h)
  • Volume = π × r² × h

Real-world: Cans, pipes, drums, tree trunks, water tanks


Cone

A shape with a circular base that tapers to a point (like an ice cream cone).

Cone(double radius, double height);

Methods:

  • area() - Returns πr² + πr√(h² + r²) (base + curved surface)
  • volume() - Returns (1/3)πr²h

Examples:

Cone cone(4, 9);
std::cout << "Surface area: " << cone.area() << "\n";     // 175.93
std::cout << "Volume: " << cone.volume() << "\n";         // 150.8

// Ice cream cone (radius ≈ 3 cm, height ≈ 10 cm)
Cone iceCream(3, 10);
std::cout << "Cone surface: " << iceCream.area() << " sq cm\n";  // 125.66
std::cout << "Cone volume: " << iceCream.volume() << " cu cm\n"; // 94.25

Formulas:

  • Surface Area = πr² + πr√(h² + r²)
    • Base area: πr²
    • Slant area: πr√(h² + r²)
  • Volume = (1/3) × π × r² × h

Fun fact: A cone's volume is exactly 1/3 of a cylinder with the same base and height!

Real-world: Ice cream cones, traffic cones, party hats, funnels


Torus

A donut shape (a tube bent into a circle).

Torus(double major_radius, double minor_radius);

Parameters:

  • major_radius - Distance from center of torus to center of tube
  • minor_radius - Radius of the tube itself

Methods:

  • area() - Returns 4π²Rr (surface area)
  • volume() - Returns 2π²Rr²

Examples:

Torus torus(10, 3);  // Big radius 10, tube radius 3
std::cout << "Surface area: " << torus.area() << "\n";    // 1184.4
std::cout << "Volume: " << torus.volume() << "\n";        // 1776.6

// Swimming tube (major ≈ 40 cm, minor ≈ 10 cm)
Torus swimmingTube(40, 10);
std::cout << "Tube surface: " << swimmingTube.area() << " sq cm\n";  // 15791.4
std::cout << "Tube volume: " << swimmingTube.volume() << " cu cm\n"; // 78957.1

Formulas:

  • Surface Area = 4π² × R × r
  • Volume = 2π² × R × r²

Where:

  • R = major radius (big circle)
  • r = minor radius (tube)

Real-world: Donuts, swimming tubes, O-rings, bagels, hula hoops


Polymorphism Example

Since all shapes inherit from Shape3D, you can use them polymorphically:

#include <vector>
#include <memory>

std::vector<std::unique_ptr<Shape3D>> shapes;

shapes.push_back(std::make_unique<Sphere>(5));
shapes.push_back(std::make_unique<Cube>(4));
shapes.push_back(std::make_unique<Cylinder>(3, 10));

for (const auto& shape : shapes) {
    std::cout << "Surface area: " << shape->area()
              << ", Volume: " << shape->volume() << "\n";
}

Volume Comparisons

Sphere vs Cube

Sphere sphere(5);
Cube cube(5);

std::cout << "Sphere volume: " << sphere.volume() << "\n";  // 523.6
std::cout << "Cube volume: " << cube.volume() << "\n";      // 125

// A sphere with radius = cube side has ~4.2x more volume!

Cylinder vs Cone

Cylinder cylinder(4, 9);
Cone cone(4, 9);  // Same base and height

std::cout << "Cylinder volume: " << cylinder.volume() << "\n"; // 452.39
std::cout << "Cone volume: " << cone.volume() << "\n";         // 150.8

// Cone is exactly 1/3 of cylinder volume!
std::cout << "Ratio: " << cylinder.volume() / cone.volume() << "\n"; // 3.0

Real-World Applications

1. Water Tank Capacity

Cylinder waterTank(1.5, 3);  // 1.5m radius, 3m height
double liters = waterTank.volume() * 1000;  // 1 m³ = 1000 liters
std::cout << "Tank holds " << liters << " liters\n";  // 21,205 liters

2. Paint for a Sphere

Sphere globe(0.5);  // 0.5m radius globe
double paintPerSqM = 0.1;  // liters per square meter
double paintNeeded = globe.area() * paintPerSqM;
std::cout << "Paint needed: " << paintNeeded << " liters\n";

3. Gift Box Volume

Cube giftBox(20);  // 20cm × 20cm × 20cm
double volumeCm3 = giftBox.volume();  // 8000 cm³
double volumeLiters = volumeCm3 / 1000;
std::cout << "Box can hold " << volumeLiters << " liters\n";  // 8 liters

4. Ice Cream Cone vs Scoop

Cone cone(3, 10);           // Cone: radius 3cm, height 10cm
Sphere scoop(3);            // Scoop: radius 3cm

std::cout << "Cone volume: " << cone.volume() << " cu cm\n";   // 94.25
std::cout << "Scoop volume: " << scoop.volume() << " cu cm\n"; // 113.1

// The scoop has more ice cream than the cone can hold!
if (scoop.volume() > cone.volume()) {
    std::cout << "Better order a bigger cone!\n";
}

5. Swimming Pool

Cylinder pool(5, 1.5);  // 5m radius, 1.5m deep
double volume = pool.volume();
double liters = volume * 1000;

std::cout << "Pool volume: " << volume << " cubic meters\n";
std::cout << "That's " << liters << " liters of water!\n";
std::cout << "Or " << liters * 0.264172 << " gallons\n";

Quick Reference Table

ShapeArea FormulaVolume FormulaExample
Sphere4πr²(4/3)πr³Ball, planet
Cube6s²Dice, box
Cylinder2πr(r+h)πr²hCan, pipe
Coneπr² + πrs(1/3)πr²hIce cream cone
Torus4π²Rr2π²Rr²Donut, tube

Where:

  • r = radius
  • s = side
  • h = height
  • R = major radius (for torus)

Volume Relationships

Interesting Facts:

  • A cone's volume = 1/3 of a cylinder (same base & height)
  • A sphere's volume = 2/3 of a cylinder (if cylinder has radius r and height 2r)
  • A cube with side s has less volume than a sphere with radius s

Common Volume Units

  • Metric: cubic meters (m³), cubic centimeters (cm³), liters (L)
    • 1 m³ = 1,000 liters
    • 1 liter = 1,000 cm³
  • Imperial: cubic feet (ft³), cubic inches (in³), gallons (gal)
    • 1 gallon ≈ 3,785 cm³

Tips

  • Surface area is measured in square units (m², cm²)
  • Volume is measured in cubic units (m³, cm³)
  • Use Cylinder for most containers (cans, tanks, tubes)
  • Use Sphere for balls and planets
  • Use Cube for perfect boxes and dice
  • Torus is rare but fun for donuts and swimming tubes!
  • Remember: Volume tells you how much fits inside, surface area tells you how much material you need to cover it